Teensy 3.2 + RTC: time_t number is negative (?!)

Status
Not open for further replies.

Kaibz

Member
Using these libraries:TimeLib.h, Time.h and DS1307RTC.h

As described in the time library in PJRC time library page: https://www.pjrc.com/teensy/td_libs_Time.html a time_t number will return the number of seconds elapsed since 1970.

But i try to output time as a time_t number:
Code:
time_t timeNow = now();
Serial.print("time now as a time_t number is: ");
Serial.print(timeNow);
Serial.println();

These are the outputs i get:

Code:
time now as a time_t number is: -1271949714
time now as a time_t number is: -1271949703
time now as a time_t number is: -1271949693
time now as a time_t number is: -1271949682

I did checked that i do get the correct time when i try to output hour() or minute() and i get the correct time of the day.

I understand it's still usable but, is there any way to get time as a time_t positive number ?
 
Using these libraries:TimeLib.h, Time.h and DS1307RTC.h

As described in the time library in PJRC time library page: https://www.pjrc.com/teensy/td_libs_Time.html a time_t number will return the number of seconds elapsed since 1970.

But i try to output time as a time_t number:
Code:
time_t timeNow = now();
Serial.print("time now as a time_t number is: ");
Serial.print(timeNow);
Serial.println();

These are the outputs i get:

Code:
time now as a time_t number is: -1271949714
time now as a time_t number is: -1271949703
time now as a time_t number is: -1271949693
time now as a time_t number is: -1271949682

I did checked that i do get the correct time when i try to output hour() or minute() and i get the correct time of the day.

I understand it's still usable but, is there any way to get time as a time_t positive number ?

I know, time_t is unsigned, but you have to tell the Serial.print that the 32 bit value is unsigned and not signed. The function only gets the 32 bits.
 
I'm not sure i get it, how do i "tell" the Serial.print that the 32 bits value is unsigned?
I mean all i managed to output a postitive number with Serial.print was to use a cast like this but i'm not sure if this is what you meant ?:

Code:
time_t timeNow = now();
Serial.print("time now as a time_t number is: ");
Serial.print((uint32_t) timeNow);
// This outputed: 3023020005

The other thing that is bothering me is, if time_t is unsigned, it means it can't be negative, then why did it not pass my "signed evaluation" ?:

Code:
time_t timeNow = now();
if (timeNow < 0) {
	Serial.println("timeNow is negative");
}
else {
	Serial.println("timeNow is positive");
}

This outputed: timeNow is negative (!)

I suppose one way to do it would be to simply define timeNow as a uint32_t instead of time_t but i just want to make sure i am not missing something important here.
 
If the clock is set to today's date, the 32 bit signed output should not be negative. That date is still 31 years away!

Here's the output I see when I run on a Teensy 3.6.

RTC has set the system time
time_t = 1508303429 5:10:29 18 10 2017
time_t = 1508303430 5:10:30 18 10 2017
time_t = 1508303431 5:10:31 18 10 2017
time_t = 1508303432 5:10:32 18 10 2017
time_t = 1508303433 5:10:33 18 10 2017
time_t = 1508303434 5:10:34 18 10 2017
time_t = 1508303435 5:10:35 18 10 2017
time_t = 1508303436 5:10:36 18 10 2017

Here's the full code (just a slight modification of the TimeTeensy3 example):

Code:
#include <TimeLib.h>

void setup()  {
  // set the Time library to use Teensy 3.0's RTC to keep time
  setSyncProvider(getTeensy3Time);

  Serial.begin(115200);
  while (!Serial);  // Wait for Arduino Serial Monitor to open
  delay(100);
  if (timeStatus()!= timeSet) {
    Serial.println("Unable to sync with the RTC");
  } else {
    Serial.println("RTC has set the system time");
  }
}

void loop() {
  if (Serial.available()) {
    time_t t = processSyncMessage();
    if (t != 0) {
      Teensy3Clock.set(t); // set the RTC
      setTime(t);
    }
  }
  digitalClockDisplay();  
  delay(1000);
}

void digitalClockDisplay() {
  // digital clock display of the time
  time_t t = now();
  Serial.print("time_t = ");
  Serial.print(t);
  Serial.print("   ");
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
}

time_t getTeensy3Time()
{
  return Teensy3Clock.get();
}

/*  code to process time sync messages from the serial port   */
#define TIME_HEADER  "T"   // Header tag for serial time sync message

unsigned long processSyncMessage() {
  unsigned long pctime = 0L;
  const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013 

  if(Serial.find(TIME_HEADER)) {
     pctime = Serial.parseInt();
     return pctime;
     if( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013)
       pctime = 0L; // return 0 to indicate that the time is not valid
     }
  }
  return pctime;
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}
 
Thank you Paul.

As the hour, and minute were accurate i thought RTC was set correctly but, i realized the date actually was....19/10/2065

So i just went back from the future and now it'is working accordingly.

Cheers.
 
Status
Not open for further replies.
Back
Top