aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source
diff options
context:
space:
mode:
Diffstat (limited to '')
-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
3 files changed, 33 insertions, 3 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"))
+ ))))