iso8601-time-0.1.5/src/0000755000000000000000000000000012564751154012551 5ustar0000000000000000iso8601-time-0.1.5/src/Data/0000755000000000000000000000000012564751154013422 5ustar0000000000000000iso8601-time-0.1.5/src/Data/Time/0000755000000000000000000000000012564753056014323 5ustar0000000000000000iso8601-time-0.1.5/test/0000755000000000000000000000000012564751154012741 5ustar0000000000000000iso8601-time-0.1.5/src/Data/Time/ISO8601.hs0000644000000000000000000000552112564753013015624 0ustar0000000000000000{-# LANGUAGE CPP #-} module Data.Time.ISO8601 ( formatISO8601 , formatISO8601Millis , formatISO8601Micros , formatISO8601Nanos , formatISO8601Picos , formatISO8601Javascript , parseISO8601 ) where import Data.Time.Clock (UTCTime) import Data.Time.Format (formatTime) #if MIN_VERSION_time(1,5,0) import Data.Time.Format (defaultTimeLocale, parseTimeM) #else import Data.Time.Format (parseTime) import System.Locale (defaultTimeLocale) #endif import Control.Applicative ((<|>)) -- | Formats a time in ISO 8601, with up to 12 second decimals. -- -- This is the `formatTime` format @%FT%T%Q@ == @%%Y-%m-%dT%%H:%M:%S%Q@. formatISO8601 :: UTCTime -> String formatISO8601 t = formatTime defaultTimeLocale "%FT%T%QZ" t -- | Pads an ISO 8601 date with trailing zeros, but lacking the trailing Z. -- -- This is needed because `formatTime` with "%Q" does not create trailing zeros. formatPadded :: UTCTime -> String formatPadded t | length str == 19 = str ++ ".000000000000" | otherwise = str ++ "000000000000" where str = formatTime defaultTimeLocale "%FT%T%Q" t -- | Formats a time in ISO 8601 with up to millisecond precision and trailing zeros. -- The format is precisely: -- -- >YYYY-MM-DDTHH:mm:ss.sssZ formatISO8601Millis :: UTCTime -> String formatISO8601Millis t = take 23 (formatPadded t) ++ "Z" -- | Formats a time in ISO 8601 with up to microsecond precision and trailing zeros. -- The format is precisely: -- -- >YYYY-MM-DDTHH:mm:ss.ssssssZ formatISO8601Micros :: UTCTime -> String formatISO8601Micros t = take 26 (formatPadded t) ++ "Z" -- | Formats a time in ISO 8601 with up to nanosecond precision and trailing zeros. -- The format is precisely: -- -- >YYYY-MM-DDTHH:mm:ss.sssssssssZ formatISO8601Nanos :: UTCTime -> String formatISO8601Nanos t = take 29 (formatPadded t) ++ "Z" -- | Formats a time in ISO 8601 with up to picosecond precision and trailing zeros. -- The format is precisely: -- -- >YYYY-MM-DDTHH:mm:ss.ssssssssssssZ formatISO8601Picos :: UTCTime -> String formatISO8601Picos t = take 32 (formatPadded t) ++ "Z" -- | Formats a time like JavaScript's @new Date().toISOString()@ -- as specified by Mozilla: -- -- This is an alias for `formatISO8601Millis`. formatISO8601Javascript :: UTCTime -> String formatISO8601Javascript = formatISO8601Millis -- | Parses an ISO 8601 string. -- -- Leading and trailing whitespace is accepted. See `parseTimeM` from the -- `time` package for more details. parseISO8601 :: String -> Maybe UTCTime #if MIN_VERSION_time(1,5,0) parseISO8601 t = parseTimeM True defaultTimeLocale "%FT%T%QZ" t <|> parseTimeM True defaultTimeLocale "%FT%T%Q%z" t #else parseISO8601 t = parseTime defaultTimeLocale "%FT%T%QZ" t <|> parseTime defaultTimeLocale "%FT%T%Q%z" t #endif iso8601-time-0.1.5/test/Main.hs0000644000000000000000000000112712564751154014162 0ustar0000000000000000import Test.Hspec import Data.Time.ISO8601 main :: IO () main = hspec $ do let readTest str date = parseISO8601 str `shouldBe` (Just date) describe "parseISO8601" $ do it "time zone formats" $ do readTest "2014-03-28T10:26:00Z" (read "2014-03-28 10:26:00 UTC") readTest "2014-03-28T10:26:00-0700" (read "2014-03-28 17:26:00 UTC") readTest "2014-03-28T10:26:00+0700" (read "2014-03-28 03:26:00 UTC") readTest "2014-03-28T10:26:00-07:00" (read "2014-03-28 17:26:00 UTC") readTest "2014-03-28T10:26:00+07:00" (read "2014-03-28 03:26:00 UTC") iso8601-time-0.1.5/Setup.hs0000644000000000000000000000005613307507726013417 0ustar0000000000000000import Distribution.Simple main = defaultMain iso8601-time-0.1.5/iso8601-time.cabal0000644000000000000000000000261113307507726015013 0ustar0000000000000000name: iso8601-time version: 0.1.5 license: MIT copyright: 2013 Niklas Hambüchen author: Niklas Hambüchen maintainer: Niklas Hambüchen category: Time build-type: Simple stability: experimental tested-With: GHC==7.6.3 cabal-version: >= 1.8 homepage: https://github.com/nh2/iso8601-time bug-Reports: https://github.com/nh2/iso8601-time/issues synopsis: Convert to/from the ISO 8601 time format description: Conversion functions between Haskell time types and the ISO 8601 format, which is often used for printing times, e.g. JavaScript's @new Date().toISOString()@. source-repository head type: git location: git://github.com/nh2/iso8601-time.git flag new-time default: True library exposed-modules: Data.Time.ISO8601 hs-source-dirs: src build-depends: base < 5 , time >= 1.4 if flag(new-time) build-depends: time >= 1.5 else build-depends: time == 1.4.* , old-locale >= 1.0 ghc-options: -Wall test-Suite tests type: exitcode-stdio-1.0 hs-source-dirs: test main-is: Main.hs build-depends: base >= 4 && < 5 , iso8601-time , hspec >= 1.3.0.1 , HUnit >= 1.2 , time >= 1.4 ghc-options: -Wall