Debian Codegraph @

Network/HTTP/Date/Converter.hs

raw
1 {-# LANGUAGE BangPatterns #-}
2 module Network.HTTP.Date.Converter ( epochTimeToHTTPDate
3 , httpDateToUTC
4 , utcToHTTPDate
5 ) where
6
7 import Data.Time
8 import Data.Time.Calendar.WeekDate
9 import Data.Word
10 import Foreign.Marshal.Array
11 import Foreign.Ptr
12 import Foreign.Storable
13 import Network.HTTP.Date.Types
14 import System.IO.Unsafe (unsafeDupablePerformIO, unsafePerformIO)
15 import System.Posix.Types
16
17 {-|
18 Translating 'EpochTime' to 'HTTPDate'.
19 -}
20 epochTimeToHTTPDate :: EpochTime -> HTTPDate
21 epochTimeToHTTPDate x = defaultHTTPDate {
22 hdYear = y
23 , hdMonth = m
24 , hdDay = d
25 , hdHour = h
26 , hdMinute = n
27 , hdSecond = s
28 , hdWkday = w
29 }
30 where
31 w64 :: Word64
32 w64 = fromIntegral $ fromEnum x
33 (days',secs') = w64 `quotRem` 86400
34 days = fromIntegral days'
35 secs = fromIntegral secs'
36 -- 1970/1/1 is Thu (4)
37 w = (days + 3) `rem` 7 + 1
38 (y,m,d) = toYYMMDD days
39 (h,n,s) = toHHMMSS secs
40
41 -- | Translating 'HTTPDate' to 'UTCTime'.
42 --
43 -- Since 0.0.7.
44 httpDateToUTC :: HTTPDate -> UTCTime
45 httpDateToUTC x = UTCTime (fromGregorian y m d) (secondsToDiffTime s)
46 where
47 y = fromIntegral $ hdYear x
48 m = hdMonth x
49 d = hdDay x
50 s = fromIntegral $ (hdHour x `rem` 24) * 3600
51 + (hdMinute x `rem` 60) * 60
52 + (hdSecond x `rem` 60)
53
54 -- | Translating 'UTCTime' to 'HTTPDate'.
55 --
56 -- Since 0.0.7.
57 utcToHTTPDate :: UTCTime -> HTTPDate
58 utcToHTTPDate x = defaultHTTPDate {
59 hdYear = fromIntegral y
60 , hdMonth = m
61 , hdDay = d
62 , hdHour = h
63 , hdMinute = n
64 , hdSecond = truncate s
65 , hdWkday = fromEnum (w :: Int)
66 }
67 where
68 (y, m, d) = toGregorian day
69 (h, n, s) = ((todHour tod), (todMin tod), (todSec tod))
70 (_, _, w) = toWeekDate day
71 day = localDay time
72 tod = localTimeOfDay time
73 time = utcToLocalTime utc x
74
75 toYYMMDD :: Int -> (Int,Int,Int)
76 toYYMMDD x = (yy, mm, dd)
77 where
78 (y,d) = x `quotRem` 365
79 cy = 1970 + y
80 cy' = cy - 1
81 leap = cy' `quot` 4 - cy' `quot` 100 + cy' `quot` 400 - 477
82 (yy,days) = adjust cy d leap
83 (mm,dd) = findMonth days
84 adjust !ty td aj
85 | td >= aj = (ty, td - aj)
86 | isLeap (ty - 1) = if td + 366 >= aj
87 then (ty - 1, td + 366 - aj)
88 else adjust (ty - 1) (td + 366) aj
89 | otherwise = if td + 365 >= aj
90 then (ty - 1, td + 365 - aj)
91 else adjust (ty - 1) (td + 365) aj
92 isLeap year = year `rem` 4 == 0
93 && (year `rem` 400 == 0 ||
94 year `rem` 100 /= 0)
95 (mnths, daysArr) = if isLeap yy
96 then (leapMonth, leapDayInMonth)
97 else (normalMonth, normalDayInMonth)
98 findMonth n = unsafeDupablePerformIO $ (,) <$> (peekElemOff mnths n) <*> (peekElemOff daysArr n)
99
100 ----------------------------------------------------------------
101
102 normalMonthDays :: [Int]
103 normalMonthDays = [31,28,31,30,31,30,31,31,30,31,30,31]
104
105 leapMonthDays :: [Int]
106 leapMonthDays = [31,29,31,30,31,30,31,31,30,31,30,31]
107
108 mkPtrInt :: [Int] -> Ptr Int
109 mkPtrInt = unsafePerformIO . newArray . concat . zipWith (flip replicate) [1..]
110
111 mkPtrInt2 :: [Int] -> Ptr Int
112 mkPtrInt2 = unsafePerformIO . newArray . concatMap (enumFromTo 1)
113
114 normalMonth :: Ptr Int
115 normalMonth = mkPtrInt normalMonthDays
116
117 normalDayInMonth :: Ptr Int
118 normalDayInMonth = mkPtrInt2 normalMonthDays
119
120 leapMonth :: Ptr Int
121 leapMonth = mkPtrInt leapMonthDays
122
123 leapDayInMonth :: Ptr Int
124 leapDayInMonth = mkPtrInt2 leapMonthDays
125
126 ----------------------------------------------------------------
127
128 toHHMMSS :: Int -> (Int,Int,Int)
129 toHHMMSS x = (hh,mm,ss)
130 where
131 (hhmm,ss) = x `quotRem` 60
132 (hh,mm) = hhmm `quotRem` 60