diff --git a/frameos/src/apps/data/eventsToAgenda/app.nim b/frameos/src/apps/data/eventsToAgenda/app.nim index 1cac38f15..b3ecd15c1 100644 --- a/frameos/src/apps/data/eventsToAgenda/app.nim +++ b/frameos/src/apps/data/eventsToAgenda/app.nim @@ -15,6 +15,7 @@ type textColor*: Color timeColor*: Color titleColor*: Color + startWithToday*: bool App* = ref object of AppRoot appConfig*: AppConfig @@ -36,17 +37,21 @@ proc get*(self: App, context: ExecutionContext): string = let time = &"^({self.appConfig.baseFontSize},{self.appConfig.timeColor.toHtmlHex()})" let events = self.appConfig.events let timezone = self.getTimezone(events) - let todayTs = epochTime().Timestamp - let today = format(todayTs, titleFormat, tzName = timezone) proc h1(text: string): string = &"{title}{text}\n{normal}\n" proc formatDay(day: string): string = format(parseTs("{year/4}-{month/2}-{day/2}", day), titleFormat) - var currentDay = format(todayTs, "{year/4}-{month/2}-{day/2}", tzName = timezone) + let noEvents = events == nil or events.kind != JArray or events.len == 0 - result = h1(today) + result = "" - if events == nil or events.kind != JArray or events.len == 0: + var currentDay = "" + if self.appConfig.startWithToday or noEvents: + let todayTs = epochTime().Timestamp + result &= h1(format(todayTs, titleFormat, tzName = timezone)) + currentDay = format(todayTs, "{year/4}-{month/2}-{day/2}", tzName = timezone) + + if noEvents: result &= &"No events found\n" return @@ -61,10 +66,9 @@ proc get*(self: App, context: ExecutionContext): string = let withTime = "T" in startDay let startDate = startDay.split("T")[0] - if startDate > currentDay: - if not hasAny: + if startDate != currentDay: # new day, past or future + if not hasAny and startDate != currentDay and self.appConfig.startWithToday: result &= "No events today\n" - result &= "\n" & h1(formatDay(startDate)) currentDay = startDate diff --git a/frameos/src/apps/data/eventsToAgenda/config.json b/frameos/src/apps/data/eventsToAgenda/config.json index b23428b38..9ad9efea8 100644 --- a/frameos/src/apps/data/eventsToAgenda/config.json +++ b/frameos/src/apps/data/eventsToAgenda/config.json @@ -44,6 +44,13 @@ "value": "#FF0000", "required": true, "label": "Time color" + }, + { + "name": "startWithToday", + "type": "boolean", + "value": "true", + "required": true, + "label": "Always start with today's date" } ], "output": [ diff --git a/frameos/src/frameos/config.nim b/frameos/src/frameos/config.nim index 5b13d2ad1..f044fc7ba 100644 --- a/frameos/src/frameos/config.nim +++ b/frameos/src/frameos/config.nim @@ -14,7 +14,7 @@ proc setConfigDefaults*(config: var FrameConfig) = if config.frameHost == "": config.frameHost = "localhost" if config.frameAccess == "": config.frameAccess = "private" if config.name == "": config.name = config.frameHost - if config.timeZone == "": config.timeZone = findSystemTimeZone() + if config.timeZone == "": config.timeZone = detectSystemTimeZone() proc loadSchedule*(data: JsonNode): FrameSchedule = result = FrameSchedule(events: @[]) @@ -101,7 +101,7 @@ proc loadConfig*(filename: string = "frame.json"): FrameConfig = saveAssets: if data{"saveAssets"} == nil: %*(false) else: data{"saveAssets"}, logToFile: data{"logToFile"}.getStr(), debug: data{"debug"}.getBool() or commandLineParams().contains("--debug"), - timeZone: data{"timeZone"}.getStr("UTC"), + timeZone: data{"timeZone"}.getStr(""), schedule: loadSchedule(data{"schedule"}), gpioButtons: loadGPIOButtons(data{"gpioButtons"}), controlCode: loadControlCode(data{"controlCode"}), diff --git a/frameos/src/lib/tz.nim b/frameos/src/lib/tz.nim index 2b5ee294d..029c40b38 100644 --- a/frameos/src/lib/tz.nim +++ b/frameos/src/lib/tz.nim @@ -8,14 +8,26 @@ proc initTimeZone*() = const tzData = staticRead("../../assets/compiled/tz/tzdata.json") loadTzData(tzData) -proc findSystemTimeZone*(): string = - let filename = "/etc/timezone" +proc detectSystemTimeZone*(): string = + ## Returns e.g. "Europe/Brussels"; never raises. + const zoneinfoPrefix = "/usr/share/zoneinfo/" try: - if fileExists(filename): - let line = readFile(filename).strip() - if line != "": - return line - except: - discard - return "UTC" + # Works whenever /etc/localtime is a symlink (systemd-managed distros) + let tgt = expandSymlink("/etc/localtime") + if tgt.startsWith(zoneinfoPrefix): + result = tgt[zoneinfoPrefix.len .. ^1] # strip the prefix + except OSError: discard + + # Debian/Raspberry-Pi fallback: /etc/timezone is a plain text copy + if result.len == 0 and fileExists("/etc/timezone"): + result = readFile("/etc/timezone").strip() + + # Last-ditch: stay explicit + if result.len == 0: + result = "UTC" + + # check if result is a valid timezone + if not valid(findTimeZone(result)): + echo "Warning: Detected timezone is not valid: ", result + result = "UTC"