1 {-# LANGUAGE MagicHash #-}
2 module Hcompta.Date.Write where
4 import qualified Data.Text as Text
5 import qualified Data.Time.LocalTime as Time
6 import GHC.Exts (Int(..))
7 import GHC.Integer.Logarithms (integerLogBase#)
9 import qualified Hcompta.Lib.Leijen as W
10 import Hcompta.Lib.Leijen (Doc, (<>))
11 import qualified Hcompta.Date as Date
12 import Hcompta.Date (Date)
18 let (y, mo, d) = Date.gregorian dat
19 (if y == 0 then W.empty else W.integer y <> sep '/') <> do
21 sep '/' <> int2 d <> do
23 Time.TimeOfDay 0 0 0 -> W.empty
24 Time.TimeOfDay h m s ->
25 sep '_' <> int2 h <> do
26 sep ':' <> int2 m <> do
30 (if s < 10 then W.char '0' else W.empty) <> do
31 W.strict_text $ Text.pack $ show $ (truncate s::Integer)))
34 -- _ | tz_name /= "" -> W.space <> do W.strict_text $ Text.pack tz_name
35 -- _ -> W.space <> do W.strict_text $ Text.pack $ Time.timeZoneOffsetString tz)
38 int2 i = if i < 10 then W.char '0' <> W.int i else W.int i
40 sep = W.bold . W.dullblack . W.char
44 date_length :: Date -> Int
46 let (y, _, _) = Date.gregorian dat
50 (if y < 0 then 1 else 0) -- sign
51 + (1 + (I# (integerLogBase# 10 (abs y)))) -- year
56 + (case Date.tod dat of
57 Time.TimeOfDay 0 0 0 -> 0
58 Time.TimeOfDay _ _ s ->