Skip to content

Commit

Permalink
Support for multiple date formats (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
almarklein authored Jul 24, 2024
1 parent 4176741 commit f297722
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
36 changes: 27 additions & 9 deletions timetagger/app/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2837,7 +2837,9 @@ def open(self, t1=None, t2=None, tags=None):
close_but = self.maindiv.children[0].children[-1]
close_but.onclick = self.close
self._date_range.innerHTML = (
t1_date + "  –  " + t2_date
dt.format_isodate(t1_date)
+ "  –  "
+ dt.format_isodate(t2_date)
)
self._date_range.innerHTML += (
"&nbsp;&nbsp;<button type='button'><i class='fas'>\uf073</i></button>"
Expand Down Expand Up @@ -3040,7 +3042,7 @@ def _generate_table_rows(self, t1, t2):
date = dt.time2localstr(record.t1).split(" ")[0]
year = int(date.split("-")[0])
if group_period == "day":
period = "-".join(reversed(date.split("-")))
period = dt.format_isodate(date)
elif group_period == "week":
week = dt.get_weeknumber(record.t1)
period = f"{year}W{week}"
Expand Down Expand Up @@ -3114,7 +3116,7 @@ def _generate_table_rows(self, t1, t2):
"record",
record.key,
duration,
sd1,
dt.format_isodate(sd1),
st1,
st2,
to_str(record.get("ds", "")), # strip tabs and newlines
Expand Down Expand Up @@ -3261,8 +3263,8 @@ def _save_as_pdf(self):
# )

tagname = self._tags.join(" ") if self._tags else "all"
d1 = reversed(self._t1_date.split("-")).join("-")
d2 = reversed(self._t2_date.split("-")).join("-")
d1 = dt.format_isodate(self._t1_date)
d2 = dt.format_isodate(self._t2_date)
doc.setFontSize(11)
doc.text("Tags: ", margin + 20, margin + 15, {"align": "right"})
doc.text(tagname, margin + 20, margin + 15)
Expand Down Expand Up @@ -3877,6 +3879,12 @@ def open(self, callback=None):
<option value='1'>Monday - Saturday</option>
<option value='0'>Monday - Sunday</option>
</select>
<div>Show dates as:</div>
<select>
<option value='yyyy-mm-dd'>yyyy-mm-dd (ISO 8601)</option>
<option value='dd-mm-yyyy'>dd-mm-yyyy (default)</option>
<option value='mm/dd/yyyy'>mm/dd/yyyy (US)</option>
</select>
<div>Show time as:</div>
<select>
<option value='auto'>Auto</option>
Expand Down Expand Up @@ -3991,27 +3999,33 @@ def open(self, callback=None):
self._workdays.value = workdays
self._workdays.onchange = self._on_workdays_change

# Date representation
date_repr = window.simplesettings.get("date_repr")
self._date_repr = self._repr_form.children[5]
self._date_repr.value = date_repr
self._date_repr.onchange = self._on_date_repr_change

# Time representation
time_repr = window.simplesettings.get("time_repr")
self._time_repr = self._repr_form.children[5]
self._time_repr = self._repr_form.children[7]
self._time_repr.value = time_repr
self._time_repr.onchange = self._on_time_repr_change

# Duration representation
duration_repr = window.simplesettings.get("duration_repr")
self._duration_repr = self._repr_form.children[7]
self._duration_repr = self._repr_form.children[9]
self._duration_repr.value = duration_repr
self._duration_repr.onchange = self._on_duration_repr_change

# Today snap time/offset
today_snap_offset = window.simplesettings.get("today_snap_offset")
self._today_snap_offset = self._repr_form.children[9]
self._today_snap_offset = self._repr_form.children[11]
self._today_snap_offset.value = today_snap_offset
self._today_snap_offset.onchange = self._on_today_snap_offset_change

# Today number of hours
today_end_offset = window.simplesettings.get("today_end_offset")
self._today_end_offset = self._repr_form.children[11]
self._today_end_offset = self._repr_form.children[13]
self._today_end_offset.value = today_end_offset
self._today_end_offset.onchange = self._on_today_end_offset_change

Expand Down Expand Up @@ -4062,6 +4076,10 @@ def _on_workdays_change(self):
workdays = int(self._workdays.value)
window.simplesettings.set("workdays", workdays)

def _on_date_repr_change(self):
date_repr = self._date_repr.value
window.simplesettings.set("date_repr", date_repr)

def _on_time_repr_change(self):
time_repr = self._time_repr.value
window.simplesettings.set("time_repr", time_repr)
Expand Down
19 changes: 18 additions & 1 deletion timetagger/app/dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,30 @@ def time2str(t, utc_offset=None):


def time2localstr(t):
"""Convert a time int into a textual local representation, and with a space instead of T."""
"""Convert a time int into a textual local representation, and with a space instead of T. Note that the date is always yyyy-mm-dd"""
s = time2str(t)
s1, s2 = s.split("T")
s2 = s2.split("-")[0].split("+")[0].rstrip("Z")
return s1 + " " + s2


def format_isodate(date, fmt=None):
"Format an iso date to a formatted date."
yyyy, mm, dd = date.split("-")
if fmt is None:
fmt = "dd-mm-yyyy"
if window.simplesettings:
fmt = window.simplesettings.get("date_repr", fmt)
if fmt == "yyyy-mm-dd":
return yyyy + "-" + mm + "-" + dd
elif fmt == "dd-mm-yyyy":
return dd + "-" + mm + "-" + yyyy
elif fmt == "mm/dd/yyyy":
return mm + "/" + dd + "/" + yyyy
else:
return date


def round(t, res):
"""Round the given date to the nearest resolution step."""
PSCRIPT_OVERLOAD = False # noqa
Expand Down
1 change: 1 addition & 0 deletions timetagger/app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ def __init__(self):
self._synced_keys = {
"first_day_of_week": 1,
"workdays": 0,
"date_repr": "dd-mm-yyyy",
"time_repr": "auto",
"duration_repr": "hms",
"today_snap_offset": "",
Expand Down

0 comments on commit f297722

Please sign in to comment.