Skip to content

Commit

Permalink
Manage negative times
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinLeTallec committed May 13, 2024
1 parent 37c7dc0 commit e82e367
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 3 additions & 0 deletions examples/datetime_human_tests.nbt
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ assert((1 µs -> human) == "0.000001 seconds")
assert((1 ns -> human) == "1.0e-9 seconds")
assert((1234 ns -> human) == "0.000001234 seconds")
assert((1s + 1234 ns -> human) == "1 second")

assert((-1 second -> human) == "1 second ago")
assert((-7.89 hour -> human) == "7 hours + 53 minutes + 24 seconds ago")
6 changes: 4 additions & 2 deletions numbat/modules/datetime/human.nbt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ fn _human_hours(time: Time) -> String = "{(time -> hours) / hour // floor
fn _human_minutes(time: Time) -> String = "{(time -> minutes) / minute // floor} minutes" -> _prettier
fn _human_seconds(time: Time) -> String = str_replace("{(time -> seconds) / second} seconds", ".0 ", " ") -> _prettier

fn _human_manage_past(str: String, time: Time) = str_append(str, if time < 0 s then " ago" else "")

fn _human_recurse(t: Time, result: String, time_unit: String) -> String =
if time_unit == "year"
then _human_recurse(t - (t -> year // floor) -> ms // round, _human_join(result, t -> _human_years), "month")
Expand All @@ -36,5 +38,5 @@ fn _human_recurse(t: Time, result: String, time_unit: String) -> String =

fn human(time: Time) -> String =
if time == 0 s then "0 seconds"
else if time < 60 s then time -> _human_seconds
else _human_recurse(time, "", "year")
else if abs(time) < 60 s then _human_manage_past(abs(time) -> _human_seconds, time)
else _human_manage_past(_human_recurse(abs(time), "", "year"), time)

0 comments on commit e82e367

Please sign in to comment.