How to specifically use Teensy's Time.h instead of Arduino built in?

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

Thanks in advance

I’ll assume you’re talking about this library:

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:

#include <Time.h>

to:

#include <TimeLib.h>
#include <TimeLib.h>

I did this but the compiler had the same complaint.

I even tried copying the Teensy's Time folder (GitHub - PaulStoffregen/Time: Time library for Arduino) to my sketch folder, and do this directive:

#include "Time\TimeLib.h"

But still the same, time_t still got overridden by some built-in Arduino definition, compiler shows the same error.

The problem could be with your installed tool chain. Or it could be in your code, which you didn't post. Hard to tell.

Also posted at:

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";

gfvalvo:
The problem could be with your installed tool chain. Or it could be in your code, which you didn’t post. Hard to tell.

This is my entire program:

#include <TimeLib.h>
time_t t = "ABC";
void setup() {
}

void loop() {
}

Board selected is Teensy 3.5

pert:
Also posted at:
https://stackoverflow.com/questions/56426827

Yes, I will also update this forum if I got an answer there :slight_smile:

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 :confused:

mariocaptain:
Yes, I will also update this forum if I got an answer there :slight_smile:

Thanks!

mariocaptain:
So this only happens when the board is Teensy. Strange :confused:

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:

#define __time_t_defined foobar
void setup() {}
void loop() {}
:\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

It looks like '__time_t_defined' is already defined when compiling for T3.5 (perhaps other Teensys too). So the conditional:

#if !defined(__time_t_defined) // avoid conflict with newlib or other posix libc
typedef unsigned long time_t;
#endif

is false and 'time_t' is not defined to 'unsigned long'