aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/program/aedifex/artifact/time_stamp/date.lux
blob: 18df2900bcd10c6444bf1f22cd18b86e8edd60fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
(.module:
  [lux #*
   [abstract
    [monad (#+ do)]]
   [control
    ["<>" parser
     ["<.>" text (#+ Parser)]]]
   [data
    [text
     ["%" format]]]
   [math
    [number
     ["n" nat]]]
   [time
    ["." date (#+ Date)]
    ["." year]
    ["." month]]])

(def: #export (pad value)
  (-> Nat Text)
  (if (n.< 10 value)
    (%.format "0" (%.nat value))
    (%.nat value)))

(def: #export (format value)
  (%.Format Date)
  (%.format (|> value date.year year.value .nat %.nat)
            (|> value date.month month.number ..pad)
            (|> value date.day_of_month ..pad)))

(def: #export parser
  (Parser Date)
  (do <>.monad
    [year (<>.codec n.decimal (<text>.exactly 4 <text>.decimal))
     year (<>.lift (year.year (.int year)))
     month (<>.codec n.decimal (<text>.exactly 2 <text>.decimal))
     month (<>.lift (month.by_number month))
     day_of_month (<>.codec n.decimal (<text>.exactly 2 <text>.decimal))]
    (<>.lift (date.date year month day_of_month))))