Created
September 17, 2011 01:55
-
-
Save roman/1223527 to your computer and use it in GitHub Desktop.
Code to parse Github dates out of it's API using Aeson
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
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | |
{-# LANGUAGE DeriveDataTypeable #-} | |
{-# LANGUAGE OverloadedStrings #-} | |
module Main where | |
import Data.Aeson (FromJSON(..), Value(..), Result(..)) | |
import Data.Aeson.Types (parse) | |
import Data.Time.Clock (UTCTime) | |
import Data.Time.Format (FormatTime, parseTime) | |
import Data.Typeable (Typeable) | |
import Data.Text (unpack) | |
import System.Locale (defaultTimeLocale) | |
newtype GithubTime | |
= GithubTime { | |
fromGithubTime :: UTCTime | |
} | |
deriving (Eq, Ord, Read, Show, Typeable, FormatTime) | |
-- Date Format: | |
-- "2011/08/10 11:30:58 -0700" | |
instance FromJSON GithubTime where | |
parseJSON (String t) = | |
case parseTime defaultTimeLocale "%Y/%m/%d %T %z" (unpack t) of | |
Just d -> return (GithubTime d) | |
_ -> fail "Could not parse github date" | |
main :: IO () | |
main = | |
print $ result | |
where | |
json = String "2011/08/10 11:30:58 -0700" | |
result = (parse parseJSON json :: Result GithubTime) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment