Gauche
Gauche copied to clipboard
Literal time object
Sometimes I want to do a quick calculation involving time on REPL, e.g. "what is 12m23s + 5m13s + 7m02s"?
We have srfi-19, but it will involve building date object, converting it to time, do the computation, invert it back to date and print. Too much hassle.
How about something like #t12m23s? It can be read as a time-duration object.
An ISO 8601 read syntax for Scheme could make sense. That standard can represent durations and intervals too.
#t stands for true; maybe we should pick something else.
Naggum's local-time proposal for Common lisp uses @ and #@.
The # syntax we have so far: https://registry.scheme.org/#hash-syntax We don't seem to be using #@ yet.
cc @johnwcowan who is working on date/time SRFIs
The above example would be something like (time+ #@PT12M23S #@PT5M13S #@PT7M02S).
Oh, this isn't for standardizing general literal time object; it's mainly for the convenience on REPL, and I intend to keep it Gauche-specific. I intentionally suggested #t... because future srfis and standards are not likely to use it because of the truth value. (We already have #f and #f32(...), so syntactically there's no problem).
#@ looks "too special" and I thought I'd keep it for more serious usage, but if it had been suggested, #@ might not be a bad choice.
LGTM, though I'm not a big fan of extending the lexical syntax. I've added optional #@ support to TimeAdvancedCowan. I've also added support for iSO duration strings, which look for example like PD10H20M30S40.50 or PY1W26 (P = period). These syntaxes are disjoint from ordinary 8601 syntax, so the same #@ can be used. I am not supporting interval syntax or interval objects in general, although you can subtract two dates to get a duration or add a duration to a date to get another date.
Any reviews of TimeAdvancedCowan (stupid name, I can never remember it) would be helpful.
(We already have
#fand#f32(...), so syntactically there's no problem).
Good point! That goes all the way back to SRFI 4.
#@ looks "too special"
Agreed, but it's hard to think of any syntax that doesn't have that problem. @ without # would be nicer, but it has lots of potential uses (at least Kawa is already using it for arg splicing IIRC; and some Scheme programs assume symbols can contain @ characters).
I am not supporting interval syntax or interval objects in general
IMHO if we add lexical syntax for date and time in SRFIs, I think we should have one syntax that covers all the ISO 8601 features:
- point in time
- duration
- interval
Perhaps there could be a simpler SRFI that offers only a subset of the syntax, and a more complex SRFI that offers the full ISO feature set.
Since Common Lisp has some people who are interested in reviving Naggum's proposal, which uses ISO, it would be really nice if we could coordinate things so that CL and Scheme have the same read syntax for ISO.