Bug in Timelib timesyncing?

Epyon

Well-known member
I have a Teensy 3.2, a Adafruit FONA GPRS modem, SD card and a battery connected to the VBAT pin. I use this for datalogging, and I need the logs to be timestamped, so I use the Timelib.

On boot, the Teensy checks if the internal RTC is set and prints the time (using now()), initialises the FONA and then queries the network for the time. The network gives local time (CET) and the offset (in quarter hours) to UTC. I need UTC to avoid problems with daylight savings, different timezones etc. So I calculate UTC time and then set the time (setTime()). I then set the RTC with the time (Teensy3Clock.set(now()). UTC is one hour behind my local time.

On a reboot I would expect the RTC to be set to UTC, but most of the times it is set as local time. Sometimes it is set as UTC time. This is problematic, because after a reboot the unit timestamps the logs in local time until a network time fix (and UTC calculation) can be made.

I'll just post the relevant time snippets of my code:
Setup:
Code:
  setSyncProvider(getTeensy3Time);
  //Check to see if the RTC has been set before
  if (timeStatus()!= timeSet) {
    Serial.println("RTC not set");
  }
  else {
    Serial.print("Time set by RTC: ");
    printTime();
    Serial.println("");
  }

My checkTime function:
Code:
  setTime(houri, minutei, secondi, dayi, monthi, yeari);
  Serial.print(F("Received timestamp: "));
  Serial.print(dayi);
  Serial.print('/');
  Serial.print(monthi);
  Serial.print('/');
  Serial.print(yeari);
  Serial.print(' ');
  Serial.print(houri);
  Serial.print(':');
  Serial.print(minutei);
  Serial.print(':');
  Serial.print(secondi);
  Serial.print(':');
  Serial.println("");  
  Serial.print("Adjusted for UTC time: ");
  if(offsetplus){
    utc = now() - offseti*900; //offset is the amount of quarters of an hour the timezone is offset to UTC
  }
  else {
    utc = now() + offseti*900;
  }
  setTime(utc);
  printTime();
  Teensy3Clock.set(now());

This is the serial output I'm getting (at 00:15 local time, 23:15 UTC)
Code:
Booting...Time set by RTC: 1480637669 2016/12/02 00:14:29 
1480637689 2016/12/02 00:14:49 CEL: Getting local time from cellular network: "16/12/02,00:15:56+04"
Received timestamp: 2/12/16 0:15:56:
Adjusted for UTC time: 1480634156 2016/12/01 23:15:56
I would think the RTC is now set with UTC time. However after a reboot:
Code:
Booting...Time set by RTC: 1480637669 2016/12/02 00:14:29 
1480637689 2016/12/02 00:14:49 CEL: Getting local time from cellular network: "16/12/02,00:18:09+04"
Received timestamp: 2/12/16 0:18:9:
Adjusted for UTC time: 1480634289 2016/12/01 23:18:09

Am I missing something here to set the RTC correctly? It always reverts to local time, with some offset sometimes :confused: .
 
Your posted code is very incomplete. You should post something that compiles and runs - no need for the GPRS stuff, a dummy time value could be hard-coded.

In any case, "now()" calls the sync provider that you set via "setSyncProvider(getTeensy3Time);". Calling "Teensy3Clock.set(now());" is likely your problem; change that to "Teensy3Clock.set(utc)".
 
The problem appeared to be hardware, not the library or my code. When fully inserting the coin cell into its holder, it made an electrical connection between the GND pad and a trace running below the holder. I presumed the soldermask would prevent this, but alas. With a piece of paper in between, the RTC is working as intended :) .
 
Back
Top