diff options
Diffstat (limited to 'stdlib/source/lux/time/instant.lux')
-rw-r--r-- | stdlib/source/lux/time/instant.lux | 166 |
1 files changed, 83 insertions, 83 deletions
diff --git a/stdlib/source/lux/time/instant.lux b/stdlib/source/lux/time/instant.lux index ea834391e..d6c506218 100644 --- a/stdlib/source/lux/time/instant.lux +++ b/stdlib/source/lux/time/instant.lux @@ -77,20 +77,20 @@ (def: #export epoch {#.doc "The instant corresponding to 1970-01-01T00:00:00Z"} Instant - (from-millis 0)) + (from-millis +0)) ## Codec::encode (def: (divisible? factor input) (-> Int Int Bit) - (|> input (i/% factor) (i/= 0))) + (|> input (i/% factor) (i/= +0))) (def: (leap-year? year) (-> Int Bit) - (and (divisible? 4 year) - (or (not (divisible? 100 year)) - (divisible? 400 year)))) + (and (divisible? +4 year) + (or (not (divisible? +100 year)) + (divisible? +400 year)))) -(def: epoch-year Int 1970) +(def: epoch-year Int +1970) (def: (find-year now) (-> Instant [Int duration.Duration]) @@ -99,7 +99,7 @@ (let [year (if (leap-year? reference) duration.leap-year duration.normal-year)] - (if (i/= 0 (duration.query year time-left)) + (if (i/= +0 (duration.query year time-left)) [reference time-left] (if (duration/>= duration.empty time-left) (recur (inc reference) (duration.merge (duration.scale -1 year) time-left)) @@ -108,36 +108,36 @@ (def: normal-months (Row Nat) - (row +31 +28 +31 - +30 +31 +30 - +31 +31 +30 - +31 +30 +31)) + (row 31 28 31 + 30 31 30 + 31 31 30 + 31 30 31)) (def: leap-year-months (Row Nat) - (row.update [+1] inc normal-months)) + (row.update [1] inc normal-months)) (def: (find-month months time) (-> (Row Nat) duration.Duration [Nat duration.Duration]) (if (duration/>= duration.empty time) (row/fold (function (_ month-days [current-month time-left]) (let [month-duration (duration.scale (.int month-days) duration.day)] - (if (i/= 0 (duration.query month-duration time-left)) + (if (i/= +0 (duration.query month-duration time-left)) [current-month time-left] [(inc current-month) (duration.merge (duration.scale -1 month-duration) time-left)]))) - [+0 time] + [0 time] months) (row/fold (function (_ month-days [current-month time-left]) (let [month-duration (duration.scale (.int month-days) duration.day)] - (if (i/= 0 (duration.query month-duration time-left)) + (if (i/= +0 (duration.query month-duration time-left)) [current-month time-left] [(dec current-month) (duration.merge month-duration time-left)]))) - [+11 time] + [11 time] (row.reverse months)))) (def: (pad value) (-> Int Text) - (if (i/< 10 value) + (if (i/< +10 value) (text/compose "0" (int/encode value)) (int/encode value))) @@ -149,48 +149,48 @@ (def: (encode-millis millis) (-> Int Text) - (cond (i/= 0 millis) "" - (i/< 10 millis) ($_ text/compose ".00" (int/encode millis)) - (i/< 100 millis) ($_ text/compose ".0" (int/encode millis)) - ## (i/< 1_000 millis) + (cond (i/= +0 millis) "" + (i/< +10 millis) ($_ text/compose ".00" (int/encode millis)) + (i/< +100 millis) ($_ text/compose ".0" (int/encode millis)) + ## (i/< +1_000 millis) ($_ text/compose "." (int/encode millis)))) (def: seconds-per-day Int (duration.query duration.second duration.day)) -(def: days-up-to-epoch Int 719468) +(def: days-up-to-epoch Int +719468) (def: (extract-date instant) (-> Instant [[Int Int Int] duration.Duration]) (let [offset (relative instant) seconds (duration.query duration.second offset) z (|> seconds (i// seconds-per-day) (i/+ days-up-to-epoch)) - era (i// 146097 - (if (i/>= 0 z) + era (i// +146097 + (if (i/>= +0 z) z - (i/- 146096 z))) - days-of-era (|> z (i/- (i/* 146097 era))) + (i/- +146096 z))) + days-of-era (|> z (i/- (i/* +146097 era))) years-of-era (|> days-of-era - (i/- (i// 1460 days-of-era)) - (i/+ (i// 36524 days-of-era)) - (i/- (i// 146096 days-of-era)) - (i// 365)) - year (|> years-of-era (i/+ (i/* 400 era))) + (i/- (i// +1460 days-of-era)) + (i/+ (i// +36524 days-of-era)) + (i/- (i// +146096 days-of-era)) + (i// +365)) + year (|> years-of-era (i/+ (i/* +400 era))) days-of-year (|> days-of-era - (i/- (|> (i/* 365 years-of-era) - (i/+ (i// 4 years-of-era)) - (i/- (i// 100 years-of-era))))) + (i/- (|> (i/* +365 years-of-era) + (i/+ (i// +4 years-of-era)) + (i/- (i// +100 years-of-era))))) day-time (duration.frame duration.day offset) days-of-year (if (duration/>= duration.empty day-time) days-of-year (dec days-of-year)) - mp (|> days-of-year (i/* 5) (i/+ 2) (i// 153)) + mp (|> days-of-year (i/* +5) (i/+ +2) (i// +153)) day (|> days-of-year - (i/- (|> mp (i/* 153) (i/+ 2) (i// 5))) - (i/+ 1)) + (i/- (|> mp (i/* +153) (i/+ +2) (i// +5))) + (i/+ +1)) month (|> mp - (i/+ (if (i/< 10 mp) - 3 + (i/+ (if (i/< +10 mp) + +3 -9))) - year (if (i/<= 2 month) + year (if (i/<= +2 month) (inc year) year)] [[year month day] @@ -218,29 +218,29 @@ (def: lex-year (l.Lexer Int) (do p.Monad<Parser> - [sign? (p.maybe (l.this "-")) + [sign (p.or (l.this "-") (l.this "+")) raw-year (p.codec number.Codec<Text,Int> (l.many l.decimal)) - #let [signum (case sign? - #.None 1 - (#.Some _) -1)]] + #let [signum (case sign + (#.Left _) -1 + (#.Right _) +1)]] (wrap (i/* signum raw-year)))) (def: lex-section (l.Lexer Int) - (p.codec number.Codec<Text,Int> (l.exactly +2 l.decimal))) + (p.codec number.Codec<Text,Int> (l.exactly 2 l.decimal))) (def: lex-millis (l.Lexer Int) - (p.either (|> (l.at-most +3 l.decimal) + (p.either (|> (l.at-most 3 l.decimal) (p.codec number.Codec<Text,Int>) (p.after (l.this "."))) - (:: p.Monad<Parser> wrap 0))) + (:: p.Monad<Parser> wrap +0))) (def: (leap-years year) (-> Int Int) - (|> (i// 4 year) - (i/- (i// 100 year)) - (i/+ (i// 400 year)))) + (|> (i// +4 year) + (i/- (i// +100 year)) + (i/+ (i// +400 year)))) ## Based on: https://stackoverflow.com/a/3309340/6823464 (def: lex-instant @@ -250,8 +250,8 @@ _ (l.this "-") utc-month lex-section _ (p.assert "Invalid month." - (and (i/>= 1 utc-month) - (i/<= 12 utc-month))) + (and (i/>= +1 utc-month) + (i/<= +12 utc-month))) #let [months (if (leap-year? utc-year) leap-year-months normal-months) @@ -261,34 +261,34 @@ _ (l.this "-") utc-day lex-section _ (p.assert "Invalid day." - (and (i/>= 1 utc-day) + (and (i/>= +1 utc-day) (i/<= (.int month-days) utc-day))) _ (l.this "T") utc-hour lex-section _ (p.assert "Invalid hour." - (and (i/>= 0 utc-hour) - (i/<= 23 utc-hour))) + (and (i/>= +0 utc-hour) + (i/<= +23 utc-hour))) _ (l.this ":") utc-minute lex-section _ (p.assert "Invalid minute." - (and (i/>= 0 utc-minute) - (i/<= 59 utc-minute))) + (and (i/>= +0 utc-minute) + (i/<= +59 utc-minute))) _ (l.this ":") utc-second lex-section _ (p.assert "Invalid second." - (and (i/>= 0 utc-second) - (i/<= 59 utc-second))) + (and (i/>= +0 utc-second) + (i/<= +59 utc-second))) utc-millis lex-millis _ (l.this "Z") #let [years-since-epoch (i/- epoch-year utc-year) previous-leap-days (i/- (leap-years epoch-year) (leap-years (dec utc-year))) - year-days-so-far (|> (i/* 365 years-since-epoch) + year-days-so-far (|> (i/* +365 years-since-epoch) (i/+ previous-leap-days)) month-days-so-far (|> months row.to-list (list.take (.nat (dec utc-month))) - (list/fold n/+ +0)) + (list/fold n/+ 0)) total-days (|> year-days-so-far (i/+ (.int month-days-so-far)) (i/+ (dec utc-day)))]] @@ -320,18 +320,18 @@ (let [[[year month day] _] (extract-date instant)] {#date.year year #date.month (case (dec month) - 0 #date.January - 1 #date.February - 2 #date.March - 3 #date.April - 4 #date.May - 5 #date.June - 6 #date.July - 7 #date.August - 8 #date.September - 9 #date.October - 10 #date.November - 11 #date.December + +0 #date.January + +1 #date.February + +2 #date.March + +3 #date.April + +4 #date.May + +5 #date.June + +6 #date.July + +7 #date.August + +8 #date.September + +9 #date.October + +10 #date.November + +11 #date.December _ (undefined)) #date.day (.nat day)})) @@ -350,16 +350,16 @@ (dec days) days) ## 1970/01/01 was a Thursday - y1970m0d0 4] + y1970m0d0 +4] (case (|> y1970m0d0 - (i/+ days) (i/% 7) + (i/+ days) (i/% +7) ## This is done to turn negative days into positive days. - (i/+ 7) (i/% 7)) - 0 #date.Sunday - 1 #date.Monday - 2 #date.Tuesday - 3 #date.Wednesday - 4 #date.Thursday - 5 #date.Friday - 6 #date.Saturday + (i/+ +7) (i/% +7)) + +0 #date.Sunday + +1 #date.Monday + +2 #date.Tuesday + +3 #date.Wednesday + +4 #date.Thursday + +5 #date.Friday + +6 #date.Saturday _ (undefined)))) |