1 {-# LANGUAGE MagicHash #-}
2 module Hcompta.Date.Write where
4 import Data.Char (Char)
5 import Data.Eq (Eq(..))
6 import Data.Ord (Ord(..))
7 import qualified Data.Text as Text
8 import qualified Data.Time.LocalTime as Time
9 import GHC.Exts (Int(..))
10 import GHC.Integer.Logarithms (integerLogBase#)
20 import Hcompta.Date (Date)
21 import qualified Hcompta.Date as Date
22 import Hcompta.Lib.Leijen (Doc, (<>))
23 import qualified Hcompta.Lib.Leijen as W
29 let (y, mo, d) = Date.gregorian dat
30 (if y == 0 then W.empty else W.integer y <> sep '/') <> do
32 sep '/' <> int2 d <> do
34 Time.TimeOfDay 0 0 0 -> W.empty
35 Time.TimeOfDay h m s ->
36 sep '_' <> int2 h <> do
37 sep ':' <> int2 m <> do
41 (if s < 10 then W.char '0' else W.empty) <> do
42 W.strict_text $ Text.pack $ show $ (truncate s::Integer)))
45 -- _ | tz_name /= "" -> W.space <> do W.strict_text $ Text.pack tz_name
46 -- _ -> W.space <> do W.strict_text $ Text.pack $ Time.timeZoneOffsetString tz)
49 int2 i = if i < 10 then W.char '0' <> W.int i else W.int i
51 sep = W.bold . W.dullblack . W.char
55 date_length :: Date -> Int
57 let (y, _, _) = Date.gregorian dat
61 (if y < 0 then 1 else 0) -- sign
62 + (1 + (I# (integerLogBase# 10 (abs y)))) -- year
67 + (case Date.tod dat of
68 Time.TimeOfDay 0 0 0 -> 0
69 Time.TimeOfDay _ _ s ->