Skip to content

Commit

Permalink
Add time functions
Browse files Browse the repository at this point in the history
  • Loading branch information
curtisma committed Mar 6, 2024
1 parent c90f6cc commit b68f5cd
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
55 changes: 55 additions & 0 deletions tests/data_types/test_Time.ils
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
let(((Time VrtImport['Time])
(Test VrtImport['Test])
(Virtue VrtImport['Virtue])
(example_time "Feb 23 10:54:12 2024")
)

procedure(Test_IntTimeTable()
let(((timeTable Time->IntTimeTable()))
assert(timeTable["string"] == example_time)
assert(timeTable["year"] == 2024)
assert(timeTable["month"] == 2)
assert(timeTable["day"] == 23)
assert(timeTable["hour"] == 10)
assert(timeTable["min"] == 54)
assert(timeTable["sec"] == 12)
assert(timeTable["weekday"] == 5)
assert(timeTable["yearday"] == 54)
assert(timeTable["isdst"] == 0)
))

procedure(Test_IsoDateString()
assert(IsoDateString(example_time)
== "2024-02-23")
)

procedure(Test_IsoTimeString()
assert(IsoTimeString(example_time)
== "10:54:12")
)

procedure(Test_CurrentDatetimeIsoUTC()
let(((value Time->CurrentDatetimeIsoUTC()))
assert(stringp(value))
assert(strlen(value) == 19)
))

procedure(Test_lint_Time(lint_settings)
assert(lint_settings->RunLint("../virtue/data_types/Time.ils"))
)

procedure(Test_lint_test_Time(lint_settings)
assert(lint_settings->RunLint("data_types/test_Time.ils"))
)

Test->RunFile(list(nil
'Test_IntTimeTable Test_IntTimeTable
'Test_IsoDateString Test_IsoDateString
'Test_IsoTimeString Test_IsoTimeString
'Test_lint_Time Test_lint_Time
'Test_lint_test_Time Test_lint_test_Time
)
?filepath Virtue->GetCurrentFilePath()
)

)
58 changes: 58 additions & 0 deletions virtue/data_types/Time.ils
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
let((Time
(module_description "Time functions")
(Module VrtImport['Module])
(Str VrtImport['Str])
)
Time = let(()

procedure(IntTimeTable(@optional (timeString getCurrentTime()) "t")
"A table with the local date and time broken out into integer parameters"
let(((timeTable makeTable(timeString))
(time timeToTm(stringToTime(timeString))))
timeTable["string"] = timeString ; The original input as a string
timeTable["year"] = 1900 + time->tm_year ; year (int)
timeTable["month"] = 1 + time->tm_mon ; month [1, 12] (int)
timeTable["day"] = time->tm_mday ; day of the month: [1, 31] (int)
timeTable["hour"] = time->tm_hour ; hours after midnight: [0, 23] (int)
timeTable["min"] = time->tm_min ; minutes after the hour: [0, 59] (int)
timeTable["sec"] = time->tm_sec ; seconds after the minute: [0, 61] (int)
timeTable["weekday"] = time->tm_wday ; days since Sunday: [0, 6] (int)
timeTable["yearday"] = time->tm_yday ; days since January: [0, 365] (int)
timeTable["isdst"] = time->tm_isdst ; daylight saving time flag: <0,0,>0
; timeTable["zone"] = system("date +'%Z") ; TODO: Time Zone
timeTable
))

procedure(IsoDateString(@optional (timeString getCurrentTime()) "t")
"The local date as a string in ISO 8601 format, YYYY-MM-DD"
let(((time IntTimeTable()))
sprintf(nil "%.4d-%.2d-%.2d" time["year"] time["month"] time["day"])
))

procedure(IsoTimeString(@optional (timeString getCurrentTime()) "t")
"The local time as a string in ISO 8601 format, 'YYYY-MM-DD'"
let(((time IntTimeTable()))
sprintf(nil "%.2d:%.2d:%.2d" time["hour"] time["min"] time["sec"])
))

procedure(CurrentDatetimeIsoUTC()
"The UTC datetime as a string in ISO 8601 format, 'YYYY-MM-DD hh:mm:ss'
A space seperates the date and time"
let((child_id out)
child_id = ipcBeginProcess("date --utc +'%Y-%m-%d %T'")
out = Str->trimWhiteSpace(ipcReadProcess(child_id 1))
out
))

list(nil
'IntTimeTable IntTimeTable
'IsoDateString IsoDateString
'IsoTimeString IsoTimeString
'CurrentDatetimeIsoUTC CurrentDatetimeIsoUTC
))

Module->New('Time
?module Time
?package VrtImport['Virtue]
?description module_description)
)

0 comments on commit b68f5cd

Please sign in to comment.