@@ -2,17 +2,17 @@
22-----------------------------------------------------------------------------
33-- |
44-- Module : Data.Time.LocalTime.TimeZone.Olson.Render
5−-- Copyright : Yitzchak Gale 2010
5+-- Copyright : Yitzchak Gale 2018
66--
77-- Maintainer : Yitzchak Gale <gale@sefer.org>
88-- Portability : portable
99--
10−-- Render Olson timezone data in the standard binary format as
11−-- used in Olson timezone files, and as specified by the
12−-- tzfile(5) man page on Unix-like systems. For more information
13−-- about this format, see http://www.twinsun.com/tz/tz-link.htm.
10+-- Render Olson timezone data in the standard binary format as used in
11+-- Olson timezone files, and as specified by the tzfile(5) man page on
12+-- Unix-like systems. For more information about this format, see
13+-- <http://www.twinsun.com/tz/tz-link.htm>.
1414
15−{- Copyright (c) 2010 Yitzchak Gale. All rights reserved.
15+{- Copyright (c) 2018ZZ Yitzchak Gale. All rights reserved.
1616For licensing information, see the BSD3-style license in the file
1717LICENSE that was originally distributed by the author together with
1818this file. -}
@@ -28,8 +28,9 @@ module Data.Time.LocalTime.TimeZone.Olson.Render
2828 renderTimeZoneSeriesToOlsonFile,
2929 timeZoneSeriesToOlson,
3030 renderOlsonToFile,
31+ verifyOlsonLimits,
3132 putOlson,
32− splitOlson
33+ extractOlsonV1
3334)
3435where
3536
@@ -46,9 +47,15 @@ import Data.Ord (comparing)
4647import Data.Word (Word8)
4748import Data.Maybe (listToMaybe, maybeToList, isNothing, fromMaybe, catMaybes)
4849import Data.Monoid (mempty)
49−import Control.Monad (guard, replicateM_)
50+import Control.Monad (guard, replicateM_, unless)
5051
5152-- | Render a @TimeZoneSeries@ as a binary Olson timezone file.
53+--
54+-- If the values in the Olson timezone data exceed the standard size
55+-- limits (see 'defaultLimits'), this function throws an
56+-- exception. For other behavior, use 'timeZoneSeriesToOlson',
57+-- 'verifyOlsonLimits', 'putOlson' and 'Data.Binary.Put.runPut'
58+-- directly.
5259renderTimeZoneSeriesToOlsonFile :: FilePath -> TimeZoneSeries -> IO ()
5360renderTimeZoneSeriesToOlsonFile fp = renderOlsonToFile fp .
5461 fromMaybe (error "Cannot render TimeZoneSeries: default is summer time") .
@@ -62,86 +69,93 @@ timeZoneSeriesToOlson (TimeZoneSeries dflt pairs)
6269 | otherwise = Just $
6370 OlsonData
6471 [Transition secs ttinfo |
65− (t, tzs) <- reverse pairs,
72+ (t, tz) <- reverse pairs,
6673 let secs = round $ utcTimeToPOSIXSeconds t,
67− ttinfo <- maybeToList $ lookup (mkTT tzs) ttAssocs]
74+ ttinfo <- maybeToList $ lookup (mkTT tz) ttAssocs]
6875 ttinfos
6976 []
7077 Nothing
7178 where
72− mkTT (TimeZone offset issdst abbr) =
73− TtInfo (offset*60) issdst Wall abbr
79+ mkTT (TimeZone offset isdst abbr) =
80+ TtInfo (offset*60) isdst Wall abbr
7481 dfltTT = mkTT dflt
7582 ttAssocs = (dfltTT, 0) :
7683 zip (uniq . sort . filter (/= dfltTT) $ map (mkTT . snd) pairs) [1..]
7784 ttinfos = map fst ttAssocs
7885
86+-- | Check whether @OlsonData@ is within size limits.
87+verifyOlsonLimits :: SizeLimits -> OlsonData -> Bool
88+verifyOlsonLimits limits (OlsonData transs ttinfos leaps _) =
89+ withinLimit maxTimes transs &&
90+ withinLimit maxTypes ttinfos &&
91+ withinLimit maxLeaps leaps &&
92+ withinLimit maxAbbrChars abbrChars
93+ where
94+ withinLimit limit items = maybe True (null . flip drop items) $
95+ limit limits
96+ abbrChars = concat abbrStrs ++ map (const '\NUL') abbrStrs
97+ abbrStrs = map tt_abbr ttinfos
98+
7999-- | Render Olson timezone data as a binary Olson timezone file
100+--
101+-- If the values in the Olson timezone data exceed the standard size
102+-- limits (see 'defaultLimits'), this function throws an
103+-- exception. For other behavior, use 'verifyOlsonLimits', 'putOlson'
104+-- and 'Data.Binary.Put.runPut' directly.
80105renderOlsonToFile :: FilePath -> OlsonData -> IO ()
81−renderOlsonToFile fp = L.writeFile fp . runPut . putOlson
106+renderOlsonToFile fp olson = do
107+ unless (verifyOlsonLimits defaultLimits olson) $
108+ error "Olson timezone data exceeds size limits"
109+ L.writeFile fp . runPut . putOlson $ olson
82110
83111-- | Render Olson timezone data in binary Olson timezone file format
84112-- as a lazy @ByteString@
85−putOlson :: OlsonData -> Put
86−putOlson olson = putOlsonParts version olson1 olson2 posix >> flush
113+putOlson olson@(OlsonData _ _ _ posix)
114+ | fitsInVersion1 = putOlsonPart 0 put32bitIntegral olson
115+ | otherwise = do putOlsonPart 50 put32bitIntegral olson1
116+ putOlsonPart 50 put64bitIntegral olson
117+ putPosixTZ posix
87118 where
88− (olson1, olson2, posix) = splitOlson olson
89− version
90− | olson2 /= mempty = 50
91− | otherwise = maybe 0 (const 50) $ posix >>= guard . not . null
92−
93−-- | Split Olson timezone data into three parts: timezone data that can
94−-- be rendered using Version 1 format, timezone data that can only be
95−-- rendered using Version 2 format, and the POSIX-style TZ string
96−-- if specified
97−splitOlson :: OlsonData -> (OlsonData, OlsonData, Maybe String)
98−splitOlson (OlsonData transs ttinfos leaps posix) =
99− (OlsonData transs1 ttinfos1 leaps1 Nothing,
100− OlsonData transs2 ttinfos2 leaps2 Nothing,
101− posix)
119+ olson1 = extractOlsonV1 olson
120+ fitsInVersion1 = fmap null posix /= Just False &&
121+ olson1 == olson {olsonPosixTZ = Nothing}
122+
123+-- | Extract Olson timezone data that can be rendered using Version 1 format
124+extractOlsonV1 :: OlsonData -> OlsonData
125+extractOlsonV1 (OlsonData transs ttinfos leaps _)
126+ | allV1 = OlsonData transs ttinfos leaps Nothing
127+ | otherwise = OlsonData transs1 ttinfos1 leaps1 Nothing
102128 where
103129 cutoff = 0x80000000 -- 2^31
104130 fitsIn32bits x = x < cutoff && x >= negate cutoff
105− ( leaps1 , leaps2 ) = partition (fitsIn32bits . leapTime) leaps
106− (transs1', transs2') = partition (fitsIn32bits . transTime) transs
107− assoc1 = mkAssoc [0] transs1'
108− assoc2 = mkAssoc [] transs2'
109− transs1 = mkTranss transs1' assoc1
110− transs2 = mkTranss transs2' assoc2
111− ttinfos1 = mkTtinfos assoc1
112− ttinfos2 = mkTtinfos assoc2
113− mkAssoc prepend transs' = zip
131+ leaps1 = filter (fitsIn32bits . leapTime) leaps
132+ transs1' = filter (fitsIn32bits . transTime) transs
133+ allV1 = length leaps1 == length leaps && length transs1' == length transs
134+ assoc1 = zip
114135 (sortBy (comparing $ fmap tt_ttype . listToMaybe . flip drop ttinfos) .
115− uniq . sort . (prepend ++) $ map transIndex transs')
136+ uniq . sort . (0 :) $ map transIndex transs1')
116137 [0..]
117− mkTranss transs' assoc = [t {transIndex = i} |
118− t <- transs', i <- maybeToList $ lookup (transIndex t) assoc]
119− mkTtinfos assoc = map snd . dropWhile (isNothing . fst) .
120− sortBy (comparing fst) $ zip (map (flip lookup assoc) [0..]) ttinfos
121−
122−putOlsonParts :: Word8 -> OlsonData -> OlsonData -> Maybe String -> Put
123−putOlsonParts 0 olson1 _ _ = putOlsonPart 0 put32bitIntegral olson1
124−putOlsonParts v2 olson1 olson2 posix = do
125− putOlsonPart v2 put32bitIntegral olson1
126− putOlsonPart v2 put64bitIntegral olson2
127− putPosixTZ posix
138+ transs1 = [t {transIndex = i} |
139+ t <- transs1', i <- maybeToList $ lookup (transIndex t) assoc1]
140+ ttinfos1 = map snd . dropWhile (isNothing . fst) .
141+ sortBy (comparing fst) $ zip (map (flip lookup assoc1) [0..]) ttinfos
128142
129143putOlsonPart :: Word8 -> (Integer -> Put) -> OlsonData -> Put
130144putOlsonPart version putTime (OlsonData transs ttinfos leaps _) = do
131− putASCII "TZif"
145+ putASCII "magic number" "TZif"
132146 putWord8 version
133147 putByteString . B.pack $ replicate 15 0 -- padding nulls
134148 replicateM_ 2 $ putCount ttinfosWithTtype
135− -- tzh_ttisgmtcnt
136− -- tzh_ttisstdcnt
137− putCount leaps -- tzh_leapcnt
138− putCount transs -- tzh_timecnt
139− putCount ttinfos -- tzh_typecnt
140− putCount abbrChars -- tzh_charcnt
149+ -- tzh_ttisgmtcnt
150+ -- tzh_ttisstdcnt
151+ putCount leaps -- tzh_leapcnt
152+ putCount transs -- tzh_timecnt
153+ putCount ttinfos -- tzh_typecnt
154+ put32bitIntegral abbrChars -- tzh_charcnt
141155 mapM_ (putTime . transTime ) transs
142156 mapM_ (put8bitIntegral . transIndex) transs
143157 mapM_ putTtInfo ttinfosIndexed
144− putASCII abbrChars
158+ mapM_ putAbbr abbrStrings
145159 mapM_ (putLeapInfo putTime) leaps
146160 mapM_ (putBool . (== Std) . tt_ttype) ttinfosWithTtype -- isstd
147161 mapM_ (putBool . (== UTC) . tt_ttype) ttinfosWithTtype -- isgmt
@@ -149,7 +163,8 @@ putOlsonPart version putTime (OlsonData transs ttinfos leaps _) = do
149163 putCount = put32bitIntegral . length
150164 ttinfosWithTtype = takeWhile ((<= UTC) . tt_ttype) ttinfosIndexed
151165 abbrStrings = uniq . sort $ map tt_abbr ttinfos
152− abbrChars = concatMap (++ "\NUL") abbrStrings
166+ abbrChars = sum (map length abbrStrings) + length abbrStrings
167+ putAbbr abbr = putASCII "time zone abbreviation" abbr >> putWord8 0
153168 abbrAssocs = zip abbrStrings . scanl (+) 0 $
154169 map ((+ 1) . length) abbrStrings
155170 ttinfosIndexed = [TtInfo gmtoff isdst ttype i |
@@ -159,7 +174,7 @@ putOlsonPart version putTime (OlsonData transs ttinfos leaps _) = do
159174putPosixTZ :: Maybe String -> Put
160175putPosixTZ posix = do
161176 putWord8 10
162− putASCII $ fromMaybe "" posix
177+ putASCII "POSIX TZ string"$ fromMaybe "" posix
163178 putWord8 10
164179
165180putTtInfo :: TtInfo Int -> Put
@@ -192,5 +207,11 @@ putBool True = putWord8 1
192207uniq :: Eq a => [a] -> [a]
193208uniq = map head . group
194209
195−putASCII :: String -> Put
196−putASCII = putByteString . B.pack . map (fromIntegral . fromEnum)
210+putASCII :: String -> String -> Put
211+putASCII what =
212+ putByteString . B.pack . map (fromIntegral . verify . fromEnum)
213+ where
214+ verify c
215+ | c >= 32 && c <= 126 = c
216+ | otherwise = error $ "Cannot render TimeZoneSeries: " ++
217+ what ++ " contains non-ASCII characters"