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.
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.
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().
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
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
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?
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.
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.