Created
June 2, 2012 08:21
-
-
Save krdlab/2857311 to your computer and use it in GitHub Desktop.
practice: Data.Time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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