diff options
Diffstat (limited to 'stdlib')
-rw-r--r-- | stdlib/source/lux/time/duration.lux | 73 | ||||
-rw-r--r-- | stdlib/test/test/lux/time/duration.lux | 28 |
2 files changed, 1 insertions, 100 deletions
diff --git a/stdlib/source/lux/time/duration.lux b/stdlib/source/lux/time/duration.lux index 20ef20c8c..a39e6f9ed 100644 --- a/stdlib/source/lux/time/duration.lux +++ b/stdlib/source/lux/time/duration.lux @@ -108,76 +108,3 @@ (structure: #export _ (Monoid Duration) (def: identity empty) (def: compose merge)) - -(def: (encode duration) - (-> Duration Text) - (if (:: Equivalence<Duration> = empty duration) - "+0ms" - (let [signed? (negative? duration) - [days time-left] [(query day duration) (frame day duration)] - days (if signed? - (int/abs days) - days) - time-left (if signed? - (scale-up -1 time-left) - time-left) - [hours time-left] [(query hour time-left) (frame hour time-left)] - [minutes time-left] [(query minute time-left) (frame minute time-left)] - [seconds time-left] [(query second time-left) (frame second time-left)] - millis (to-millis time-left)] - ($_ text/compose - (if signed? "-" "+") - (if (i/= +0 days) "" (text/compose (nat/encode (.nat days)) "D")) - (if (i/= +0 hours) "" (text/compose (nat/encode (.nat hours)) "h")) - (if (i/= +0 minutes) "" (text/compose (nat/encode (.nat minutes)) "m")) - (if (i/= +0 seconds) "" (text/compose (nat/encode (.nat seconds)) "s")) - (if (i/= +0 millis) "" (text/compose (nat/encode (.nat millis)) "ms")) - )))) - -(def: (lex-section suffix) - (-> Text (l.Lexer Int)) - (|> (p.codec number.Codec<Text,Int> (l.many l.decimal)) - (p.before (p.and (l.this suffix) (p.not l.alpha))) - (p.default +0))) - -(def: lex-duration - (l.Lexer Duration) - (do p.Monad<Parser> - [signed? (p.or (l.this? "-") (l.this? "+")) - #let [sign (function (_ raw) - (case signed? - (#.Left _) - (i/* -1 raw) - - (#.Right _) - raw))] - utc-day (lex-section "D") - utc-hour (lex-section "h") - utc-minute (lex-section "m") - _ (p.assert "Invalid minute." - (and (i/>= +0 utc-minute) - (i/<= +59 utc-minute))) - utc-second (lex-section "s") - _ (p.assert "Invalid second." - (and (i/>= +0 utc-second) - (i/<= +59 utc-second))) - utc-millis (lex-section "ms") - _ (p.assert "Invalid milli-seconds." - (and (i/>= +0 utc-millis) - (i/<= +999 utc-millis)))] - (wrap (|> empty - (merge (scale-up (sign utc-day) day)) - (merge (scale-up (sign utc-hour) hour)) - (merge (scale-up (sign utc-minute) minute)) - (merge (scale-up (sign utc-second) second)) - (merge (scale-up (sign utc-millis) milli)))))) - -(def: (decode input) - (-> Text (e.Error Duration)) - (l.run input lex-duration)) - -(structure: #export _ - {#.doc "For example: +15D21h14m51s827ms"} - (Codec Text Duration) - (def: encode encode) - (def: decode decode)) diff --git a/stdlib/test/test/lux/time/duration.lux b/stdlib/test/test/lux/time/duration.lux index 669af6b4c..8bf00b88b 100644 --- a/stdlib/test/test/lux/time/duration.lux +++ b/stdlib/test/test/lux/time/duration.lux @@ -57,30 +57,4 @@ (test "Merging with the empty duration changes nothing." (|> sample (@.merge @.empty) (@/= sample))) (test "Merging a duration with it's opposite yields an empty duration." - (|> sample (@.merge (@.scale-up -1 sample)) (@/= @.empty))) - (test "Can frame a duration in terms of another." - (cond (and (@.positive? frame) (@.positive? sample)) - (|> sample (@.frame frame) (@/< frame)) - - (and (@.negative? frame) (@.negative? sample)) - (|> sample (@.frame frame) (@/> frame)) - - (or (or (@.neutral? frame) (@.neutral? sample)) - (|> sample - (@.frame frame) - (@.scale-up -1) - (@/< (if (@.negative? frame) - (@.scale-up -1 frame) - frame)))))))))) - -(context: "Codec" - (<| (times 100) - (do @ - [sample duration - #let [(^open "@/.") @.Equivalence<Duration> - (^open "@/.") @.Codec<Text,Duration>]] - (test "Can encode/decode durations." - (E.default #0 - (do E.Monad<Error> - [decoded (|> sample @/encode @/decode)] - (wrap (@/= sample decoded)))))))) + (|> sample (@.merge (@.scale-up -1 sample)) (@/= @.empty))))))) |