on master help sign in
Merge pull request #275 from lpsmith/year-fourdigits

Ensure that years are encoded with at least 4 digits

commite9e983cb5a12b272b6bd56a6745a70d94cefef61 authorBryan O'Sullivan <bos@serpentine.com> dateWed 22 Jul 2015, 20:47 (11 years ago) parent8ab095928cd427
1 file changed+9−1
Data/Aeson/Encode/Builder.hs+9−1
Data/Aeson/Encode/Builder.hs+9−1
@@ -169,11 +169,19 @@ ascii8 cs = BP.liftFixedToBounded $ (const cs) >$<
169169{-# INLINE ascii8 #-}
170170
171171day :: Day -> Builder
172−day dd = B.integerDec y <>
172+day dd = encodeYear y <>
173173 BP.primBounded (ascii6 ('-',(mh,(ml,('-',(dh,dl)))))) ()
174174 where (y,m,d) = toGregorian dd
175175 !(T mh ml) = twoDigits m
176176 !(T dh dl) = twoDigits d
177+ encodeYear y
178+ | y >= 1000 = B.integerDec y
179+ | y > 0 =
180+ let (ab,c) = fromIntegral y `quotRem` 10
181+ (a,b) = ab `quotRem` 10
182+ in BP.primBounded (ascii4 ('0',(digit a,(digit b,digit c)))) ()
183+ | otherwise =
184+ error "Data.Aeson.Encode.Builder.day: years BCE not supported"
177185{-# INLINE day #-}
178186
179187timeOfDay :: TimeOfDay -> Builder