Gap of 1 day in time.h

Hello,

i am a little bit confused by getting a difference of one day using the time.h:

Using

Serial.println(&timeinfo, "%j");       // day of the year (0-366)
Serial.println(timeinfo.tm_yday);   // day of the year (0-366)

results in:

22:11:54.840 -> 093
22:11:54.840 -> 92

in the serial monitor. So there is a gap of one day. Can someone explain it to me please?

Thank you!

Please post the code, using code tags, and a link to the exact library you are using. Many Arduino libraries have the same or very similar names.

I'm curious as to why you used %j as the format specifier.

This one forgot leap day.

tm_yday always ranges from 0 to 365 (364 on non-leap years). It's the number of days since January 1 (so January 1 is naturally 0).

Where is the first Serial.println usage documented which uses a format string? I can only find references to the second argument being either the base to print an integer in or the number of digits to print after the decimal place with a floating-point number.

Edit: here's the documentation I've found:

Edit2: I tried this on Arduino Uno and it says

sketch.ino:7:31: error: no matching function for call to 'println(tm*, const char [3])'
 Serial.println(&timeinfo, "%j");       // day of the year (0-366)

What type of Arduino is this on?

Might be ESP32, where printf format specifiers are allowed. But it is anyone's guess what "%j" is supposed to do.

The esp32 print() statement can take a tm struct (from time.h) as an argument: arduino-esp32/cores/esp32/Print.cpp at master · espressif/arduino-esp32 · GitHub

It then uses strftime() https://cplusplus.com/reference/ctime/strftime/ to format it.

%j is day of year (Julian date range 1 to 366)

2 Likes

Hello,

i am sorry about the circumstances, this was my very first post.
And thank you all for the fast response and help!

I am programming a M5StampS3. It is a ESP32S3. For the output i use a
0.96 OLED. So i was interested to use the first "0" with the value and center it
(manually) on the display.

So i get the time by connecting the ESP to an ntp-server. I need the "Day of Year"
to save it permanently to the ESP to do something in the code only once a day.
If the ESP restarts it is easy to have a look if he has starts this day befor.

This is a part of the code:


void printLocalTime(){
  struct tm timeinfo;
  /*
struct tm
{
int    tm_sec;   //   Seconds [0,60]. 
int    tm_min;   //   Minutes [0,59]. 
int    tm_hour;  //   Hour [0,23]. 
int    tm_mday;  //   Day of month [1,31]. 
int    tm_mon;   //   Month of year [0,11]. 
int    tm_year;  //   Years since 1900. 
int    tm_wday;  //   Day of week [0,6] (Sunday =0). 
int    tm_yday;  //   Day of year [0,365]. 
int    tm_isdst; //   Daylight Savings flag. 
}
 */  
  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return;
  }
  u8g2.setFont(u8g2_font_smart_patrol_nbp_tf);
  u8g2.setCursor(6, 30);
  u8g2.print(&timeinfo, "%d %b %Y");
  u8g2.setFont(u8g2_font_crox4hb_tf);
  u8g2.setCursor(25, 53);
  u8g2.print(&timeinfo, "%H:%M:%S");

  dayofyear = timeinfo.tm_yday;
  char doy[4];
  strftime(doy, sizeof(doy), "%j", &timeinfo);

  u8g2.setFont(u8g2_font_smart_patrol_nbp_tf);
  u8g2.setFontDirection(1);
  u8g2.setCursor(110, 25);
  //u8g2.print(dayofyear);
  u8g2.print(&timeinfo, "%j");
  u8g2.setFontDirection(0);

preferences.putUInt("Tag", dayofyear);
preferences.end();

// testing values
Serial.println(&timeinfo, "%j");
Serial.println(doy);
Serial.println(timeinfo.tm_yday);
Serial.println(dayofyear);

}

Looks like the gap is the missing lead day. So it will work fine the next three years? :wink:

The "gap" is due to the different numbering systems which depend on whether the first of January is regarded as day 0 or day 1. If you are consistent and use one system it should not matter in this application. If the day is somehow visible to the user I would prefer having the of January as day 1.

The following is incorrect. The range is 1 to 366.

Hello,

thanks to 6v6gt i got it with your last explanation. Thank you very much!

I will use one value constantly.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.