setting local time on pico w #9545
-
As far i know the way to set the time on a pico w is using ntptime module, this works fine but i want to set my time zone and i don´t know how to do it. Is there any "official" way for that? |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 13 replies
-
I think it makes sense for ntptime to set the time in UTC. The issue is that micropython itself has no built-in notion of timezones or the current timezone, so In micropython-lib we have a (micro) datetime package. I have not looked into it in detail, but I believe that as long as your app code knows the current timezone, then you can construct offset times. e.g. assuming the clock has been set by ntptime my_timezone = timezone(timedelta(hours=10))
current_time = datetime.datetime.now(my_timezone) https://github.com/micropython/micropython-lib/tree/master/python-stdlib/datetime (You can install this via mip if you're using the latest |
Beta Was this translation helpful? Give feedback.
-
This was my solution:
-4 is the default timezone, easily change in source or set on the fly via |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Yet another option is to get the time from http://worldtimeapi.org which can return the time in json format for any location including the daylight saving time if its in operation. |
Beta Was this translation helpful? Give feedback.
-
I don't know if this helps at all, but I simply took the tuple from rtc.datetime() and converted it to a list called tlist, changed the hour element in the list and initalised the rtc with the list using rtc.init(tlist). This works fine on the esp32. As the 2040 has an RTC I would assume it would work on it too. RTC imported as: ntptime.settime() then use rtc.datetime() as the time source. |
Beta Was this translation helpful? Give feedback.
-
No. It's in it's a very crude form of an immediate solution to a problem. Taking the time accross the date line in the change, and changing/not changine the time based on the date are a couple of the things yet to be addressed. |
Beta Was this translation helpful? Give feedback.
-
I've started a Discussion here to talk about the overall problem and possible ways forward for it: |
Beta Was this translation helpful? Give feedback.
-
''' import time def getNow(UTC_OFFSET=+5.5): if name == "main": """ copy whole content to file and save as getNow.py Usage (assuming a network connection):
read current datetime from RTCIndex Attribute Values """ |
Beta Was this translation helpful? Give feedback.
-
Thank you for this solution. |
Beta Was this translation helpful? Give feedback.
I think it makes sense for ntptime to set the time in UTC. The issue is that micropython itself has no built-in notion of timezones or the current timezone, so
time.localtime
andtime.gmtime
will always return the same thing.In micropython-lib we have a (micro) datetime package. I have not looked into it in detail, but I believe that as long as your app code knows the current timezone, then you can construct offset times. e.g. assuming the clock has been set by ntptime
https://github.com/micropython/micropython-lib/tree/master/python-stdlib/datetime
(You can install this via mip if you're using …