Skip to content

Commit

Permalink
Use variable in funtion to simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinLeTallec committed Sep 29, 2024
1 parent bbe7d19 commit a6c2f0f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/tests/human.nbt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ assert_eq((10 yr + 2 s -> human), "3652.42 days (approx. 10 years)")

assert_eq((1 sidereal_day -> human), "23 hours + 56 minutes + 4.091 seconds")

assert_eq((10000 days -> human), "10000 days (approx. 27 years + 4 months)")
assert_eq((10000 days -> human), "10000 days (approx. 27 years + 5 months)")
assert_eq((50 million days -> human), "50_000_000 days (approx. 136_895 years)")

assert_eq((1e12 days -> human), "1_000_000_000_000 days (approx. 2_737_909_345 years)")
Expand Down
11 changes: 5 additions & 6 deletions numbat/modules/datetime/human.nbt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ fn _prettier(str: String) -> String =
where clean_str = str_replace(str, ".0 ", " ")

fn _human_years(time: Time) -> String = "{(time -> years) / year |> floor} years" -> _prettier
fn _human_months(time: Time) -> String = "{(time -> months) / month |> floor} months" -> _prettier
fn _human_months(time: Time) -> String = "{(time -> months) / month |> round} months" -> _prettier

fn _human_days(time: Time) -> String = "{(time -> days) / day |> floor} days" -> _prettier
fn _human_hours(time: Time) -> String = "{(time -> hours) / hour |> floor} hours" -> _prettier
fn _human_minutes(time: Time) -> String = "{(time -> minutes) / minute |> floor} minutes" -> _prettier
Expand All @@ -32,10 +33,8 @@ fn _human_recurse(t: Time, result: String, time_unit: String) -> String =
then _human_recurse(t - (t |> floor_in(min)), _human_join(result, t -> _human_minutes), "second")
else _human_join(result, (t |> round_in(ms)) -> _precise_human_seconds)

fn _human_approx_recurse(t: Time, result: String, time_unit: String) -> String =
if time_unit == "year"
then _human_approx_recurse(t - (t |> floor_in(year)) |> round_in(ms), _human_join(result, t -> _human_years), "month")
else _human_join(result, t -> _human_months)
fn _year_month_approx(t: Time) -> String = _human_join(the_years -> _human_years, t - the_years -> _human_months)
where the_years = t |> floor_in(year)

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

Expand All @@ -49,7 +48,7 @@ fn _abs_human(time: Time) -> String =
else if time < 1 year
then _human_for_long_duration(time -> _precise_human_days, (time |> round_in(month/10)) -> _precise_human_months)
else if time < 100 years
then _human_for_long_duration(time -> _precise_human_days, _human_approx_recurse(time, "", "year"))
then _human_for_long_duration(time -> _precise_human_days, _year_month_approx(time))
else
_human_for_long_duration(time -> _precise_human_days, time -> _human_years)

Expand Down

0 comments on commit a6c2f0f

Please sign in to comment.