2023-11-11 00:42:33 +00:00
|
|
|
import "pkg:/source/utils/misc.bs"
|
2023-11-06 19:05:21 +00:00
|
|
|
|
2023-12-23 20:15:16 +00:00
|
|
|
' @fileoverview Clock component to display current time formatted based on user's chosen 12 or 24 hour setting
|
|
|
|
|
|
|
|
' Possible clock formats
|
|
|
|
enum ClockFormat
|
|
|
|
h12 = "12h"
|
|
|
|
h24 = "24h"
|
|
|
|
end enum
|
|
|
|
|
2023-11-06 19:05:21 +00:00
|
|
|
sub init()
|
|
|
|
|
2023-12-23 20:15:16 +00:00
|
|
|
' If hideclick setting is enabled, exit without setting any variables
|
2023-11-06 19:05:21 +00:00
|
|
|
if m.global.session.user.settings["ui.design.hideclock"]
|
|
|
|
return
|
|
|
|
end if
|
|
|
|
|
|
|
|
m.clockTime = m.top.findNode("clockTime")
|
|
|
|
|
|
|
|
m.currentTimeTimer = m.top.findNode("currentTimeTimer")
|
|
|
|
m.dateTimeObject = CreateObject("roDateTime")
|
|
|
|
|
|
|
|
m.currentTimeTimer.observeField("fire", "onCurrentTimeTimerFire")
|
|
|
|
m.currentTimeTimer.control = "start"
|
|
|
|
|
|
|
|
' Default to 12 hour clock
|
2023-12-23 20:15:16 +00:00
|
|
|
m.format = ClockFormat.h12
|
2023-11-06 19:05:21 +00:00
|
|
|
|
|
|
|
' If user has selected a 24 hour clock, update date display format
|
2023-12-23 20:15:16 +00:00
|
|
|
if LCase(m.global.device.clockFormat) = ClockFormat.h24
|
|
|
|
m.format = ClockFormat.h24
|
2023-11-06 19:05:21 +00:00
|
|
|
end if
|
|
|
|
end sub
|
|
|
|
|
|
|
|
|
|
|
|
' onCurrentTimeTimerFire: Code that runs every time the currentTimeTimer fires
|
|
|
|
'
|
|
|
|
sub onCurrentTimeTimerFire()
|
|
|
|
' Refresh time variable
|
|
|
|
m.dateTimeObject.Mark()
|
|
|
|
|
|
|
|
' Convert to local time zone
|
|
|
|
m.dateTimeObject.ToLocalTime()
|
|
|
|
|
2023-12-23 20:15:16 +00:00
|
|
|
' Format time for display - based on 12h/24h setting
|
|
|
|
formattedTime = formatTimeAsString()
|
|
|
|
|
|
|
|
' Display time
|
|
|
|
m.clockTime.text = formattedTime
|
2023-11-06 19:05:21 +00:00
|
|
|
end sub
|
2023-12-23 20:15:16 +00:00
|
|
|
|
|
|
|
' formatTimeAsString: Returns a string with the current time formatted for either a 12 or 24 hour clock
|
|
|
|
'
|
|
|
|
' @return {string} current time formatted for either a 12 hour or 24 hour clock
|
|
|
|
function formatTimeAsString() as string
|
|
|
|
return m.format = ClockFormat.h12 ? format12HourTime() : format24HourTime()
|
|
|
|
end function
|
|
|
|
|
|
|
|
' format12HourTime: Returns a string with the current time formatted for a 12 hour clock
|
|
|
|
'
|
|
|
|
' @return {string} current time formatted for a 12 hour clock
|
|
|
|
function format12HourTime() as string
|
|
|
|
currentHour = m.dateTimeObject.GetHours()
|
|
|
|
currentMinute = m.dateTimeObject.GetMinutes()
|
|
|
|
|
|
|
|
displayedHour = StrI(currentHour).trim()
|
|
|
|
displayedMinute = StrI(currentMinute).trim()
|
|
|
|
meridian = currentHour < 12 ? "am" : "pm"
|
|
|
|
|
|
|
|
if currentHour = 0
|
|
|
|
displayedHour = "12"
|
|
|
|
end if
|
|
|
|
|
|
|
|
if currentHour > 12
|
|
|
|
correctedHour = currentHour - 12
|
|
|
|
displayedHour = StrI(correctedHour).trim()
|
|
|
|
end if
|
|
|
|
|
|
|
|
if currentMinute < 10
|
|
|
|
displayedMinute = `0${displayedMinute}`
|
|
|
|
end if
|
|
|
|
|
|
|
|
return `${displayedHour}:${displayedMinute} ${meridian}`
|
|
|
|
end function
|
|
|
|
|
|
|
|
' format24HourTime: Returns a string with the current time formatted for a 24 hour clock
|
|
|
|
'
|
|
|
|
' @return {string} current time formatted for a 24 hour clock
|
|
|
|
function format24HourTime() as string
|
|
|
|
currentHour = m.dateTimeObject.GetHours()
|
|
|
|
currentMinute = m.dateTimeObject.GetMinutes()
|
|
|
|
|
|
|
|
displayedHour = StrI(currentHour).trim()
|
|
|
|
displayedMinute = StrI(currentMinute).trim()
|
|
|
|
|
|
|
|
if currentHour < 10
|
|
|
|
displayedHour = `0${displayedHour}`
|
|
|
|
end if
|
|
|
|
|
|
|
|
if currentMinute < 10
|
|
|
|
displayedMinute = `0${displayedMinute}`
|
|
|
|
end if
|
|
|
|
|
|
|
|
return `${displayedHour}:${displayedMinute}`
|
|
|
|
end function
|