Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: time_t is a signed integer - is this right?

  1. #1
    Junior Member
    Join Date
    Mar 2021
    Posts
    2

    time_t is a signed integer - is this right?

    Hi,

    First of all thanks to everyone for such a great platform - I have only been working on it for a few months but its great fun and so flexible and easy to use!

    I guess this is a simple (probably a NOOB) question but this has me stumped!

    I am running a teensy3.2 with Arduino 1.8.13 and TeensyDuino 1.53, I am working on a project to connect to GCP IOT Core, and was producing JWT tokens successfully in my demo code using current time from a network time server.

    I now want to set the internal Teensy clock with that value from the network time server which is in the format unsigned long int (date from 1970) to ensure I can re-create tokens as required without multiple time server calls.

    However as soon as I started using the time functions I started to notice that time_t is defined as a long int not an unsigned long int, which is causing all kinds of problems.

    Sample code to show the type:
    Code:
    typedef unsigned long time_t;
    
    void setup() {
      // put your setup code here, to run once:
    
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    
    }
    Compile and I get:
    Code:
    sketch_mar05a:1: error: conflicting declaration 'typedef long unsigned int time_t'
     typedef unsigned long time_t;
                           ^
    In file included from /Users/timteece/Work/work/Coded Cognition/Clients/Audio Wings/MK1DemoUnit/Teensyduino.app/Contents/Java/hardware/tools/arm/arm-none-eabi/include/sys/select.h:26:0,
                     from /Users/timteece/Work/work/Coded Cognition/Clients/Audio Wings/MK1DemoUnit/Teensyduino.app/Contents/Java/hardware/tools/arm/arm-none-eabi/include/sys/types.h:68,
                     from /Users/timteece/Work/work/Coded Cognition/Clients/Audio Wings/MK1DemoUnit/Teensyduino.app/Contents/Java/hardware/tools/arm/arm-none-eabi/include/stdio.h:61,
                     from /Users/timteece/Work/work/Coded Cognition/Clients/Audio Wings/MK1DemoUnit/Teensyduino.app/Contents/Java/hardware/teensy/avr/cores/teensy3/Print.h:35,
                     from /Users/timteece/Work/work/Coded Cognition/Clients/Audio Wings/MK1DemoUnit/Teensyduino.app/Contents/Java/hardware/teensy/avr/cores/teensy3/Stream.h:24,
                     from /Users/timteece/Work/work/Coded Cognition/Clients/Audio Wings/MK1DemoUnit/Teensyduino.app/Contents/Java/hardware/teensy/avr/cores/teensy3/HardwareSerial.h:252,
                     from /Users/timteece/Work/work/Coded Cognition/Clients/Audio Wings/MK1DemoUnit/Teensyduino.app/Contents/Java/hardware/teensy/avr/cores/teensy3/WProgram.h:46,
                     from /var/folders/tt/jlz51f3j4j1cvw7b_kvkf0gr0000gp/T/arduino_build_176998/pch/Arduino.h:6:
    /Users/timteece/Work/work/Coded Cognition/Clients/Audio Wings/MK1DemoUnit/Teensyduino.app/Contents/Java/hardware/tools/arm/arm-none-eabi/include/sys/_timeval.h:40:18: note: previous declaration as 'typedef long int time_t'
     typedef _TIME_T_ time_t;
                      ^
    conflicting declaration 'typedef long unsigned int time_t'
    Now I was clearly expecting the error, but is it right on this platform Teensy3.2 that time_t should be signed and not unsigned?

    Hope this is an easy one to clear up - have google widely but no joy so far!

    Many thanks

    Tim

  2. #2
    Senior Member
    Join Date
    Aug 2013
    Location
    Gothenburg, Sweden
    Posts
    412
    Time libraries are supposed to be able to handle times before 1970, so the times can be represented as negative numbers.

    If you are sure that your use of time is after 1970 you can convert/copy/typecast time values into unsigned variables as needed.

  3. #3
    Junior Member
    Join Date
    Mar 2021
    Posts
    2

    Quote Originally Posted by mlu View Post
    Time libraries are supposed to be able to handle times before 1970, so the times can be represented as negative numbers.

    If you are sure that your use of time is after 1970 you can convert/copy/typecast time values into unsigned variables as needed.
    Hi MLU,

    Thanks for the quick response, and thanks for confirming!

    Now I have stopped looking for the reasons in the underlying libraries, and focused on my own code again (lol) I spotted that time servers (NTP) give their time since 1900 not 1970 and so I was 70 years out! This was then obviously causing type conversion issues as both unsigned long and time_t are 32 bits, but we should be good for a few years yet!

    Thanks again for your help, much appreciated!

    Tim

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •