Debian Codegraph @

src/Lambdabot/Compat/AltTime.hs

raw
1 -- | Time compatibility layer
2 -- (stuff to support old lambdabot state serialization formats)
3 --
4 -- TODO: trim this down to just the explicitly serialization-related stuff
5 module Lambdabot.Compat.AltTime
6 ( ClockTime
7 , getClockTime
8 , diffClockTimes
9 , addToClockTime
10 , timeDiffPretty
11
12 , TimeDiff(..)
13 , noTimeDiff
14 ) where
15
16 import Control.Arrow (first)
17
18 import Data.Binary
19
20 import Data.List
21 import Data.Time
22 import Text.Read hiding (get, lexP, readPrec)
23 import Text.Read.Lex
24
25 -- | Wrapping ClockTime (which doesn't provide a Read instance!) seems
26 -- easier than talking care of the serialization of UserStatus
27 -- ourselves.
28 --
29 newtype ClockTime = ClockTime UTCTime
30 deriving Eq
31
32 newtype TimeDiff = TimeDiff NominalDiffTime
33 deriving (Eq, Ord)
34
35 noTimeDiff :: TimeDiff
36 noTimeDiff = TimeDiff 0
37
38 epoch :: UTCTime
39 epoch = UTCTime (fromGregorian 1970 1 1) 0
40
41 -- convert to/from the format in old-time, so we can serialize things
42 -- in the same way as older versions of lambdabot.
43 toOldTime :: ClockTime -> (Integer, Integer)
44 toOldTime (ClockTime t) = round (diffUTCTime t epoch * 1e12) `divMod` 1000000000000
45
46 fromOldTime :: Integer -> Integer -> ClockTime
47 fromOldTime x y = ClockTime (addUTCTime (fromIntegral x + fromIntegral y * 1e-12) epoch)
48
49 instance Show ClockTime where
50 showsPrec p = showsPrec p . toOldTime
51
52 instance Read ClockTime where
53 readsPrec p = map (first (uncurry fromOldTime)) . readsPrec p
54
55 instance Show TimeDiff where
56 showsPrec p td = showParen (p > 10) $
57 ( showString "TimeDiff {tdYear = "
58 . showsPrec 11 ye
59 . showString ", tdMonth = "
60 . showsPrec 11 mo
61 . showString ", tdDay = "
62 . showsPrec 11 da
63 . showString ", tdHour = "
64 . showsPrec 11 ho
65 . showString ", tdMin = "
66 . showsPrec 11 mi
67 . showString ", tdSec = "
68 . showsPrec 11 se
69 . showString ", tdPicosec = "
70 . showsPrec 11 ps
71 . showString "}")
72 where (ye, mo, da, ho, mi, se, ps) = toOldTimeDiff td
73
74 instance Read TimeDiff where
75 readsPrec = readPrec_to_S $ parens
76 (prec 11 (do
77 let lexP = lift Text.Read.Lex.lex
78 readPrec :: Read a => ReadPrec a
79 readPrec = readS_to_Prec readsPrec
80 Ident "TimeDiff" <- lexP
81 Punc "{" <- lexP
82 Ident "tdYear" <- lexP
83 Punc "=" <- lexP
84 ye <- reset readPrec
85 Punc "," <- lexP
86 Ident "tdMonth" <- lexP
87 Punc "=" <- lexP
88 mo <- reset readPrec
89 Punc "," <- lexP
90 Ident "tdDay" <- lexP
91 Punc "=" <- lexP
92 da <- reset readPrec
93 Punc "," <- lexP
94 Ident "tdHour" <- lexP
95 Punc "=" <- lexP
96 ho <- reset readPrec
97 Punc "," <- lexP
98 Ident "tdMin" <- lexP
99 Punc "=" <- lexP
100 mi <- reset readPrec
101 Punc "," <- lexP
102 Ident "tdSec" <- lexP
103 Punc "=" <- lexP
104 se <- reset readPrec
105 Punc "," <- lexP
106 Ident "tdPicosec" <- lexP
107 Punc "=" <- lexP
108 ps <- reset readPrec
109 Punc "}" <- lexP
110 return (fromOldTimeDiff ye mo da ho mi se ps)))
111 readList = readListDefault
112 readListPrec = readListPrecDefault
113
114 -- | Retrieve the current clocktime
115 getClockTime :: IO ClockTime
116 getClockTime = ClockTime `fmap` getCurrentTime
117
118 -- | Difference of two clock times
119 diffClockTimes :: ClockTime -> ClockTime -> TimeDiff
120 diffClockTimes (ClockTime ct1) (ClockTime ct2) = TimeDiff (diffUTCTime ct1 ct2)
121
122 -- | @'addToClockTime' d t@ adds a time difference @d@ and a -- clock
123 -- time @t@ to yield a new clock time.
124 addToClockTime :: TimeDiff -> ClockTime -> ClockTime
125 addToClockTime (TimeDiff td) (ClockTime ct) = ClockTime (addUTCTime td ct)
126
127 -- | Pretty-print a TimeDiff. Both positive and negative Timediffs produce
128 -- the same output.
129 --
130 -- 14d 17h 8m 53s
131 --
132 timeDiffPretty :: TimeDiff -> String
133 timeDiffPretty td = concat . intersperse " " $ filter (not . null)
134 [ prettyP ye "y"
135 , prettyP mo "m"
136 , prettyP da "d"
137 , prettyP ho "h"
138 , prettyP mi "m"
139 , prettyP se "s"
140 ]
141 where
142 prettyP 0 _ = []
143 prettyP i s = show i ++ s
144
145 (ye, mo, da, ho, mi, se, _) = toOldTimeDiff td
146
147 toOldTimeDiff :: TimeDiff -> (Int, Int, Int, Int, Int, Int, Integer)
148 toOldTimeDiff (TimeDiff td) = (fromInteger ye, fromInteger mo, fromInteger da, fromInteger ho, fromInteger mi, fromInteger se, ps)
149 where
150 (a, ps) = round (td * 1e12) `divMod` 1000000000000
151 (b, se) = a `divMod` 60
152 (c, mi) = b `divMod` 60
153 (d, ho) = c `divMod` 24
154 (e, da) = d `divMod` 28
155 (ye, mo) = e `divMod` 12
156
157 fromOldTimeDiff :: Int -> Int -> Int -> Int -> Int -> Int -> Integer -> TimeDiff
158 fromOldTimeDiff ye mo da ho mi se ps =
159 TimeDiff
160 (1e-12 * fromIntegral (ps
161 + 1000000000000 * (toInteger se
162 + 60 * (toInteger mi
163 + 60 * (toInteger ho
164 + 24 * (toInteger da
165 + 28 * (toInteger mo
166 + 12 * toInteger ye)))))))
167
168 ------------------------------------------------------------------------
169
170 instance Binary ClockTime where
171 put t = put i >> put j
172 where (i, j) = toOldTime t
173 get = do
174 i <- get
175 j <- get
176 return (fromOldTime i j)
177
178 instance Binary TimeDiff where
179 put td = do
180 put ye; put mo; put da; put ho; put mi; put se; put ps
181 where (ye, mo, da, ho, mi, se, ps) = toOldTimeDiff td
182 get = do
183 ye <- get
184 mo <- get
185 da <- get
186 ho <- get
187 mi <- get
188 se <- get
189 ps <- get
190 return (fromOldTimeDiff ye mo da ho mi se ps)
191
192