aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--stdlib/source/lux/data/text/format.lux5
-rw-r--r--stdlib/source/lux/macro/poly/json.lux6
-rw-r--r--stdlib/source/lux/time/duration.lux25
-rw-r--r--stdlib/test/test/lux/data/format/json.lux11
4 files changed, 39 insertions, 8 deletions
diff --git a/stdlib/source/lux/data/text/format.lux b/stdlib/source/lux/data/text/format.lux
index ad0653e76..234a639f2 100644
--- a/stdlib/source/lux/data/text/format.lux
+++ b/stdlib/source/lux/data/text/format.lux
@@ -15,6 +15,7 @@
[list ("list/." Monad<List>)]]]
[time
["." instant]
+ ["." duration]
["." date]]
[math
["." modular]]
@@ -57,6 +58,10 @@
[%date date.Date (:: date.Codec<Text,Date> encode)]
)
+(def: #export %duration
+ (Format duration.Duration)
+ duration.encode)
+
(def: #export (%cursor [file line column])
(Format Cursor)
(|> (list (%t file) (%n line) (%n column))
diff --git a/stdlib/source/lux/macro/poly/json.lux b/stdlib/source/lux/macro/poly/json.lux
index f1a5a840a..96c647c6b 100644
--- a/stdlib/source/lux/macro/poly/json.lux
+++ b/stdlib/source/lux/macro/poly/json.lux
@@ -24,7 +24,7 @@
["d" dictionary]]]
[time
## ["i" instant]
- ["du" duration]
+ ## ["du" duration]
["da" date]]
[macro (#+ with-gensyms)
["s" syntax (#+ syntax:)]
@@ -103,7 +103,7 @@
(wrap (` (: (~ (@JSON//encode inputT))
(|>> (:: <codec> (~' encode)) #//.String)))))]
- [du.Duration du.Codec<Text,Duration>]
+ ## [du.Duration du.Codec<Text,Duration>]
## [i.Instant i.Codec<Text,Instant>]
[da.Date da.Codec<Text,Date>]
[da.Day da.Codec<Text,Day>]
@@ -219,7 +219,7 @@
(wrap (` (: (~ (@JSON//decode inputT))
(p.codec <codec> //.string)))))]
- [du.Duration du.Codec<Text,Duration>]
+ ## [du.Duration du.Codec<Text,Duration>]
## [i.Instant i.Codec<Text,Instant>]
[da.Date da.Codec<Text,Date>]
[da.Day da.Codec<Text,Day>]
diff --git a/stdlib/source/lux/time/duration.lux b/stdlib/source/lux/time/duration.lux
index a39e6f9ed..35401497a 100644
--- a/stdlib/source/lux/time/duration.lux
+++ b/stdlib/source/lux/time/duration.lux
@@ -108,3 +108,28 @@
(structure: #export _ (Monoid Duration)
(def: identity empty)
(def: compose merge))
+
+(def: #export (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"))
+ ))))
diff --git a/stdlib/test/test/lux/data/format/json.lux b/stdlib/test/test/lux/data/format/json.lux
index dc30d54d7..0f905a844 100644
--- a/stdlib/test/test/lux/data/format/json.lux
+++ b/stdlib/test/test/lux/data/format/json.lux
@@ -30,13 +30,14 @@
[time
["ti" instant]
["tda" date]
- ["tdu" duration]]
+ ## ["tdu" duration]
+ ]
test]
[test
[lux
[time
["_." instant]
- ["_." duration]
+ ## ["_." duration]
["_." date]]]]
)
@@ -93,7 +94,7 @@
## #tuple [Bit Frac Text]
#recursive Recursive
## #instant ti.Instant
- #duration tdu.Duration
+ ## #duration tdu.Duration
#date tda.Date
#grams (unit.Qty unit.Gram)
})
@@ -125,7 +126,7 @@
## ($_ r.and r.bit r.frac (r.unicode size))
gen-recursive
## _instant.instant
- _duration.duration
+ ## _duration.duration
_date.date
qty
)))
@@ -161,7 +162,7 @@
## (:: text.Equivalence<Text> = tL2 tR2)))
(:: Equivalence<Recursive> = (get@ #recursive recL) (get@ #recursive recR))
## (:: ti.Equivalence<Instant> = (get@ #instant recL) (get@ #instant recR))
- (:: tdu.Equivalence<Duration> = (get@ #duration recL) (get@ #duration recR))
+ ## (:: tdu.Equivalence<Duration> = (get@ #duration recL) (get@ #duration recR))
(:: tda.Equivalence<Date> = (get@ #date recL) (get@ #date recR))
(:: unit.Equivalence<Unit> = (get@ #grams recL) (get@ #grams recR))
))))