Skip to content

Instantly share code, notes, and snippets.

@krdlab
Created June 2, 2012 08:21
Show Gist options
  • Save krdlab/2857311 to your computer and use it in GitHub Desktop.
Save krdlab/2857311 to your computer and use it in GitHub Desktop.
practice: Data.Time
import System.Locale (defaultTimeLocale)
import Data.Time
-- 時刻をフォーマット
formatISO8601 :: ZonedTime -> String
formatISO8601 t = formatTime defaultTimeLocale "%FT%T%z" t
-- 時刻を表した文字列のパース (今回は Twitter の created_at)
parseCreatedAt :: String -> Maybe UTCTime
parseCreatedAt s = parseTime defaultTimeLocale "%a %b %d %H:%M:%S %z %Y" s
-- お試し
main :: IO ()
main = do
-- formatting
zt <- getZonedTime
putStrLn $ formatISO8601 zt
-- parsing
let ct = "Sat Jun 02 07:31:54 +0000 2012" -- twitter の created_at
case parseCreatedAt ct of
Just t -> do
tz <- getCurrentTimeZone
putStrLn . show $ utcToZonedTime tz t
Nothing -> putStrLn "parse error"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment