on master help sign in
Fix instance of ToJSON for ZonedTime
dc8de16Bryan O'Sullivan
Data/Aeson/Types/Instances.hs+6−5
@@ -66,7 +66,7 @@ import Data.Monoid (Dual(..), First(..), Last(..), mappend)
66import Data.Ratio (Ratio, (%), numerator, denominator)
66import Data.Ratio (Ratio, (%), numerator, denominator)
67import Data.Text (Text, pack, unpack)
67import Data.Text (Text, pack, unpack)
68import Data.Time (UTCTime, ZonedTime(..), TimeZone(..))
68import Data.Time (UTCTime, ZonedTime(..), TimeZone(..))
69−import Data.Time.Format (formatTime, parseTime)
69+import Data.Time.Format (FormatTime, formatTime, parseTime)
70import Data.Traversable (traverse)
70import Data.Traversable (traverse)
71import Data.Vector (Vector)
71import Data.Vector (Vector)
72import Data.Word (Word, Word8, Word16, Word32, Word64)
72import Data.Word (Word, Word8, Word16, Word32, Word64)
@@ -454,9 +454,8 @@ instance FromJSON Value where
454
454
455instance ToJSON DotNetTime where
455instance ToJSON DotNetTime where
456 toJSON (DotNetTime t) =
456 toJSON (DotNetTime t) =
457− String (pack (secs ++ msecs ++ ")/"))
457+ String (pack (secs ++ formatMillis t ++ ")/"))
458 where secs = formatTime defaultTimeLocale "/Date(%s" t
458 where secs = formatTime defaultTimeLocale "/Date(%s" t
459− msecs = take 3 $ formatTime defaultTimeLocale "%q" t
460 {-# INLINE toJSON #-}
459 {-# INLINE toJSON #-}
461
460
462instance FromJSON DotNetTime where
461instance FromJSON DotNetTime where
@@ -471,12 +470,14 @@ instance FromJSON DotNetTime where
471instance ToJSON ZonedTime where
470instance ToJSON ZonedTime where
472 toJSON t = String $ pack $ formatTime defaultTimeLocale format t
471 toJSON t = String $ pack $ formatTime defaultTimeLocale format t
473 where
472 where
474− format = "%FT%T" ++ milliseconds ++ tzFormat
473+ format = "%FT%T." ++ formatMillis t ++ tzFormat
475− milliseconds = take 4 $ formatTime defaultTimeLocale "%Q" t
476 tzFormat
474 tzFormat
477 | 0 == timeZoneMinutes (zonedTimeZone t) = "Z"
475 | 0 == timeZoneMinutes (zonedTimeZone t) = "Z"
478 | otherwise = "%z"
476 | otherwise = "%z"
479
477
478+formatMillis :: (FormatTime t) => t -> String
479+formatMillis t = take 3 . formatTime defaultTimeLocale "%q" $ t
480+
480instance FromJSON ZonedTime where
481instance FromJSON ZonedTime where
481 parseJSON (String t) =
482 parseJSON (String t) =
482 tryFormats alternateFormats
483 tryFormats alternateFormats