With TimeLib included month(t) errors with "month cannot be used as a function

I am using the IDE 2.2.1 to program a M5Paper device, effectively an ESP32. I have installed TimeLib,h library via the library manager (where it is just called Time) and included <TimeLib.h> in my sketch. I am using the library to decode a UTC timestamp from openweathermaps. I can decode the day of the week with int wday = weekday(t); but if I try the same with day(t) or month(t) the sketch fails to compile with the warning "month cannot be used as a function" or "day cannot be used as a function"

However if I use Serial.print(month(t)); or Serial.print(day(t)); the sketch compiles and the month and/or day appear in the serial monitor.

How can I use the month(t) function of the TimeLib.h library to give me a variable to use elsewhere without the ide warning me that I cannot use month as a function?

Please post the complete code, using code tags.

Also, post a link to the source code for the exact time library you are using.

I am using the TimeLib.h library ver. 1.6.1 by Michael Margolis, which is the one offered in the library section of the IDE 2.2.1. Arduino Playground - Time

I usually post code but in this case I am only experimenting and do not really have any code to post. I think I have explained the issue fairly well and let's wait and see if anyone has has the same problem with this library.

I don't have that problem, with that library.

I conclude that the code you refuse to post contains errors.

Good luck.

#include <TimeLib.h>

void setup() {
   Serial.begin(115200);
   setTime(16, 6, 30, 7, 12, 2023);
   time_t t = now();
   int wkdy = weekday(t);
   Serial.print("Weekday: "); Serial.println(wkdy);
   int dom = day(t);
   Serial.print("Day: "); Serial.println(dom);
   int mon = month(t);
   Serial.print("Month: "); Serial.println(mon);
}

void loop() {
}

The above compiles just fine for me.

I'll bet a donut that the @steveinaustria is doing something like:

int month = month(t);

and getting an error like this:

/home/me/Documents/sketchbook/Uno_R3/test/test.ino: In function 'void setup()':
/home/me/Documents/sketchbook/Uno_R3/test/test.ino:11:23: error: 'month' cannot be used as a function
    int month = month(t);
                       ^
Used library Version Path
Time         1.6.1   /home/me/Documents/sketchbook/libraries/Time

And I have no trouble understanding why the compiler is objecting. I just defined month locally to be an int variable, then tried to use it as a function. That's not going to work.

The reason that int wday = weekday(t); worked was that the variable 'wday' wasn't redefining the function weekday.

Call your variables dow and mon and you'll be fine.

And next time post your code so we don't have to guess.

It seems that you do have some code after all

You win a donut! Thanks.

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