aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source
diff options
context:
space:
mode:
authorEduardo Julian2017-09-19 20:46:12 -0400
committerEduardo Julian2017-09-19 20:46:12 -0400
commit53b8f08c90a9d903d5ebffb6f37acd7224a4736c (patch)
tree7ca479a99a84b60d9dc3d021aa6c1081cfac4d65 /stdlib/source
parent61b92fcab969231092f1bb41ef9a6021a498dc8f (diff)
- Some improvements/refactoring for durations.
Diffstat (limited to '')
-rw-r--r--stdlib/source/lux/time/duration.lux18
1 files changed, 10 insertions, 8 deletions
diff --git a/stdlib/source/lux/time/duration.lux b/stdlib/source/lux/time/duration.lux
index 2adc023a2..800a2536a 100644
--- a/stdlib/source/lux/time/duration.lux
+++ b/stdlib/source/lux/time/duration.lux
@@ -68,7 +68,7 @@
(def: #export empty Duration (from-millis 0))
(def: #export milli Duration (from-millis 1))
-(def: #export second Duration (from-millis 1_000))
+(def: #export second Duration (scale 1_000 milli))
(def: #export minute Duration (scale 60 second))
(def: #export hour Duration (scale 60 minute))
(def: #export day Duration (scale 24 hour))
@@ -81,18 +81,20 @@
(if (:: Eq<Duration> = empty duration)
"0ms"
(let [signed? (negative? duration)
- [hours time-left] [(query hour duration) (frame hour duration)]
- hours (if signed?
- (int/abs hours)
- hours)
+ [days time-left] [(query day duration) (frame day duration)]
+ days (if signed?
+ (int/abs days)
+ days)
time-left (if signed?
(scale -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/append
(if signed? "-" "")
+ (if (i.= 0 days) "" (text/append (int/encode days) "D"))
(if (i.= 0 hours) "" (text/append (int/encode hours) "h"))
(if (i.= 0 minutes) "" (text/append (int/encode minutes) "m"))
(if (i.= 0 seconds) "" (text/append (int/encode seconds) "s"))
@@ -110,6 +112,7 @@
(do p;Monad<Parser>
[signed? (l;this? "-")
#let [sign (function [raw] (if signed? (i.* -1 raw) raw))]
+ utc-day (lex-section "D")
utc-hour (lex-section "h")
utc-minute (lex-section "m")
_ (p;assert "Invalid minute."
@@ -124,6 +127,7 @@
(and (i.>= 0 utc-millis)
(i.<= 999 utc-millis)))]
(wrap (|> empty
+ (merge (scale (sign utc-day) day))
(merge (scale (sign utc-hour) hour))
(merge (scale (sign utc-minute) minute))
(merge (scale (sign utc-second) second))
@@ -134,9 +138,7 @@
(l;run input lex-duration))
(struct: #export _
- {#;doc "Based on ISO 8601.
-
- For example: 21h14m51s827ms"}
+ {#;doc "For example: 15D21h14m51s827ms"}
(Codec Text Duration)
(def: encode encode)
(def: decode decode))