1 {-# LANGUAGE MagicHash #-}
2 module Hcompta.Date.Write where
4 import Data.Fixed (showFixed)
5 import qualified Data.Text as Text
6 import qualified Data.Time.LocalTime as Time
7 import GHC.Exts (Int(..))
8 import GHC.Integer.Logarithms (integerLogBase#)
10 import qualified Hcompta.Lib.Leijen as W
11 import Hcompta.Lib.Leijen (Doc, (<>))
12 import qualified Hcompta.Date as Date
13 import Hcompta.Date (Date)
19 let (y, mo, d) = Date.gregorian dat
20 (if y == 0 then W.empty else W.integer y <> sep '/') <> do
22 sep '/' <> int2 d <> do
24 Time.TimeOfDay 0 0 0 -> W.empty
25 Time.TimeOfDay h m s ->
26 W.space <> int2 h <> do
27 sep ':' <> int2 m <> do
31 (if s < 10 then W.char '0' else W.empty) <> do
32 W.strict_text $ Text.pack $ showFixed True s))
35 -- _ | tz_name /= "" -> W.space <> do W.strict_text $ Text.pack tz_name
36 -- _ -> W.space <> do W.strict_text $ Text.pack $ Time.timeZoneOffsetString tz)
39 int2 i = if i < 10 then W.char '0' <> W.int i else W.int i
41 sep = W.bold . W.dullblack . W.char
45 date_length :: Date -> Int
47 let (y, _, _) = Date.gregorian dat
51 (if y < 0 then 1 else 0) -- sign
52 + (1 + (I# (integerLogBase# 10 (abs y)))) -- year
57 + (case Date.tod dat of
58 Time.TimeOfDay 0 0 0 -> 0
59 Time.TimeOfDay _ _ s ->