Skip to content

Instantly share code, notes, and snippets.

@phaer
Created March 15, 2023 23:09
Show Gist options
  • Save phaer/fde7108b2945259434f280f84967b994 to your computer and use it in GitHub Desktop.
Save phaer/fde7108b2945259434f280f84967b994 to your computer and use it in GitHub Desktop.
parse-currentTime.nix
# https://stackoverflow.com/questions/7136385/calculate-day-number-from-an-unix-timestamp-in-a-math-way
# https://howardhinnant.github.io/date_algorithms.html#civil_from_days
let
t = builtins.currentTime;
z = t / 86400 + 719468;
era = (if z >= 0 then z else z - 146096) / 146097;
doe = (z - era * 146097);
y' = (yoe) + era * 400;
doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
mp = (5 * doy + 2) / 153;
day = doy - (153 * mp + 2) / 5 + 1;
month = if mp < 10 then mp + 3 else mp - 9;
year = y' + (if month <= 2 then 1 else 0);
in {
inherit year month day t;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment