Pour Go's time package into a Elvish module
See issue #1029 for an example why Elvish should provide access to Go's time package. It shouldn't be necessary to run, non-portable, external commands such as date +%s to perform basic time related functions such as determining the current time.
There needs to be a comprehensive design of how to wrap the time.Duration and time.Time types. I haven't thought much about it yet but I can review a design if someone else gives it a go - preferably as a document, not code yet.
I can review a design if someone else gives it a go - preferably as a document, not code yet.
Absolutely. The Go time package is a very different animal than the string and math Go packages. The latter were amenable to simply aliasing a Go function to an Elvish symbol (possibly via a shim function). The time package, however, requires exposing new data types. Too, deciding where and how to allow simple strings where the new time and duration data types might otherwise be expected to be arguments.
May I suggest using the universal strftime standard that (somewhat) exists in every language? Instead of Go's cryptic (and kind of made up IMO) time format / layout like 01/02 03:04:05PM '06 -0700. You can't expect everyone to remember the release date of Go, and then write/interpret the time format based on those numbers. It's also not as safe (you can escape % character, but escaping every number that's not meant to be formatted is too ugly). Besides the padding (with zeros / spaces) syntax is also more readable and flexible with strftime, kind of like printf (that kind of exists in almost every language).
There are also several Go implementations.
And of course an strptime (reverse of strftime) would be nice too.
I suggest taking a look at Python's time.
Here is my thoughts: (I wrote objects as maps, but you give them types)
use time
~ > time:now
▶ [&year=2022 &month=1 &day=8 &hour=15 &minute=14 &second=59 &weekday=Sat]
~ > var t = (time:now)
~ > put t[year] t[month] t[day]
▶ 2022
▶ 1
▶ 8
~ > time:format "+%Y-%m-%d-%H%M%S" $t
▶ 2022-01-08-151459
~ > time:parse "+%Y-%m-%d-%H%M%S" "2022-01-08-151459"
▶ [&year=2022 &month=1 &day=8 &hour=15 &minute=14 &second=59 &weekday=Sat]
~ > time:duration "1h 30m"
▶ [&days=0 &hours=1 &minutes=30 &seconds=0 &nanoseconds=0]
~ > time:after $t (time:duration "1h 30m")
▶ [&year=2022 &month=1 &day=8 &hour=16 &minute=44 &second=59 &weekday=Sat]
~ > var midnight = (time:replace &hour=0 &minute=0 &second=0 $t)
~ > time:delta $t $midnight
▶ [&days=0 &hours=15 &minutes=14 &seconds=59 &nanoseconds=0]