1 {-# LANGUAGE MagicHash #-}
2 module Hcompta.Format.JCC.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#)
11 import Prelude (($), (.), Integer, Num(..), RealFrac(..), Show(..))
13 import Hcompta.Date (Date)
14 import qualified Hcompta.Date as Date
15 import Hcompta.Lib.Leijen (Doc, (<>))
16 import qualified Hcompta.Lib.Leijen as W
22 let (y, mo, d) = Date.gregorian dat
23 (if y == 0 then W.empty else W.integer y <> sep '-') <> do
25 sep '-' <> int2 d <> do
27 Time.TimeOfDay 0 0 0 -> W.empty
28 Time.TimeOfDay h m s ->
29 sep '_' <> int2 h <> do
30 sep ':' <> int2 m <> do
34 (if s < 10 then W.char '0' else W.empty) <> do
35 W.strict_text $ Text.pack $ show $ (truncate s::Integer)))
38 -- _ | tz_name /= "" -> W.space <> do W.strict_text $ Text.pack tz_name
39 -- _ -> W.space <> do W.strict_text $ Text.pack $ Time.timeZoneOffsetString tz)
42 int2 i = if i < 10 then W.char '0' <> W.int i else W.int i
44 sep = W.bold . W.dullblack . W.char
48 date_length :: Date -> Int
50 let (y, _, _) = Date.gregorian dat
54 (if y < 0 then 1 else 0) -- sign
55 + (1 + (I# (integerLogBase# 10 (abs y)))) -- year
60 + (case Date.tod dat of
61 Time.TimeOfDay 0 0 0 -> 0
62 Time.TimeOfDay _ _ s ->