Files
playlet-piped/src/source/utils/TimeUtils.bs
T
Brahim Hadricheandgithub-action update-app-version 5822207397 Create lint workflow (#32)
* Create lint workflow

* Update lint.yml

* Lint fix

* Update lint.yml

Co-authored-by: github-action update-app-version <githubaction@githubaction.com>
2022-12-17 07:48:11 -05:00

113 lines
3.2 KiB
Plaintext

import "pkg:/source/utils/Types.bs"
namespace TimeUtils
' Helper function convert seconds to mm:ss format
' getTime(138) returns 2:18
function GetFormattedTime(length as integer) as string
minutes = (length \ 60).ToStr()
seconds = length mod 60
if seconds < 10
seconds = "0" + seconds.ToStr()
else
seconds = seconds.ToStr()
end if
return minutes + ":" + seconds
end function
function GetFormattedTimeLeft(unixTimestamp as integer) as string
if unixTimestamp = invalid
return "N/A"
end if
currentTime = TimeUtils.Now().AsSeconds()
timeLeft = unixTimestamp - currentTime
if timeLeft <= 0
return "now"
end if
if timeLeft < 60
result = `${timeLeft} second`
if timeLeft > 1
result += "s"
end if
return result
end if
timeLeft = timeLeft / 60.0
if timeLeft < 60
timeLeft = Cint(timeLeft)
result = `${timeLeft} minute`
if timeLeft > 1
result += "s"
end if
return result
end if
timeLeft = timeLeft / 60.0
if timeLeft < 24
timeLeft = Cint(timeLeft)
result = `${timeLeft} hour`
if timeLeft > 1
result += "s"
end if
return result
else
timeLeft = timeLeft / 24.0
timeLeft = Cint(timeLeft)
result = `${timeLeft} day`
if timeLeft > 1
result += "s"
end if
return result
end if
end function
function Now() as object
this = m.now
if this = invalid
this = CreateObject("roDateTime")
m.now = this
end if
this.mark()
return this
end function
function RFC1123_Date(when as object) as string
wd = when.getWeekday().left(3)
dom = Stri(when.getDayOfMonth()).trim()
mon = Ucase(TimeUtils.MonthStr(when.getMonth()).left(3))
yr = Stri(when.getYear()).trim()
hr = Stri(when.getHours()).trim()
mn = Stri(when.getMinutes()).trim()
sc = Stri(when.getSeconds()).trim()
date = wd + ", " + dom + " " + mon + " " + yr + " " + hr + ":" + mn + ":" + sc + " GMT"
return date
end function
function MonthStr(mNum as integer) as string
ma = m.Num2Month
if ma = invalid
ma = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]
m.Num2Month = ma
end if
return validstr(ma[mNum - 1])
end function
function ParseTime(time as string) as integer
hours = 0
minutes = 0
seconds = 0
tokens = time.Tokenize(":")
if tokens.Count() = 3
hours = tokens[0].ToInt()
minutes = tokens[1].ToInt()
seconds = tokens[2].ToInt()
else if tokens.Count() = 2
minutes = tokens[0].ToInt()
seconds = tokens[1].ToInt()
end if
return hours * 3600 + minutes * 60 + seconds
end function
end namespace