diff options
Diffstat (limited to '')
-rw-r--r-- | stdlib/source/lux/time/instant.lux | 93 |
1 files changed, 48 insertions, 45 deletions
diff --git a/stdlib/source/lux/time/instant.lux b/stdlib/source/lux/time/instant.lux index d8fb0fe98..b85e3edd1 100644 --- a/stdlib/source/lux/time/instant.lux +++ b/stdlib/source/lux/time/instant.lux @@ -2,26 +2,29 @@ [lux #* [io (#+ IO io)] [control - equivalence - order - enum + [equivalence (#+ Equivalence)] + [order (#+ Order)] + [enum (#+ Enum)] codec [monad (#+ do Monad)] ["p" parser]] [data ["." error (#+ Error)] ["." maybe] - ["." number ("int/." Codec<Text,Int>)] - [text ("text/." Monoid<Text>) + [number + ["." int ("int/." decimal)]] + [text ("text/." monoid) ["l" lexer]] [collection - ["." list ("list/." Fold<List>)] - ["." row (#+ Row row) ("row/." Functor<Row> Fold<Row>)]]] + ["." list ("list/." fold)] + ["." row (#+ Row row) ("row/." functor fold)]]] [type abstract]] [// - ["." duration ("duration/." Order<Duration>)] - ["." date]]) + ["." duration ("duration/." order)] + ["." date (#+ Date)] + ["." month (#+ Month)] + ["." day (#+ Day)]]) (abstract: #export Instant {#.doc "Instant is defined as milliseconds since the epoch."} @@ -51,24 +54,24 @@ (-> duration.Duration Instant) (|> offset duration.to-millis :abstraction)) - (structure: #export _ (Equivalence Instant) + (structure: #export equivalence (Equivalence Instant) (def: (= param subject) - (:: number.Equivalence<Int> = (:representation param) (:representation subject)))) + (:: int.equivalence = (:representation param) (:representation subject)))) - (`` (structure: #export _ (Order Instant) - (def: eq Equivalence<Instant>) + (`` (structure: #export order (Order Instant) + (def: &equivalence ..equivalence) (~~ (do-template [<name>] [(def: (<name> param subject) - (:: number.Order<Int> <name> (:representation param) (:representation subject)))] + (:: int.order <name> (:representation param) (:representation subject)))] [<] [<=] [>] [>=] )))) - (`` (structure: #export _ (Enum Instant) - (def: order Order<Instant>) + (`` (structure: #export enum (Enum Instant) + (def: &order ..order) (~~ (do-template [<name>] [(def: <name> - (|>> :representation (:: number.Enum<Int> <name>) :abstraction))] + (|>> :representation (:: int.enum <name>) :abstraction))] [succ] [pred] )))) @@ -217,9 +220,9 @@ ## Codec::decode (def: lex-year (l.Lexer Int) - (do p.Monad<Parser> + (do p.monad [sign (p.or (l.this "-") (l.this "+")) - raw-year (p.codec number.Codec<Text,Int> (l.many l.decimal)) + raw-year (p.codec int.decimal (l.many l.decimal)) #let [signum (case sign (#.Left _) -1 (#.Right _) +1)]] @@ -227,14 +230,14 @@ (def: lex-section (l.Lexer Int) - (p.codec number.Codec<Text,Int> (l.exactly 2 l.decimal))) + (p.codec int.decimal (l.exactly 2 l.decimal))) (def: lex-millis (l.Lexer Int) (p.either (|> (l.at-most 3 l.decimal) - (p.codec number.Codec<Text,Int>) + (p.codec int.decimal) (p.after (l.this "."))) - (:: p.Monad<Parser> wrap +0))) + (:: p.monad wrap +0))) (def: (leap-years year) (-> Int Int) @@ -245,7 +248,7 @@ ## Based on: https://stackoverflow.com/a/3309340/6823464 ## (def: lex-instant ## (l.Lexer Instant) -## (do p.Monad<Parser> +## (do p.monad ## [utc-year lex-year ## _ (l.this "-") ## utc-month lex-section @@ -315,32 +318,32 @@ (io (from-millis ("lux io current-time")))) (def: #export (date instant) - (-> Instant date.Date) + (-> Instant Date) (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 #month.January + +1 #month.February + +2 #month.March + +3 #month.April + +4 #month.May + +5 #month.June + +6 #month.July + +7 #month.August + +8 #month.September + +9 #month.October + +10 #month.November + +11 #month.December _ (undefined)) #date.day (.nat day)})) (def: #export (month instant) - (-> Instant date.Month) + (-> Instant Month) (let [[year month day] (date instant)] month)) (def: #export (day instant) - (-> Instant date.Day) + (-> Instant Day) (let [offset (relative instant) days (duration.query duration.day offset) day-time (duration.frame duration.day offset) @@ -354,11 +357,11 @@ (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 + +0 #day.Sunday + +1 #day.Monday + +2 #day.Tuesday + +3 #day.Wednesday + +4 #day.Thursday + +5 #day.Friday + +6 #day.Saturday _ (undefined)))) |