Skip to content

Add timezone and DST support#20

Draft
samcorky wants to merge 8 commits intosjefferson99:mainfrom
samcorky:timezones
Draft

Add timezone and DST support#20
samcorky wants to merge 8 commits intosjefferson99:mainfrom
samcorky:timezones

Conversation

@samcorky
Copy link
Copy Markdown

This PR adds timezone support so the clock can display local time based on a configured IANA timezone (or default to UTC when unset/unknown).

What’s included

TIMEZONE config option (IANA name; None = UTC)
Timezone module that maps IANA → POSIX TZ and converts epoch/time tuples into localized time (including DST rules where applicable)
Clock display updates now use localized time/date values

If the configured timezone isn’t recognised, the clock falls back to UTC.

Comment thread src/lib/timezone/__init__.py
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a lightweight timezone layer to the Pico Clock so displayed time/date can be localized from a configured IANA timezone (with DST where applicable), falling back to UTC when unknown.

Changes:

  • Introduces a timezone package with an IANA→POSIX mapping and POSIX TZ conversion utilities.
  • Adds a TIMEZONE config option and wires localization into the clock display update loop.
  • Expands .gitignore to exclude common local/dev artifacts.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/lib/timezone/localPZtime.py POSIX TZ parsing/conversion implementation used to compute localized time/DST.
src/lib/timezone/db.py Embedded IANA→POSIX timezone mapping plus lookup helper.
src/lib/timezone/__init__.py Timezone wrapper that selects a POSIX string and converts tuples/epochs to local time.
src/lib/clock.py Uses Timezone to localize TimeSource output before updating displays.
src/config.py Adds TIMEZONE configuration value.
.gitignore Ignores venv/IDE and Python lock/config files.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +4 to +7

:author: Roberto Bellingeri
:copyright: Copyright 2023 - NetGuru
:license: GPL
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file header declares :license: GPL and includes a third‑party copyright notice, while the repository root LICENSE is MIT. Please confirm the provenance and licensing is compatible with this repo, or replace/rewrite this module under an MIT-compatible license before merging.

Suggested change
:author: Roberto Bellingeri
:copyright: Copyright 2023 - NetGuru
:license: GPL

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samcorky my code is MIT so this will need resolving,

Comment on lines +107 to +111
stx += f"{(tx[9] // 3600):+03d}"

mins = abs(tx[9]) % 3600
if (mins != 0):
stx += ":" + f"{(mins // 60):02d}"
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tziso() formats the UTC offset hours using floor division (tx[9] // 3600). For negative offsets with minutes (e.g. UTC−03:30), floor division rounds down and produces an incorrect hour (−04), yielding the wrong ISO8601 offset string. Compute hours/minutes using absolute values + sign (or truncation toward zero) to avoid this.

Suggested change
stx += f"{(tx[9] // 3600):+03d}"
mins = abs(tx[9]) % 3600
if (mins != 0):
stx += ":" + f"{(mins // 60):02d}"
# tx[9] is the UTC offset in seconds; compute sign, hours, and minutes
total_seconds = abs(tx[9])
hours = total_seconds // 3600
mins = (total_seconds % 3600) // 60
sign = 1 if tx[9] >= 0 else -1
stx += f"{sign * hours:+03d}"
if (mins != 0):
stx += ":" + f"{mins:02d}"

Copilot uses AI. Check for mistakes.
Comment thread src/config.py
Comment on lines +19 to +21
## Timezone in IANA format
TIMEZONE = "Etc/UTC"

Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default TIMEZONE value ("ETC/UTC") does not match the IANA casing used by the timezone database keys (e.g. "Etc/UTC"). This will always trigger the "not found" fallback path and log a warning on every boot with the default config. Use the correct canonical IANA ID or normalize input before lookup.

Copilot uses AI. Check for mistakes.
Comment thread src/lib/clock.py Outdated
Comment on lines +1 to +2
import time

Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import time is added but not used anywhere in this file; please remove it to avoid dead imports.

Suggested change
import time

Copilot uses AI. Check for mistakes.
Comment thread src/lib/timezone/__init__.py Outdated
import time

from lib.timezone import localPZtime
from lib.timezone.db import TIMEZONE_TO_POSIX, timezone_to_posix
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIMEZONE_TO_POSIX is imported here but never used; please remove the unused import to keep the module minimal (especially important on MicroPython targets).

Suggested change
from lib.timezone.db import TIMEZONE_TO_POSIX, timezone_to_posix
from lib.timezone.db import timezone_to_posix

Copilot uses AI. Check for mistakes.
Comment thread src/lib/timezone/__init__.py Outdated
Comment on lines +42 to +51
def epoch_to_local_time_iso8601(self, time_tuple: tuple) -> str:
"""
Convert time tuple to ISO8601 formatted local time string
"""
# Pad time_tuple with 0s up to 8 elements
padded_time_tuple = time_tuple + (0,) * (8 - len(time_tuple))

epoch = time.mktime(padded_time_tuple)
local_time = localPZtime.tziso(epoch, self.posix_str)
return local_time
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method name epoch_to_local_time_iso8601 is misleading: it accepts a time tuple (not an epoch) and internally calls time.mktime to compute an epoch. Rename it (e.g. time_tuple_to_local_time_iso8601) or change the parameter to an epoch seconds value to match the name/docstring.

Copilot uses AI. Check for mistakes.
@samcorky samcorky marked this pull request as draft February 18, 2026 21:26
@samcorky
Copy link
Copy Markdown
Author

The https://github.com/samcorky/utimezone.git repository is almost ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants