Created
March 15, 2023 23:09
-
-
Save phaer/fde7108b2945259434f280f84967b994 to your computer and use it in GitHub Desktop.
parse-currentTime.nix
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
# 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