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