1module Data.Time.Calendar.Private where
2
3import Data.Time.Orphans ()
4import Data.Fixed
5
6data PadOption = Pad Int Char | NoPad
7
8showPadded :: PadOption -> String -> String
9showPadded NoPad s = s
10showPadded (Pad i c) s = replicate (i - length s) c ++ s
11
12class (Num t,Ord t,Show t) => ShowPadded t where
13    showPaddedNum :: PadOption -> t -> String
14
15instance ShowPadded Integer where
16    showPaddedNum NoPad i = show i
17    showPaddedNum pad i | i < 0 = '-':(showPaddedNum pad (negate i))
18    showPaddedNum pad i = showPadded pad $ show i
19
20instance ShowPadded Int where
21    showPaddedNum NoPad i = show i
22    showPaddedNum _pad i | i == minBound = show i
23    showPaddedNum pad i | i < 0 = '-':(showPaddedNum pad (negate i))
24    showPaddedNum pad i = showPadded pad $ show i
25
26show2Fixed :: Pico -> String
27show2Fixed x | x < 10 = '0':(showFixed True x)
28show2Fixed x = showFixed True x
29
30show2 :: (ShowPadded t) => t -> String
31show2 = showPaddedNum $ Pad 2 '0'
32
33show3 :: (ShowPadded t) => t -> String
34show3 = showPaddedNum $ Pad 3 '0'
35
36show4 :: (ShowPadded t) => t -> String
37show4 = showPaddedNum $ Pad 4 '0'
38
39mod100 :: (Integral i) => i -> i
40mod100 x = mod x 100
41
42div100 :: (Integral i) => i -> i
43div100 x = div x 100
44
45clip :: (Ord t) => t -> t -> t -> t
46clip a _ x | x < a = a
47clip _ b x | x > b = b
48clip _ _ x = x
49
50clipValid :: (Ord t) => t -> t -> t -> Maybe t
51clipValid a _ x | x < a = Nothing
52clipValid _ b x | x > b = Nothing
53clipValid _ _ x = Just x
54
55quotBy :: (Real a,Integral b) => a -> a -> b
56quotBy d n = truncate ((toRational n) / (toRational d))
57
58remBy :: Real a => a -> a -> a
59remBy d n = n - (fromInteger f) * d where
60    f = quotBy d n
61
62quotRemBy :: (Real a,Integral b) => a -> a -> (b,a)
63quotRemBy d n = let
64    f = quotBy d n
65    in (f,n - (fromIntegral f) * d)
66