-
-
Notifications
You must be signed in to change notification settings - Fork 55
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
Issue #390 #400
Closed
Closed
Issue #390 #400
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,34 +3,51 @@ use core::strings | |
use units::si | ||
use datetime::functions | ||
|
||
fn _human_num_days(time: Time) -> Scalar = floor(time / day) | ||
fn _human_num_days(time: Time) -> Time = floor(time / day)*day | ||
|
||
fn _human_num_hours(time: Time) -> Time = floor(time / hours)*hour | ||
|
||
fn _human_num_minutes(time: Time) -> Time = floor(time / minutes)*minute | ||
|
||
fn _human_num_seconds(time: Time) -> Time = (time / seconds)second | ||
|
||
fn _seconds_on_stopwatch(time: Time) -> Time = _human_num_seconds(time)-_human_num_seconds(_human_num_minutes(time)) | ||
|
||
fn _minutes_on_clock(time: Time) -> Time = _human_num_minutes(time)-_human_num_minutes(_human_num_hours(time)) | ||
|
||
fn _hours_of_a_day(time: Time) -> Time = _human_num_hours(time)-_human_num_hours(_human_num_days(time)) | ||
|
||
fn _human_join(a: String, b: String) -> String = | ||
if str_slice(a, 0, 2) == "0 " then b else if str_slice(b, 0, 2) == "0 " then a else "{a} + {b}" | ||
|
||
fn _remove_plural_suffix(str: String) -> String = | ||
if str_slice(str, 0, 2) == "1 " then str_slice(str, 0, str_length(str) - 1) else str | ||
|
||
fn _human_seconds(dt: DateTime) -> String = | ||
_remove_plural_suffix(format_datetime("%-S%.f seconds", dt)) | ||
fn _human_formatted_seconds(time: Time) -> String = | ||
_remove_plural_suffix(format_datetime("%-S%.f seconds", datetime("0001-01-01T00:00:00Z") + _seconds_on_stopwatch(time))) | ||
|
||
fn _human_minutes(dt: DateTime) -> String = | ||
_remove_plural_suffix(format_datetime("%-M minutes", dt)) | ||
fn _human_formatted_minutes(time: Time) -> String = | ||
_remove_plural_suffix("{_minutes_on_clock(time)/minute} minutes") | ||
|
||
fn _human_hours(dt: DateTime) -> String = | ||
_remove_plural_suffix(format_datetime("%-H hours", dt)) | ||
fn _human_formatted_hours(time: Time) -> String = | ||
_remove_plural_suffix("{_hours_of_a_day(time)/hour} hours") | ||
|
||
fn _human_days(num_days: Scalar) -> String = | ||
_remove_plural_suffix("{num_days} days") | ||
fn _human_formatted_days(num_days: Time) -> String = | ||
_remove_plural_suffix("{_human_num_days(num_days)/day} days") | ||
|
||
fn _human_readable_duration(time: Time, dt: DateTime, num_days: Scalar) -> String = | ||
_human_join(_human_join(_human_join(_human_days(_human_num_days(time)), _human_hours(dt)), _human_minutes(dt)), _human_seconds(dt)) | ||
fn _human_readable_duration(time: Time) -> String = | ||
_human_join(_human_join(_human_join(_human_formatted_days(time), _human_formatted_hours(time)), _human_formatted_minutes(time)), _human_formatted_seconds(time)) | ||
|
||
# Implementation details: | ||
# we skip hours/minutes/seconds for durations larger than 1000 days because: | ||
# (a) we run into floating point precision problems at the nanosecond level at this point | ||
# (b) for much larger numbers, we can't convert to DateTimes anymore | ||
fn human_time_check_overflow(time: Time) = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we please |
||
if _human_num_days(time) > 1000 days | ||
then _human_formatted_days(time) | ||
else _human_readable_duration(time) | ||
|
||
fn human(time: Time) = | ||
if _human_num_days(time) > 1000 | ||
then "{_human_num_days(time)} days" | ||
else _human_readable_duration(time, datetime("0001-01-01T00:00:00Z") + time, _human_num_days(time)) | ||
if _human_num_seconds(time) < 0 seconds | ||
then "{human_time_check_overflow(time*-1)} ago" | ||
else human_time_check_overflow(time) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic for "… ago" is relatively simple, right? In that case, we don't need to repeat all these tests here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True but does protect against changes if someone switches the logic in some way in the future, and with the test base's overall speed I'd say it doesn't hurt anyone to keep these in
I can remove some of the more redundant cases though if you'd prefer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Speed is not the matter, but humans are going to read those tests and wonder what they are for. I would really prefer if we strip down the negative-time-duration tests to one or two.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated as requested