packages feed

htsn-import-0.0.6: src/TSN/Picklers.hs

-- | (Un)picklers for data types present in The Sports Network XML
--   feed.
--
module TSN.Picklers (
  xp_date,
  xp_date_padded,
  xp_datetime,
  xp_earnings,
  xp_gamedate,
  xp_tba_time,
  xp_time,
  xp_time_dots,
  xp_time_stamp )
where

-- System imports.
import Data.List ( intercalate )
import Data.List.Split ( chunksOf )
import Data.String.Utils ( replace )
import Data.Time.Clock ( NominalDiffTime, UTCTime, addUTCTime )
import Data.Time.Format ( formatTime, parseTime )
import System.Locale ( defaultTimeLocale )
import Text.XML.HXT.Arrow.Pickle (
  xpText,
  xpWrap,
  xpWrapMaybe )
import Text.XML.HXT.Arrow.Pickle.Xml ( PU )

-- Local imports.
import TSN.Parse (
  parse_time_stamp,
  time_format,
  time_stamp_format )


-- | The format string for a base date in m/d/yyyy format. The
--   day/month are not padded at all. This will match for example,
--
--   * 2\/15\/1983
--
--   * 1\/1\/0000
--
date_format :: String
date_format = "%-m/%-d/%Y"


-- | The format string for a base date in mm/dd/yyyy format. The
--   day/month are padded to two characters with zeros. This will
--   match for example,
--
--   * 02\/15\/1983
--
--   * 01\/01\/0000
--
date_format_padded :: String
date_format_padded = "%0m/%0d/%Y"


-- | (Un)pickle a UTCTime without the time portion.
--
xp_date :: PU UTCTime
xp_date =
  (to_date, from_date) `xpWrapMaybe` xpText
  where
    to_date :: String -> Maybe UTCTime
    to_date = parseTime defaultTimeLocale date_format

    from_date :: UTCTime -> String
    from_date = formatTime defaultTimeLocale date_format


-- | (Un)pickle a UTCTime without the time portion. The day/month are
--   padded to two characters with zeros.
--
xp_date_padded :: PU UTCTime
xp_date_padded =
  (to_date, from_date) `xpWrapMaybe` xpText
  where
    to_date :: String -> Maybe UTCTime
    to_date = parseTime defaultTimeLocale date_format_padded

    from_date :: UTCTime -> String
    from_date = formatTime defaultTimeLocale date_format_padded



-- | Format a number as a string using a comma as the thousands
--   separator.
--
--   Examples:
--
--   >>> format_commas 0
--   "0"
--   >>> format_commas 10
--   "10"
--   >>> format_commas 100
--   "100"
--   >>> format_commas 1000
--   "1,000"
--   >>> format_commas 10000
--   "10,000"
--   >>> format_commas 100000
--   "100,000"
--   >>> format_commas 1000000
--   "1,000,000"
--
format_commas :: Int -> String
format_commas x =
  reverse (intercalate "," $ chunksOf 3 $ reverse $ show x)

-- | Parse \<Earnings\> from an 'AutoRaceResultsListing'. These are
--   essentially 'Int's, but they look like,
--
--   * \<Earnings\>336,826\</Earnings\>
--
--   * \<Earnings\>1,000,191\</Earnings\>
--
--   * \<Earnings\>TBA\</Earnings\>
--
xp_earnings :: PU (Maybe Int)
xp_earnings =
  (to_earnings, from_earnings) `xpWrap` xpText
  where
    strip_commas :: String -> String
    strip_commas = replace "," ""

    to_earnings :: String -> Maybe Int
    to_earnings s
      | s == "TBA" = Nothing
      | otherwise = Just  $ (read . strip_commas) s

    from_earnings :: Maybe Int -> String
    from_earnings Nothing = "TBA"
    from_earnings (Just i) = format_commas i


-- | (Un)pickle an unpadded 'UTCTime'. Used for example on the
--   \<RaceDate\> elements in an 'AutoRaceResults' message.
--
--   Examples:
--
--   * \<RaceDate\>6/1/2014 1:00:00 PM\</RaceDate\>
--
--   * \<RaceDate\>5/24/2014 2:45:00 PM\</RaceDate\>
--
xp_datetime :: PU UTCTime
xp_datetime =
  (to_datetime, from_datetime) `xpWrapMaybe` xpText
  where
    format = date_format ++ " " ++ "%-I:%M:%S %p"

    to_datetime :: String -> Maybe UTCTime
    to_datetime = parseTime defaultTimeLocale format

    from_datetime :: UTCTime -> String
    from_datetime = formatTime defaultTimeLocale format


-- | (Un)pickle a UTCTime from a weather forecast's gamedate. Example
--   input looks like,
--
--   \<forecast gamedate=\"Monday, December 30th\"\>
--
--   When unpickling we get rid of the suffixes \"st\", \"nd\", \"rd\", and
--   \"th\". During pickling, we add them back based on the last digit
--   of the date.
--
xp_gamedate :: PU UTCTime
xp_gamedate =
  (to_gamedate, from_gamedate) `xpWrapMaybe` xpText
  where
    format = "%A, %B %-d"

    to_gamedate :: String -> Maybe UTCTime
    to_gamedate s =
      parseTime defaultTimeLocale format s'
      where
        s' = case (reverse s) of
               (c2:c1:cs) -> let suffix = [c1,c2]
                             in
                               case suffix of
                                 "st" -> reverse cs
                                 "nd" -> reverse cs
                                 "rd" -> reverse cs
                                 "th" -> reverse cs
                                 _    -> s -- Unknown suffix, leave it alone.
               _ -> s -- The String is less than two characters long,
                      -- leave it alone.


    from_gamedate :: UTCTime -> String
    from_gamedate d = s ++ (suffix s)
      where
        s = formatTime defaultTimeLocale format d

        suffix :: String -> String
        suffix cs =
          case (reverse cs) of
            []       -> []
            ('1':_) -> "st"
            ('2':_) -> "nd"
            ('3':_) -> "rd"
            _        -> "th"






-- | (Un)pickle a UTCTime without the date portion.
--
xp_time :: PU UTCTime
xp_time =
  (to_time, from_time) `xpWrapMaybe` xpText
  where
    to_time :: String -> Maybe UTCTime
    to_time = parseTime defaultTimeLocale time_format

    from_time :: UTCTime -> String
    from_time = formatTime defaultTimeLocale time_format


-- | (Un)pickle a UTCTime without the date portion. This differs from
--   'xp_time' in that it uses periods in the AM/PM part, i.e. \"A.M.\"
--   and \"P.M.\" It also doesn't use padding for the \"hours\" part.
--
--   Examples:
--
--   * \<CurrentTimeStamp\>11:30 A.M.\</CurrentTimeStamp\>
--
xp_time_dots :: PU UTCTime
xp_time_dots =
  (to_time, from_time) `xpWrapMaybe` xpText
  where
    -- | The hours arent padded with zeros.
    nopad_time_format :: String
    nopad_time_format = "%-I:%M %p"

    to_time :: String -> Maybe UTCTime
    to_time = (parseTime defaultTimeLocale nopad_time_format) . (replace "." "")

    from_time :: UTCTime -> String
    from_time t =
        replace "AM" "A.M." (replace "PM" "P.M." s)
      where
        s = formatTime defaultTimeLocale nopad_time_format t


-- | (Un)pickle a UTCTime without the date portion, allowing for a
--   value of \"TBA\" (which gets translated to 'Nothing').
--
xp_tba_time :: PU (Maybe UTCTime)
xp_tba_time =
  (to_time, from_time) `xpWrap` xpText
  where
    to_time :: String -> Maybe UTCTime
    to_time s
      | s == "TBA" = Nothing
      | otherwise = parseTime defaultTimeLocale time_format s

    from_time :: Maybe UTCTime -> String
    from_time Nothing = "TBA"
    from_time (Just t) = formatTime defaultTimeLocale time_format t



-- | (Un)pickle the \<time_stamp\> element format to/from a 'UTCTime'.
--
--   Example:  \<time_stamp\> January 6, 2014, at 10:11 PM ET \</time_stamp\>
--
--   TSN doesn't provide a proper time zone name, so we assume that
--   it's always Eastern Standard Time. EST is UTC-5, so we
--   add/subtract 5 hours to convert to/from UTC.
--
xp_time_stamp :: PU UTCTime
xp_time_stamp =
  (parse_time_stamp, from_time_stamp) `xpWrapMaybe` xpText
  where
    five_hours :: NominalDiffTime
    five_hours = 5 * 60 * 60

    subtract_five :: UTCTime -> UTCTime
    subtract_five = addUTCTime (-1 * five_hours)

    from_time_stamp :: UTCTime -> String
    from_time_stamp =
      formatTime defaultTimeLocale time_stamp_format . subtract_five