Unexpected month value retrieved from printLocalTime()

I am calling the printLocalTime() function to retrieve the month and day as part
of timestamping the last time a pressure pump ran.
The day value is correct but not the month value. Instead of getting a numeric 2 for
the month of February there is a 1. Am I misunderstanding something about how this
value is presented?

void printLocalTime() {  // this function monitors current time of day to initiate
                         // pump counter reset at midnight
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
    return;
  }
  time_hour = timeinfo.tm_hour;  // tm structure element "hour" from time.h
  time_min = timeinfo.tm_min;    //                     "minute"  "    "
  time_sec = timeinfo.tm_sec;    //                      "second"

  time_month = timeinfo.tm_mon;
  time_day = timeinfo.tm_mday;

  if (time_hour == hour_trigger && time_min == min_trigger && time_sec == sec_trigger) {
    Serial.println("Reset counters...");
    counter_reset = true;
    delay(3000);  // used to avoid conflict as time rolls over to new day
  }

  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S zone %Z %z ");
  Serial.println();
  Serial.println("Month [timeinfo.tm_mon] is: ");
  Serial.println(timeinfo.tm_mon);
  Serial.println();
}

This the serial monitor output where displaying the entire date/time/time zone
and just the value of the timeinfo.tm_mon:

Tuesday, February 27 2024 14:30:18 zone CST -0600 

Month [timeinfo.tm_mon] is: 
1

Tuesday, February 27 2024 14:30:18 zone CST -0600 

Month [timeinfo.tm_mon] is: 
1

Month value in the tm struct is 0-11:

https://mikaelpatel.github.io/Arduino-RTC/d8/d5a/structtm.html

1 Like

Month numbers are starts from zero, as well as a weekdays numbers

1 Like

Hi edthewino,

I see your question has already been answered, but hopefully to add some information that may be useful, here goes.

The 'indexing' for an array begins at 0, so for this example the month names would likely be stored in an array, month = {Jan, Feb, Mar, .... Dec.}, meaning to access the second element in this list, you would call month[1], hence why the value you are being given is a 1!

Hope this helps!
-Isaac

Thank you for the explanation.... :+1:

1 Like

I appreciate that link you provided and I thought maybe it was something to do
with array storage. Looking at that link it shows the value for days starting with a 1 for the first day of the month, not 0.
So why would the months also not have been set up starting with a 1?

int8_t tm_wday
Days since Sunday [0-6]. More...

Very silly they did that, but it seems as though that is due to the way that they define the months vs days.
In the link @BitSeeker sent, the month value is defined as "months since January", whereas days are just defined as "day in month".

I agree, for consistency sake it would make more sense that they make the day defined as "days since the first of the month", or something similar. Days seems like the outlier here.

No, you have the actual date, and the days since Sunday

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