-
-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for parsing and displaying times in HH:MM:SS format? #181
Comments
A related enhancement would be to support queries like |
@eminence that would involve datetime handling, and to be frank, I'm not sure if we want to open that big can of worms (mostly related to timezone handling). |
I agree that adding datetime handling would be hard (lots of input formats to parse, tricky bits about how to convert between timezones, how to control output formats, etc), but I actually think there is a minimal version of date/time handling that is not so difficult that we can't do it :) |
I closed my related issue #274 in favor of discussing this here in one place as it mostly touches the same area though I also mentioned support for dates there. My use case would be simple mathematical operations involving mostly dates (without time) though datetime support would also be very much appreciated. I also do not think that this is that big of a can of worms. Timezone handling with
|
@septatrix I agree with everything you said. I also think we should do this step by step. Let's focus on the first point: "alternative formatting of time durations". Do we want HH:MM:SS or something like As for the opposite part, how to input times (and later dates) in a convenient way, I think it would be cool to support HH:MM and HH:MM:SS somehow. A straightforward way would be to have a special function that takes a string. Something like |
Hm, would That being said, some kind of delimiter could allow for other kinds of datetime literals too. |
Yes, type annotations, but also
I believe you are right. But it might prevent other future uses of the colon operator in some cases (for example: Haskell uses colon as a "cons" infix operator, where numbers can be on the right-hand side of
👍 |
If you eventually want to be able to have things like spaces (or slashes) in them, square brackets In lieu of something like that, |
Hm I think both are perfectly fine (though a colon kinda implies for me a time of day, not a duration)¹. However, I was wondering when this alternative formatting should be used and think having it as a function makes the most sense. It would be very counter-intuitive if calculations resulting in times suddenly changed e.g. "150 minutes" to "2 hours + 30 minutes" or other scenarios in which to output is ambiguous. Having this as an explicit function would make that less confusing - and could allow different ways to output the value. I am also not opposed to require usage of a function to parse such a time format. Whichever way we choose for time input - we should use something which could be expanded to date(time)s. ¹ ISO 8601 / RFC 3339 thus also use a different syntax: P3H32M24S |
For everyone involved in this thread, you might be interested in the discussion in #287 with an initial draft for datetime support in Numbat. |
Based on datetime handling in #287, we can implement a function called The code does not look great — mainly because we lack something like a fn __num_days(time: Time) -> Scalar =
((time / day) -> 1) // floor
fn __human_hms(time: Time) -> String =
format_datetime("%-H hours, %-M minutes, %-S seconds", parse_datetime("0001-01-01T00:00:00Z") + time)
fn __human_time(time: Time) -> String =
if time < 1 minute
then format_datetime("%-S seconds", parse_datetime("0001-01-01T00:00:00Z") + time)
else if time < 1 hour
then format_datetime("%-M minutes, %-S seconds", parse_datetime("0001-01-01T00:00:00Z") + time)
else __human_hms(time)
fn human(time: Time) =
if __num_days(time) < 1
then __human_time(time)
else "{__num_days(time)} days, {__human_hms(mod(time, 1 day))}"
assert((1 second // human) == "1 seconds")
assert((5 second // human) == "5 seconds")
assert((60 seconds // human) == "1 minutes, 0 seconds")
assert((73 seconds // human) == "1 minutes, 13 seconds")
assert((1 minute // human) == "1 minutes, 0 seconds")
assert((1.25 minute // human) == "1 minutes, 15 seconds")
assert((1 hour // human) == "1 hours, 0 minutes, 0 seconds")
assert((1 day // human) == "1 days, 0 hours, 0 minutes, 0 seconds")
assert((1.37 day // human) == "1 days, 8 hours, 52 minutes, 48 seconds")
assert((1 week // human) == "7 days, 0 hours, 0 minutes, 0 seconds") |
All points have been addressed in #287 and #327. A convenient syntax for entering dates is still missing, but let's discuss this in #323. |
We released a first version of date time handling in Numbat v1.10.1. For details, see |
See previous discussions here:
See also:
numbat/examples/format_time.nbt
Lines 1 to 9 in a114a07
The text was updated successfully, but these errors were encountered: