pimoroni-pico icon indicating copy to clipboard operation
pimoroni-pico copied to clipboard

Word Clock on Inky Frame - UTC only!

Open wholepunch opened this issue 2 years ago • 4 comments
trafficstars

Hey. I cannot, for the life of me, work out how to allow for timezone in the Python word clock. I've tried a few bodges, but nothing sticks for long and the time drifts out again. I'm in GB (UTC +1 atm) - how the heck do I reflect that in the code? Thanks!

wholepunch avatar Jul 09 '23 12:07 wholepunch

MicroPython has no timezone support so you've got to add an hour to the time as it's ready back from the RTC.

Probably somewhere around here:

https://github.com/pimoroni/pimoroni-pico/blob/25237c54ce465279134fe429e4c11ffe5cb01cfd/micropython/examples/inky_frame/inkylauncher/word_clock.py#L42C31-L42C31

And dealing with rollover is a nightmare, so you might need to convert into a UNIX timecode and add an hour in seconds, which is sort of what's happening here:

https://github.com/ahnlak/badger2040/blob/bc1a666f4042c1018fe40faaf94ab2f4e6086dba/firmware/PIMORONI_BADGER2040W/lib/badger2040.py#L145-L151

Gadgetoid avatar Jul 10 '23 09:07 Gadgetoid

Ok, thanks, iI'll have a punt at inserting the additional hour where you suggest.

wholepunch avatar Jul 10 '23 15:07 wholepunch

Think I got it.

hours = hours + 1

here https://github.com/pimoroni/pimoroni-pico/blob/25237c54ce465279134fe429e4c11ffe5cb01cfd/micropython/examples/inky_frame/inkylauncher/word_clock.py#L19

Obv I'll need to update it for the clock changes, but it's easier than trying to get epoch time working (for me, anyway..)

Thanks!

wholepunch avatar Jul 10 '23 15:07 wholepunch

This is how i'm adding a utc offset:

import utime

def get_time(utc_offset: int):
  # utc_offset is in seconds
  utime.localtime(utime.mktime(utime.localtime()) + utc_offset)

However just setting a static offset doesn't work because of daylight savings. So you could use https://ip-api.com/ to lookup the offset based on your IP.

import urequest

# limited to 45 / minute but really you should only do it once a day
def get_utc_offset():
  r = urequest.get('http://ip-api.com/json/?fields=offset')
  return r.json()['offset']

North101 avatar Jul 12 '23 18:07 North101

Issue appears to have been resolved. Please reopen if required :)

thirdr avatar May 30 '24 13:05 thirdr