I wanted to avoid the year 2038 problem when using 32-bit signed time_t type, so that I want to specifically use the Time.h from Teensy (I am writing code for Teensy 3.5).
But the IDE seems to ignore Teensy’s Time.h, in which time_t is defined as
#if !defined(__time_t_defined) // avoid conflict with newlib or other posix libc
typedef unsigned long time_t;
#endif
I find out that no matter what I include, the time_t type I am using is compiled as “long int”. This code reveals that:
time_t t = "ABC";
The compiler will complain: invalid conversion from ‘const char*’ to ‘time_t {aka long int}’ [-fpermissive]
How to make sure that I am using 32-bit, unsigned time_t?
Also I want when I call now() it’s Teensy’s now() that returns unsigned long time_t, not the built-in long int time_t
On filename case-insensitive operating systems like Windows, there is a problem with the Time.h filename used by that library. An #include directive for Time.h ends up matching to the file time.h from the toolchain. To avoid this problem, the file was renamed TimeLib.h:
So you only need to change your #include directive from:
Yes, I will also update this forum if I got an answer there
gfvalvo:
Compiling for an Uno on IDE v1.8.5 this:
#include "TimeLib.h"
time_t xxx = “hello”;
void setup() {
}
void loop() {
}
Gives the error message you'd expect:
C:\Users\tr001221\AppData\Local\Temp\arduino_modified_sketch_480339\sketch_jun03a.ino:2:14: warning: invalid conversion from ‘const char*’ to ‘time_t {aka long unsigned int}’ [-fpermissive]
time_t xxx = “hello”;
I tried an got the same like you for UNO on Arduino 1.8.7.
So this only happens when the board is Teensy. Strange
mariocaptain:
Yes, I will also update this forum if I got an answer there
Thanks!
mariocaptain:
So this only happens when the board is Teensy. Strange
If you turn on warnings (File > Preferences > Compiler warnings) and then compile the following sketch for Teensy 3.5, you can see that the __time_t_defined macro is defined in the toolchain:
:\Users\per\AppData\Local\Temp\arduino_modified_sketch_974617\sketch_jun03a.ino:1:0: warning: "__time_t_defined" redefined
#define __time_t_defined foobar
^
In file included from e:\programfiles\arduinoide\arduino-1.8.8-td\hardware\tools\arm\arm-none-eabi\include\sys\select.h:26:0,
from e:\programfiles\arduinoide\arduino-1.8.8-td\hardware\tools\arm\arm-none-eabi\include\sys\types.h:68,
from e:\programfiles\arduinoide\arduino-1.8.8-td\hardware\tools\arm\arm-none-eabi\include\stdio.h:61,
from E:\ProgramFiles\ArduinoIDE\arduino-1.8.8-td\hardware\teensy\avr\cores\teensy3/Print.h:35,
from E:\ProgramFiles\ArduinoIDE\arduino-1.8.8-td\hardware\teensy\avr\cores\teensy3/Stream.h:24,
from E:\ProgramFiles\ArduinoIDE\arduino-1.8.8-td\hardware\teensy\avr\cores\teensy3/HardwareSerial.h:252,
from E:\ProgramFiles\ArduinoIDE\arduino-1.8.8-td\hardware\teensy\avr\cores\teensy3/WProgram.h:46,
from C:\Users\per\AppData\Local\Temp\arduino_build_252405\pch\Arduino.h:6:
e:\programfiles\arduinoide\arduino-1.8.8-td\hardware\tools\arm\arm-none-eabi\include\sys\_timeval.h:41:0: note: this is the location of the previous definition
#define __time_t_defined
You can see the code in TimeLib.h will only define the time_t type alias if the __time_t_defined macro is not already defined:
#if !defined(__time_t_defined) // avoid conflict with newlib or other posix libc
typedef unsigned long time_t;
#endif