Calculate week number & day number

ESP32 - Wifi
Android version 11
Blynk server
v 0.6.1

Hi, i’m using RTCwidget for getting the Blynk server time.
In my project i need to calculate the week number (week of year) and the day number (day of week starting from monday).
Is there an easy way to do this? Are there variables in the RTC that i can use?

The Time library does all of this for you.
Just be aware that if you use the Blynk time input widget then it uses Monday as day 1, whereas the Time library uses Sunday as day 1.

The Sketch Builder time input examples are a good starting point, as are the Time library examples.

Also, be aware that I think Blynk uses UINX time in milliseconds and the Time library might default to seconds (since 1st Jan 1970), so you may need to divide the Blynk time value by 1000

Pete.

Hi Pete, thanks for the quick answer.

Could you be a bit more specific?

In timelib i could detect the weekday() function that returns the weeknumber. This solves half of my problem.

I browsed the timelib.h and time.h libraries and i could not find a function that gives me a week number based on a time/date (retrieved from RTC).
I also, as per your suggestion, looked into the Blynk example builder to the Time Input (simle/advanced), but no luck there. Or did you mean something else?

This might help
https://playground.arduino.cc/Code/Time/

I think there’s a variable called %U in Time that returns the week number.

Pete.

I thought i was getting closer to the answer, but still 'im missing something.
Does anybody see the mistake?
I’m getting 00 as week number (in both functions)

#include "time.h"
#include <WidgetRTC.h>

BLYNK_CONNECTED()
{
  terminal.print("CONNECTED to server" );
  terminal.flush();
  
  //Sync time on connection
  rtc.begin();
  
  String currentime= String(hour())+":"+minute()+":"+second();
  terminal.print(currentime + " Blynk Server Time Sync'ed");
  terminal.flush();
  
  int test;
  getWeek(test);
  terminal.println("weeknbr"+ test);
  terminal.flush();
}

void getWeek(int& week) {
     
  time_t rawtime;
  struct tm * timeinfo;
  char buffer[4];

  time(&rawtime);
  timeinfo = localtime(&rawtime);

  strftime(buffer,4,"%W",timeinfo);   // '%W' = week number of the year, eg 1/1/09 == 1

  week = atoi(buffer);     // convert char to int

  terminal.print("Week number is ");
  terminal.println(String(week)); //Prints out week number
  terminal.flush();
}´´´
String Weekday = dayAsString(weekday());

Hi Blynk_Coeur,
thanks for your suggestion, but the purpose is to get the week number in the year. So, for today (4/12/2021) the result should be week ‘48’.

Sorry , I didn’t read well :wink:

Try this

int main ()
{
  time_t rawtime;
  struct tm * timeinfo;
  char buffer [80];

  time (&rawtime);
  timeinfo = localtime (&rawtime);

  strftime (buffer,80,"%U",timeinfo);
  puts (buffer);

  return 0;
}

Hi John93, unfortunately this didn’t work out. Same result: week 00

Detailed debugging made me come to the conlusion that the localtime function (in timeinfo = localtime (&rawtime);) always gave me 1 jan1970. So, this means that rtc.begin() doesn’t affect the localtime.
I could solve it by adding:

timeinfo->tm_year = year() - 1900;
  timeinfo->tm_mon = month() - 1;
  timeinfo->tm_mday = day();
  
  mktime ( timeinfo );

This now does the job fine for me. I don’t know whether this is the most efficient, but it suits the purpose for me :wink:

2 Likes