1

In Arduino C+, I wanted to avoid the year 2038 overflow problem when using 32-bit, signed time_t type, so that I'd like to specifically use the Time.h from Teensy (Or TimeLib.h for that matter; I am writing code for Teensy 3.5 on Arduino 1.8.7).

But the IDE seems to ignore Teensy's Time.h, in which time_t is defined as:

typedef unsigned long time_t;

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 show that time_t is actually defined somewhere as long int:

invalid conversion from 'const char*' to 'time_t {aka long int}' [-fpermissive]

I even tried copying the Teensy's Time folder (https://github.com/PaulStoffregen/Time) to my sketch folder, and do this to no avail:

#include "Time\TimeLib.h"

How to make sure that I am using an unsigned, 32-bit time_t in Arduino? 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!

1 Answer 1

1

In teensy TimeLib.h it's defined as:

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

and the sys/_types.h defines it as:

#define _TIME_T_    long        /* time() */
typedef _TIME_T_    __time_t;

Used in several places as:

#if !defined(__time_t_defined) && !defined(_TIME_T_DECLARED)
typedef _TIME_T_    time_t;
#define __time_t_defined
#define _TIME_T_DECLARED
#endif

So it's not such a mystery it's ignored. Otherwise you wouldn't be able to compile because of conflicting types.

2
  • Thanks, now I can see more clearly. Is there a way to override the definition in sys/_types.h?
    – Dave
    Jun 3, 2019 at 13:36
  • @Dave depends on usage. If you want to use TimeLib, it might be better to change time_t to utime_t or something similar everywhere. Or maybe hide it into separate namespace
    – KIIV
    Jun 3, 2019 at 13:43

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.