How can i make Time set the correct time zone?

Hi, i have this:

void setup() {
Time.zone(1);
}

void loop() {
Serial.println(Time.format(Time.local(), “%H:%M”));
}

My timezone is UTC+01:00 (Norway), and from reading the documentation on Time i would believe setting zone to 1 would give me the correct time? But it displays one hour behind. Right now it displays 18:27, while the real time is 19:27.

Any clue how i can fix this?

@14881122, did you look at the Time docs?

https://docs.particle.io/reference/firmware/photon/#local-

@peekay123 Yes, i did, and local gives me one hour wrong :frowning:

Daylight savings settings?

DST is a way to enable or disable DST but it doesn’t actually calculate when to apply DST.

@14881122, are you taking DST into account?

Apparantly, DST only starts on March 25 in Norway :stuck_out_tongue_closed_eyes:

And would we not rather see time.local() lag behind one hour, rather than an hour ahead? Unless you mean, his code already is accounting for DST, in which case yeah, that would create issues

1 Like

@Vitesze, you are correct regarding March 25. @14881122, where/how are you setting Time.zone(1) in your code?

@Vitesze @peekay123

Umm, awkward. I wrote wrong in my initial post. It displays one hour behind. Now its 19:25 here, and Time displays in the serial monitor 18:25. So, yeah… I have tried setting Time.zone(1) in setup(), and i have also tried calling setDSTOffset() with all possible values.

@14881122, @peekay123 Either I’m not understanding something about Time.local(), or there’s a bug in it. I tried this code,

void setup() {
    Time.zone(0);
    Serial.begin();
}

void loop() {
    Serial.printlnf("LocalTime is: %s", (const char*)Time.format(Time.local(), "%H:%M"));
    Serial.printlnf("Time is: %d:%d)", Time.hour(), Time.minute());
    delay(10000);
}

This gives me:

LocalTime is: 20:37
Time is: 20:37

Which is correct. But if I pass 1 to Time.zone() instead of 0, I get this:

LocalTime is: 22:37
Time is: 21:37

So changing the time zone by 1 hour caused the output of Time.local() to change by 2 hours, but Time.hour() only changes by one, as it should.

1 Like

I believe (but have not verified) that both Time.local() and Time.format() are add/subtracting off the time zone offset.

That seems to be correct. Using Time.zone(1), if I print out the numerical values of Time.now() and Time.local() they are different by 3600, which is correct.

2 Likes

So the moral of the story is to call Time.format() only with Time.now() and only use Time.local() when you are decoding it yourself.

I keep repeating this all over the place when this question comes up.
If you want to use Time.format() for the current time don’t provide the time parameter to it.
There is a dedicated overload that only takes the format string and implicitly will apply the correct time which is Time.now() not Time.local().

Some similar questions answered before:
https://community.particle.io/search?q=time.local%20wrong

4 Likes

I hadn’t paid attention to these since I don’t use Time.local() or Time.format() in my own projects . The documentation on this seems confusing to me, with no mention of the fact that you can call Time.format() with no arguments.

That’s a valid point - this is a “terrible” oversight.
I’ll add a not for that next time tamper with the docs :wink:

Till then, this is where I usually look when I want to know how things behave really

So you can have

  Time.format(Time.now(), strFormat); // fully qualified (e.g. current time w/ custom format)
  Time.format(strFormat);             // current time with custom format
  Time.format(someTime);              // custom time with preset format
  Time.format();                      // current time with preset format
2 Likes

Thanks alot everyone. I got the correct time now. But one more silly question from me - Time gets the time from the cloud right? So if i were to hook it up with an external power source somewhere with internet connection it would still be able to get the time from Time? It doesent get the time from my computer somehow? :stuck_out_tongue:

The device has a builtin RTC (real time clock) which is synchronised to the cloud time when the device connects. From then on the RTC keps incrementing the time independently till the next time connection will be re-established or your code calls Particle.syncTime().
If your code doesn’t disconnect/reconnect or call Particle.syncTime() regularly (e.g. every 24 hours) the time will slowly drift away from the actual global time.

3 Likes

Alright, got it. Thanks alot!

I have now updated the docs.
Hope this improves clarity.

2 Likes

Hey @ScruffR,

Since I’m in india, my time zone is +5:30. I have used the Time zone like Time.zone(+5.30); and Time.format(Time.now(), “%I: %M %p”)); but am getting 12 minutes difference which is 12 minutes less than actual time,can you please help me in this regard.:smile: