Sketch providing EPOCH date time from NTP, for start and end of current week?

Hi and good evening

I am working on a piece for the ESP8266 and need the EPOCH value for the start and end of the current week.

Example - assuming the current date is Wednesday 20th Jan or indeed Thursday 21st of Jan, I want two variables that give me the EPOCH start of that week (Monday 18th Jan 00:01) and the end of that week (Sunday 24th Jan 23:59).

I will be using these values to retrieve data from an API between those dates - always based on the current day - its that week only I want the data for.

If anyone has any handy sketches that would be a great help.

Thanks in advance

To get the start of the week, truncate to the start of the day and subtract enough days to get to the start of the week. To get to the end of the week, add 7 days and subtract 1 second.

// Assuming epoch is in seconds:
const unsigned long SECOND = 1;
const unsigned long MINUTE = 60 * SECOND;
const unsigned long HOUR = 60 * MINUTE;
const unsigned long DAY = 24 * HOUR;
const unsigned long WEEK = 7 * DAYS;

unsigned long StartOfWeek(unsigned long epochTime)
{
  unsigned long startOfDay = epochTime - (epochTime % DAY);
  return startOfDay - (dayOfTheWeek(epochTime) * DAY);
  // NOTE: If EPOCH starts on the first day of a week, this reduces to:
  // return epochTime - (epochTime % WEEK);
}

unsigned long EndOfWeek(unsigned long epochTime)
{
  return StartOfWeek(epochTime) + WEEK - SECOND;
}

Which library are you using for the time functions? There may already be a pre-defined function.

johnwasser:
To get the start of the week, truncate to the start of the day and subtract enough days to get to the start of the week. To get to the end of the week, add 7 days and subtract 1 second.

// Assuming epoch is in seconds:

const unsigned long SECOND = 1;
const unsigned long MINUTE = 60 * SECOND;
const unsigned long HOUR = 60 * MINUTE;
const unsigned long DAY = 24 * HOUR;
const unsigned long WEEK = 7 * DAYS;

unsigned long StartOfWeek(unsigned long epochTime)
{
 unsigned long startOfDay = epochTime - (epochTime % DAY);
 return startOfDay - (dayOfTheWeek(epochTime) * DAY);
 // NOTE: If EPOCH starts on the first day of a week, this reduces to:
 // return epochTime - (epochTime % WEEK);
}

unsigned long EndOfWeek(unsigned long epochTime)
{
 return StartOfWeek(epochTime) + WEEK - SECOND;
}

After some slight changes this worked great for me - also taught me a lot about EPOCH times - thank you