Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions frameos/src/apps/data/eventsToAgenda/app.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type
textColor*: Color
timeColor*: Color
titleColor*: Color
startWithToday*: bool

App* = ref object of AppRoot
appConfig*: AppConfig
Expand All @@ -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

Expand All @@ -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

Expand Down
7 changes: 7 additions & 0 deletions frameos/src/apps/data/eventsToAgenda/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
4 changes: 2 additions & 2 deletions frameos/src/frameos/config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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: @[])
Expand Down Expand Up @@ -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"}),
Expand Down
30 changes: 21 additions & 9 deletions frameos/src/lib/tz.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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"