Skip to content

Commit

Permalink
report times in local tz. hopefully fixes #17
Browse files Browse the repository at this point in the history
  • Loading branch information
time4tea committed Feb 25, 2022
1 parent 903579f commit 6d57083
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gopro_overlay/fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def fake_timeseries(length: datetime.timedelta = datetime.timedelta(seconds=20),
temp = Random1D(27, rng=rng)

ts = Timeseries()
current_dt = datetime.datetime.fromtimestamp(0)
current_dt = datetime.datetime.fromtimestamp(0, tz=datetime.timezone.utc)
end_dt = current_dt + length

while current_dt <= end_dt:
Expand Down
18 changes: 12 additions & 6 deletions gopro_overlay/layout_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
import os.path
import sys
import xml.etree.ElementTree as ET
from typing import Callable

from gopro_overlay import layouts
from gopro_overlay.layout_components import date_and_time, gps_info, moving_map, journey_map, big_mph, gradient_chart, \
text, metric
from gopro_overlay.point import Coordinate
from gopro_overlay.timeseries import Entry
from gopro_overlay.units import units
from gopro_overlay.widgets import simple_icon, Translate, Composite

Expand Down Expand Up @@ -189,20 +191,24 @@ def create_icon(element, **kwargs):
)


def date_formatter_from(element, entry):
def date_formatter_from(entry: Callable[[], Entry], format_string, truncate=0, tz=None) -> Callable[[], str]:
if truncate > 0:
return lambda: entry().dt.astimezone(tz=tz).strftime(format_string)[:-truncate]
else:
return lambda: entry().dt.astimezone(tz=tz).strftime(format_string)


def date_formatter_from_element(element, entry: Callable[[], Entry]):
format_string = attrib(element, "format")
truncate = iattrib(element, "truncate", d=0)

if truncate > 0:
return lambda: entry().dt.strftime(format_string)[:-truncate]
else:
return lambda: entry().dt.strftime(format_string)
return date_formatter_from(entry, format_string, truncate)


def create_datetime(element, entry, font, **kwargs):
return text(
at=at(element),
value=date_formatter_from(element, entry),
value=date_formatter_from_element(element, entry),
font=font(iattrib(element, "size", d=16)),
align=attrib(element, "align", d="left"),
cache=battrib(element, "cache", d=True),
Expand Down
16 changes: 15 additions & 1 deletion tests/test_layout_xml.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from gopro_overlay.layout_xml import metric_accessor_from
import datetime

from gopro_overlay.layout_xml import metric_accessor_from, date_formatter_from
from gopro_overlay.timeseries import Entry
from gopro_overlay.units import units
from tests.test_timeseries import datetime_of
Expand All @@ -18,3 +20,15 @@ def test_metric_accessor_speed_fallback():
entry = Entry(datetime_of(0), cspeed=cspeed)

assert metric_accessor_from("speed")(entry) == cspeed


def test_date_formatter():
entry = lambda: Entry(datetime_of(1644606742))

utc = datetime.timezone.utc
# timezone doesn't really want to be called externally..., and don't want to depend on pytz
sort_of_pst = datetime.timezone.__new__(datetime.timezone, datetime.timedelta(hours=-8), "Bodge/PST")

# Will just have to accept that calling with tz=None will do local tz, as its cached in datetime.py
assert date_formatter_from(entry, "%Y/%m/%d %H:%M:%S.%f", tz=utc)() == "2022/02/11 19:12:22.000000"
assert date_formatter_from(entry, "%Y/%m/%d %H:%M:%S.%f", tz=sort_of_pst)() == "2022/02/11 11:12:22.000000"
2 changes: 1 addition & 1 deletion tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_delta_processing():


def datetime_of(i):
return datetime.datetime.fromtimestamp(i)
return datetime.datetime.fromtimestamp(i, tz=datetime.timezone.utc)


def test_processing_with_simple_exp_smoothing():
Expand Down

0 comments on commit 6d57083

Please sign in to comment.