Skip to content

Commit 92bdd55

Browse files
authored
Fix timezones (#143)
* fix timezone * fix for no events
1 parent 782e560 commit 92bdd55

File tree

4 files changed

+42
-19
lines changed

4 files changed

+42
-19
lines changed

frameos/src/apps/data/eventsToAgenda/app.nim

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type
1515
textColor*: Color
1616
timeColor*: Color
1717
titleColor*: Color
18+
startWithToday*: bool
1819

1920
App* = ref object of AppRoot
2021
appConfig*: AppConfig
@@ -36,17 +37,21 @@ proc get*(self: App, context: ExecutionContext): string =
3637
let time = &"^({self.appConfig.baseFontSize},{self.appConfig.timeColor.toHtmlHex()})"
3738
let events = self.appConfig.events
3839
let timezone = self.getTimezone(events)
39-
let todayTs = epochTime().Timestamp
40-
let today = format(todayTs, titleFormat, tzName = timezone)
4140

4241
proc h1(text: string): string = &"{title}{text}\n{normal}\n"
4342
proc formatDay(day: string): string = format(parseTs("{year/4}-{month/2}-{day/2}", day), titleFormat)
4443

45-
var currentDay = format(todayTs, "{year/4}-{month/2}-{day/2}", tzName = timezone)
44+
let noEvents = events == nil or events.kind != JArray or events.len == 0
4645

47-
result = h1(today)
46+
result = ""
4847

49-
if events == nil or events.kind != JArray or events.len == 0:
48+
var currentDay = ""
49+
if self.appConfig.startWithToday or noEvents:
50+
let todayTs = epochTime().Timestamp
51+
result &= h1(format(todayTs, titleFormat, tzName = timezone))
52+
currentDay = format(todayTs, "{year/4}-{month/2}-{day/2}", tzName = timezone)
53+
54+
if noEvents:
5055
result &= &"No events found\n"
5156
return
5257

@@ -61,10 +66,9 @@ proc get*(self: App, context: ExecutionContext): string =
6166
let withTime = "T" in startDay
6267
let startDate = startDay.split("T")[0]
6368

64-
if startDate > currentDay:
65-
if not hasAny:
69+
if startDate != currentDay: # new day, past or future
70+
if not hasAny and startDate != currentDay and self.appConfig.startWithToday:
6671
result &= "No events today\n"
67-
6872
result &= "\n" & h1(formatDay(startDate))
6973
currentDay = startDate
7074

frameos/src/apps/data/eventsToAgenda/config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@
4444
"value": "#FF0000",
4545
"required": true,
4646
"label": "Time color"
47+
},
48+
{
49+
"name": "startWithToday",
50+
"type": "boolean",
51+
"value": "true",
52+
"required": true,
53+
"label": "Always start with today's date"
4754
}
4855
],
4956
"output": [

frameos/src/frameos/config.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ proc setConfigDefaults*(config: var FrameConfig) =
1414
if config.frameHost == "": config.frameHost = "localhost"
1515
if config.frameAccess == "": config.frameAccess = "private"
1616
if config.name == "": config.name = config.frameHost
17-
if config.timeZone == "": config.timeZone = findSystemTimeZone()
17+
if config.timeZone == "": config.timeZone = detectSystemTimeZone()
1818

1919
proc loadSchedule*(data: JsonNode): FrameSchedule =
2020
result = FrameSchedule(events: @[])
@@ -101,7 +101,7 @@ proc loadConfig*(filename: string = "frame.json"): FrameConfig =
101101
saveAssets: if data{"saveAssets"} == nil: %*(false) else: data{"saveAssets"},
102102
logToFile: data{"logToFile"}.getStr(),
103103
debug: data{"debug"}.getBool() or commandLineParams().contains("--debug"),
104-
timeZone: data{"timeZone"}.getStr("UTC"),
104+
timeZone: data{"timeZone"}.getStr(""),
105105
schedule: loadSchedule(data{"schedule"}),
106106
gpioButtons: loadGPIOButtons(data{"gpioButtons"}),
107107
controlCode: loadControlCode(data{"controlCode"}),

frameos/src/lib/tz.nim

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,26 @@ proc initTimeZone*() =
88
const tzData = staticRead("../../assets/compiled/tz/tzdata.json")
99
loadTzData(tzData)
1010

11-
proc findSystemTimeZone*(): string =
12-
let filename = "/etc/timezone"
1311

12+
proc detectSystemTimeZone*(): string =
13+
## Returns e.g. "Europe/Brussels"; never raises.
14+
const zoneinfoPrefix = "/usr/share/zoneinfo/"
1415
try:
15-
if fileExists(filename):
16-
let line = readFile(filename).strip()
17-
if line != "":
18-
return line
19-
except:
20-
discard
21-
return "UTC"
16+
# Works whenever /etc/localtime is a symlink (systemd-managed distros)
17+
let tgt = expandSymlink("/etc/localtime")
18+
if tgt.startsWith(zoneinfoPrefix):
19+
result = tgt[zoneinfoPrefix.len .. ^1] # strip the prefix
20+
except OSError: discard
21+
22+
# Debian/Raspberry-Pi fallback: /etc/timezone is a plain text copy
23+
if result.len == 0 and fileExists("/etc/timezone"):
24+
result = readFile("/etc/timezone").strip()
25+
26+
# Last-ditch: stay explicit
27+
if result.len == 0:
28+
result = "UTC"
29+
30+
# check if result is a valid timezone
31+
if not valid(findTimeZone(result)):
32+
echo "Warning: Detected timezone is not valid: ", result
33+
result = "UTC"

0 commit comments

Comments
 (0)