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)
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?
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.