# Galactic Unicorn modified clock example

**URL:** <https://forums.pimoroni.com/t/galactic-unicorn-modified-clock-example/20738>\
**Category:** Projects\
**Created:** [November 17, 2022, 3:22pm UTC](https://forums.pimoroni.com/t/galactic-unicorn-modified-clock-example/20738 "2022-11-17T15:22:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulsk](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/paulsk/32/7590_2.png) [@paulsk](https://forums.pimoroni.com/u/paulsk)\
**Post date:** [November 17, 2022, 3:22pm UTC](https://forums.pimoroni.com/t/galactic-unicorn-modified-clock-example/20738/1 "2022-11-17T15:22:52Z")

</div>

I published on Github a repo with a modified clock example for the Galactic Unicorn.  
[clock modified](https://github.com/PaulskPt/galactic_unicorn_clock_mod)

The original version is at:  
[clock original](https://github.com/pimoroni/pimoroni-pico/blob/main/micropython/examples/galactic_unicorn/clock.py)

---

<div class="post-metadata">

**Author:** ![iohans](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/iohans/32/9138_2.png) [@iohans](https://forums.pimoroni.com/u/iohans)\
**Post date:** [November 17, 2022, 4:08pm UTC](https://forums.pimoroni.com/t/galactic-unicorn-modified-clock-example/20738/2 "2022-11-17T16:08:43Z")

</div>

Cool. What does it look like when it is all together and programmed?

---

<div class="post-metadata">

**Author:** ![paulsk](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/paulsk/32/7590_2.png) [@paulsk](https://forums.pimoroni.com/u/paulsk)\
**Post date:** [November 17, 2022, 4:30pm UTC](https://forums.pimoroni.com/t/galactic-unicorn-modified-clock-example/20738/3 "2022-11-17T16:30:47Z")

</div>

Hi johans. Thank you for your reaction although I don’t understand your question.  
My ‘clock modified’ and the ‘clock original’ (by Pimoroni) are similar but different scripts.  
I created my ‘mod’ because I wanted to do something different regarding the clock.  
Don’t have the intention to put the original (Pimoroni’s) and my examples together if that is what you trying to suggest.  
Regarding my ‘clock modified’. It is ‘programmed’ in the sense of being transferred to the flash memory of the Galactic Unicorn (in fact the Raspberry Pi Pico W). It ran on my Galactic Unicorn board for more than 24 hours without error. During the night you can even dim the brilliance of the LEDs to zero and in the morning push on ‘LUX +’ to see the clock again. My example is programmed to connect to internet at an interval of 10 minutes. At each interval an updated date & time is fetched from an NTP time service. Then the built-in realtime clock (RTC) is updated (synchronized) with that updated date & time. I did this because I have the experience that the built-in RTC (of microcontrollers) sometimes tend to run faster than the real world time.

---

<div class="post-metadata">

**Author:** ![bablokb](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@bablokb](https://forums.pimoroni.com/u/bablokb)\
**Post date:** [November 18, 2022, 8:17am UTC](https://forums.pimoroni.com/t/galactic-unicorn-modified-clock-example/20738/4 "2022-11-18T08:17:15Z")

</div>

> [@paulsk](#):
>
> At each interval an updated date & time is fetched from an NTP time service. Then the built-in realtime clock (RTC) is updated (synchronized) with that updated date & time.

I can see in your code that you use buttons to adapt the UTC-time to local-time. This works, but it would be so much simpler if you would query the local-time directly, e.g. from [worldtimeapi.org](http://worldtimeapi.org). You don’t even have to provide a time-zone for that, because [worldtimeapi.org](http://worldtimeapi.org) guesses your timezone from your IP. Added bonus: no more trouble with daylight-saving-time.

---

<div class="post-metadata">

**Author:** ![paulsk](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/paulsk/32/7590_2.png) [@paulsk](https://forums.pimoroni.com/u/paulsk)\
**Post date:** [November 18, 2022, 11:29am UTC](https://forums.pimoroni.com/t/galactic-unicorn-modified-clock-example/20738/5 "2022-11-18T11:29:55Z")

</div>

Thank you for your reply. Nice you looked into the code. The programming of the buttons was just a kinda ‘finger exercise’ so to speak. Thank you for your tip about [worldtimeapi.org](http://worldtimeapi.org). I have several other projects with NTP and RTC’s however never came across [worldtimeapi.org](http://worldtimeapi.org). Great that is takes the ‘burden’ of the DST problem. Makes the code smaller and less prone to bugs. I am going to see and try to implement it.

---

<div class="post-metadata">

**Author:** ![bablokb](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@bablokb](https://forums.pimoroni.com/u/bablokb)\
**Post date:** [November 18, 2022, 11:36am UTC](https://forums.pimoroni.com/t/galactic-unicorn-modified-clock-example/20738/6 "2022-11-18T11:36:18Z")

</div>

Just a little warning: [worldtimeapi.org](http://worldtimeapi.org) is free, but unreliable and has rate-limiting. For normal use, this is no problem, because once every ten minutes is no problem and if an update fails, the next will succeed. But during development/testing, the rate-limiting might hurt.

You will probably find many examples in the internet for dealing with [worldtimeapi.org](http://worldtimeapi.org). I have an example in CircuitPython, but this is not very CP specific:

```auto
  def _get_remotetime(self):
    """ query time from time-server """

    response = self._wifi.get(settings.TIMEAPI_URL).json()

    current_time = response["datetime"]
    the_date, the_time = current_time.split("T")
    year, month, mday = [int(x) for x in the_date.split("-")]
    the_time = the_time.split(".")[0]
    hours, minutes, seconds = [int(x) for x in the_time.split(":")]

    year_day = int(response["day_of_year"])
    week_day = int(response["day_of_week"])
    week_day = 6 if week_day == 0 else week_day-1
    is_dst = int(response["dst"])

    return time.struct_time(
      (year, month, mday, hours, minutes, seconds, week_day, year_day, is_dst))

```

---

<div class="post-metadata">

**Author:** ![paulsk](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/paulsk/32/7590_2.png) [@paulsk](https://forums.pimoroni.com/u/paulsk)\
**Post date:** [November 18, 2022, 11:59am UTC](https://forums.pimoroni.com/t/galactic-unicorn-modified-clock-example/20738/7 "2022-11-18T11:59:05Z")

</div>

Ah great, thank you for this additional info and the code example Bernard!  
I first have to go out for my weekly groceries shopping. Later today I’ll dive into it.  
Thanks!

---

<div class="post-metadata">

**Author:** ![paulsk](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/paulsk/32/7590_2.png) [@paulsk](https://forums.pimoroni.com/u/paulsk)\
**Post date:** [November 18, 2022, 7:23pm UTC](https://forums.pimoroni.com/t/galactic-unicorn-modified-clock-example/20738/8 "2022-11-18T19:23:45Z")

</div>

I removed function adjust\_utc\_offset() which was a leftover from the original clock.py example from Pimoroni. The volume up/down buttons I already was using for that purpose instead of changing the utc\_offset. The utc\_offset can now be set at key TZ\_OFFSET in the file ‘clock\_mod\_secrets.py’.

I added key NTP\_SERVER to file ‘clock\_mod\_secrets.py’,  
to be able to choose a favorite NTP server. Then in function sync\_time() there are now:

```
        ntptime.host = ntp_server
        ntptime.settime()

```
