Particle Core and Time Library

So, based on the documents description of the Time Library

I expect to see UTC time printed out when I pass the now() argument and local time when I pass the local() argument… but alas no:

now() is moved 5 hours and local() is off by 10!!

Is this a Core thing?

code:


void setup(void)
{
  Serial.begin(115200);
  Particle.syncTime();
  Time.zone(-5);
}
void loop(void)
{
  uint32_t lastUpdateMillis = 0;
  if(millis() - lastUpdateMillis > 1000)
  {
    Serial.print("NoArg:\t");
    Serial.println(Time.timeStr());
    Serial.print("now:\t");
    Serial.println(Time.timeStr(Time.now()));
    Serial.print("local:\t");
    Serial.println(Time.timeStr(Time.local()));
    Serial.println();
    lastUpdateMillis = millis();
  }
}

when I comment out the Time.zone(-5); line, both now() and local() show UTC, as expected…

what am I misunderstanding? I didn’t have a Photon to test…

Have you seen this?

Time.now() and Time.local() only return a numer of seconds without any further information about when the counting started.
And Time.timeStr() just takes a number of seconds (without caring about the way how you got that number) and formats that according to the data stored in the Time object (especially the zone).

A over-simplified example:

int zone = -5;
int now() { return millis()/1000; }
int local() { return now() + zone*3600; }
String timeStr(int sec) {  return ("The localized time is " + String(sec + zone*3600) + " seconds after bootup"); } 

If you call that the way you did above, you'll see similar behaviour (local() will show twice the zone offset).
And when you set zone = 0, you'll get the same result for local() and now()

1 Like

I see, and I did look for the answer! I appreciate your help…

1 Like