Data.Time.Format

time-1.9.2: A time library

Safe Haskell | Safe
Language | Haskell2010

Data.Time.Format

Contents

- UNIX-style formatting
- UNIX-style parsing
- Locale

Synopsis

- class FormatTime t
- formatTime :: FormatTime t => TimeLocale -> String -> t -> String
- parseTimeM :: (MonadFail m, ParseTime t) => Bool -> TimeLocale -> String -> String -> m t
- parseTimeOrError :: ParseTime t => Bool -> TimeLocale -> String -> String -> t
- readSTime :: ParseTime t => Bool -> TimeLocale -> String -> ReadS t
- readPTime :: ParseTime t => Bool -> TimeLocale -> String -> ReadP t
- parseTime :: ParseTime t => TimeLocale -> String -> String -> Maybe t
- readTime :: ParseTime t => TimeLocale -> String -> String -> t
- readsTime :: ParseTime t => TimeLocale -> String -> ReadS t
- class ParseTime t
- data TimeLocale = TimeLocale {
  - wDays :: [(String, String)]
  - months :: [(String, String)]
  - amPm :: (String, String)
  - dateTimeFmt, dateFmt, timeFmt, time12Fmt :: String
  - knownTimeZones :: [TimeZone]
  }
- defaultTimeLocale :: TimeLocale
- iso8601DateFormat :: Maybe String -> String
- rfc822DateFormat :: String

# UNIX-style formatting

class FormatTime t Source #

Minimal complete definition

formatCharacter

#### Instances

Instances details

FormatTime CalendarDiffDays # |
Instance details Defined in Data.Time.Format.Format.Instances Methods formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> CalendarDiffDays -> String) Source #
FormatTime Day # |
Instance details Defined in Data.Time.Format.Format.Instances Methods formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> Day -> String) Source #
FormatTime DayOfWeek # |
Instance details Defined in Data.Time.Format.Format.Instances Methods formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> DayOfWeek -> String) Source #
FormatTime DiffTime # |
Instance details Defined in Data.Time.Format.Format.Instances Methods formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> DiffTime -> String) Source #
FormatTime NominalDiffTime # |
Instance details Defined in Data.Time.Format.Format.Instances Methods formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> NominalDiffTime -> String) Source #
FormatTime UTCTime # |
Instance details Defined in Data.Time.Format.Format.Instances Methods formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> UTCTime -> String) Source #
FormatTime UniversalTime # |
Instance details Defined in Data.Time.Format.Format.Instances Methods formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> UniversalTime -> String) Source #
FormatTime CalendarDiffTime # |
Instance details Defined in Data.Time.Format.Format.Instances Methods formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> CalendarDiffTime -> String) Source #
FormatTime TimeZone # |
Instance details Defined in Data.Time.Format.Format.Instances Methods formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> TimeZone -> String) Source #
FormatTime TimeOfDay # |
Instance details Defined in Data.Time.Format.Format.Instances Methods formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> TimeOfDay -> String) Source #
FormatTime LocalTime # |
Instance details Defined in Data.Time.Format.Format.Instances Methods formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> LocalTime -> String) Source #
FormatTime ZonedTime # |
Instance details Defined in Data.Time.Format.Format.Instances Methods formatCharacter :: Bool -> Char -> Maybe (FormatOptions -> ZonedTime -> String) Source #

formatTime :: FormatTime t => TimeLocale -> String -> t -> String Source #

Substitute various time-related information for each %-code in the string, as per `formatCharacter`.

The general form is `%<modifier><width><alternate><specifier>`, where `<modifier>`, `<width>`, and `<alternate>` are optional.

## `<modifier>`

glibc-style modifiers can be used before the specifier (here marked as `z`):

`%-z`
  no padding
`%_z`
  pad with spaces
`%0z`
  pad with zeros
`%^z`
  convert to upper case
`%#z`
  convert to lower case (consistently, unlike glibc)

## `<width>`

Width digits can also be used after any modifiers and before the specifier (here marked as `z`), for example:

`%4z`
  pad to 4 characters (with default padding character)
`%_12z`
  pad with spaces to 12 characters

## `<alternate>`

An optional `E` character indicates an alternate formatting. Currently this only affects `%Z` and `%z`.

`%Ez`
  alternate formatting

## `<specifier>`

For all types (note these three are done by `formatTime`, not by `formatCharacter`):

`%%`
  `%`
`%t`
  tab
`%n`
  newline

### `TimeZone`

For `TimeZone` (and `ZonedTime` and `UTCTime`):

`%z`
  timezone offset in the format `±HHMM`
`%Ez`
  timezone offset in the format `±HH:MM`
`%Z`
  timezone name (or else offset in the format `±HHMM`)
`%EZ`
  timezone name (or else offset in the format `±HH:MM`)

### `LocalTime`

For `LocalTime` (and `ZonedTime` and `UTCTime` and `UniversalTime`):

`%c`
  as `dateTimeFmt` `locale` (e.g. `%a %b %e %H:%M:%S %Z %Y`)

### `TimeOfDay`

For `TimeOfDay` (and `LocalTime` and `ZonedTime` and `UTCTime` and `UniversalTime`):

`%R`
  same as `%H:%M`
`%T`
  same as `%H:%M:%S`
`%X`
  as `timeFmt` `locale` (e.g. `%H:%M:%S`)
`%r`
  as `time12Fmt` `locale` (e.g. `%I:%M:%S %p`)
`%P`
  day-half of day from (`amPm` `locale`), converted to lowercase, `am`, `pm`
`%p`
  day-half of day from (`amPm` `locale`), `AM`, `PM`
`%H`
  hour of day (24-hour), 0-padded to two chars, `00` - `23`
`%k`
  hour of day (24-hour), space-padded to two chars, `0` - `23`
`%I`
  hour of day-half (12-hour), 0-padded to two chars, `01` - `12`
`%l`
  hour of day-half (12-hour), space-padded to two chars, `1` - `12`
`%M`
  minute of hour, 0-padded to two chars, `00` - `59`
`%S`
  second of minute (without decimal part), 0-padded to two chars, `00` - `60`
`%q`
  picosecond of second, 0-padded to twelve chars, `000000000000` - `999999999999`.
`%Q`
  decimal point and fraction of second, up to 12 second decimals, without trailing zeros. For a whole number of seconds, `%Q` omits the decimal point unless padding is specified.

### `UTCTime` and `ZonedTime`

For `UTCTime` and `ZonedTime`:

`%s`
  number of whole seconds since the Unix epoch. For times before the Unix epoch, this is a negative number. Note that in `%s.%q` and `%s%Q` the decimals are positive, not negative. For example, 0.9 seconds before the Unix epoch is formatted as `-1.1` with `%s%Q`.

### `DayOfWeek`

For `DayOfWeek` (and `Day` and `LocalTime` and `ZonedTime` and `UTCTime` and `UniversalTime`):

`%u`
  day of week number for Week Date format, `1` (= Monday) - `7` (= Sunday)
`%w`
  day of week number, `0` (= Sunday) - `6` (= Saturday)
`%a`
  day of week, short form (`snd` from `wDays` `locale`), `Sun` - `Sat`
`%A`
  day of week, long form (`fst` from `wDays` `locale`), `Sunday` - `Saturday`

### `Day`

For `Day` (and `LocalTime` and `ZonedTime` and `UTCTime` and `UniversalTime`):

`%D`
  same as `%m/%d/%y`
`%F`
  same as `%Y-%m-%d`
`%x`
  as `dateFmt` `locale` (e.g. `%m/%d/%y`)
`%Y`
  year, no padding. Note `%0Y` and `%_Y` pad to four chars
`%y`
  year of century, 0-padded to two chars, `00` - `99`
`%C`
  century, no padding. Note `%0C` and `%_C` pad to two chars
`%B`
  month name, long form (`fst` from `months` `locale`), `January` - `December`
`%b`, `%h`
  month name, short form (`snd` from `months` `locale`), `Jan` - `Dec`
`%m`
  month of year, 0-padded to two chars, `01` - `12`
`%d`
  day of month, 0-padded to two chars, `01` - `31`
`%e`
  day of month, space-padded to two chars, `1` - `31`
`%j`
  day of year, 0-padded to three chars, `001` - `366`
`%f`
  century for Week Date format, no padding. Note `%0f` and `%_f` pad to two chars
`%V`
  week of year for Week Date format, 0-padded to two chars, `01` - `53`
`%U`
  week of year where weeks start on Sunday (as `sundayStartWeek`), 0-padded to two chars, `00` - `53`
`%W`
  week of year where weeks start on Monday (as `mondayStartWeek`), 0-padded to two chars, `00` - `53`

## Duration types

The specifiers for `DiffTime`, `NominalDiffTime`, `CalendarDiffDays`, and `CalendarDiffTime` are semantically separate from the other types. Specifiers on negative time differences will generally be negative (think `rem` rather than `mod`).

### `NominalDiffTime` and `DiffTime`

Note that a "minute" of `DiffTime` is simply 60 SI seconds, rather than a minute of civil time. Use `NominalDiffTime` to work with civil time, ignoring any leap seconds.

For `NominalDiffTime` and `DiffTime`:

`%w`
  total whole weeks
`%d`
  total whole days
`%D`
  whole days of week
`%h`
  total whole hours
`%H`
  whole hours of day
`%m`
  total whole minutes
`%M`
  whole minutes of hour
`%s`
  total whole seconds
`%Es`
  total seconds, with decimal point and up to <width> (default 12) decimal places, without trailing zeros. For a whole number of seconds, `%Es` omits the decimal point unless padding is specified.
`%0Es`
  total seconds, with decimal point and <width> (default 12) decimal places.
`%S`
  whole seconds of minute
`%ES`
  seconds of minute, with decimal point and up to <width> (default 12) decimal places, without trailing zeros. For a whole number of seconds, `%ES` omits the decimal point unless padding is specified.
`%0ES`
  seconds of minute as two digits, with decimal point and <width> (default 12) decimal places.

### `CalendarDiffDays`

For `CalendarDiffDays` (and `CalendarDiffTime`):

`%y`
  total years
`%b`
  total months
`%B`
  months of year
`%w`
  total weeks, not including months
`%d`
  total days, not including months
`%D`
  days of week

### `CalendarDiffTime`

For `CalendarDiffTime`:

`%h`
  total hours, not including months
`%H`
  hours of day
`%m`
  total minutes, not including months
`%M`
  minutes of hour
`%s`
  total whole seconds, not including months
`%Es`
  total seconds, not including months, with decimal point and up to <width> (default 12) decimal places, without trailing zeros. For a whole number of seconds, `%Es` omits the decimal point unless padding is specified.
`%0Es`
  total seconds, not including months, with decimal point and <width> (default 12) decimal places.
`%S`
  whole seconds of minute
`%ES`
  seconds of minute, with decimal point and up to <width> (default 12) decimal places, without trailing zeros. For a whole number of seconds, `%ES` omits the decimal point unless padding is specified.
`%0ES`
  seconds of minute as two digits, with decimal point and <width> (default 12) decimal places.

# UNIX-style parsing

parseTimeM Source #

Arguments

:: (MonadFail m, ParseTime t) |
=> Bool | Accept leading and trailing whitespace?
-> TimeLocale | Time locale.
-> String | Format string.
-> String | Input string.
-> m t | Return the time value, or fail if the input could not be parsed using the given format.

Parses a time value given a format string. Supports the same %-codes as `formatTime`, including `%-`, `%_` and `%0` modifiers, however padding widths are not supported. Case is not significant in the input string. Some variations in the input are accepted:

`%z`
  accepts any of `±HHMM` or `±HH:MM`.
`%Z`
  accepts any string of letters, or any of the formats accepted by `%z`.
`%0Y`
  accepts exactly four digits.
`%0G`
  accepts exactly four digits.
`%0C`
  accepts exactly two digits.
`%0f`
  accepts exactly two digits.

For example, to parse a date in YYYY-MM-DD format, while allowing the month and date to have optional leading zeros (notice the `-` modifier used for `%m` and `%d`):

    Prelude Data.Time> parseTimeM True defaultTimeLocale "%Y-%-m-%-d" "2010-3-04" :: Maybe Day
    Just 2010-03-04

parseTimeOrError Source #

Arguments

:: ParseTime t |
=> Bool | Accept leading and trailing whitespace?
-> TimeLocale | Time locale.
-> String | Format string.
-> String | Input string.
-> t | The time value.

Parse a time value given a format string. Fails if the input could not be parsed using the given format. See `parseTimeM` for details.

readSTime Source #

Arguments

:: ParseTime t |
=> Bool | Accept leading whitespace?
-> TimeLocale | Time locale.
-> String | Format string
-> ReadS t |

Parse a time value given a format string. See `parseTimeM` for details.

readPTime Source #

Arguments

:: ParseTime t |
=> Bool | Accept leading whitespace?
-> TimeLocale | Time locale.
-> String | Format string
-> ReadP t |

Parse a time value given a format string. See `parseTimeM` for details.

parseTime Source #

Arguments

:: ParseTime t |
=> TimeLocale | Time locale.
-> String | Format string.
-> String | Input string.
-> Maybe t | The time value, or `Nothing` if the input could not be parsed using the given format.

Deprecated: use "parseTimeM True" instead

readTime Source #

Arguments

:: ParseTime t |
=> TimeLocale | Time locale.
-> String | Format string.
-> String | Input string.
-> t | The time value.

Deprecated: use "parseTimeOrError True" instead

readsTime Source #

Arguments

:: ParseTime t |
=> TimeLocale | Time locale.
-> String | Format string
-> ReadS t |

Deprecated: use "readSTime True" instead

class ParseTime t Source #

The class of types which can be parsed given a UNIX-style time format string.

Minimal complete definition

parseTimeSpecifier, buildTime

#### Instances

Instances details

ParseTime CalendarDiffDays # |
Instance details Defined in Data.Time.Format.Parse.Instances Methods substituteTimeSpecifier :: proxy CalendarDiffDays -> TimeLocale -> Char -> Maybe String Source # parseTimeSpecifier :: proxy CalendarDiffDays -> TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String Source # buildTime :: TimeLocale -> [(Char, String)] -> Maybe CalendarDiffDays Source #
ParseTime Day # |
Instance details Defined in Data.Time.Format.Parse.Instances Methods substituteTimeSpecifier :: proxy Day -> TimeLocale -> Char -> Maybe String Source # parseTimeSpecifier :: proxy Day -> TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String Source # buildTime :: TimeLocale -> [(Char, String)] -> Maybe Day Source #
ParseTime DiffTime # |
Instance details Defined in Data.Time.Format.Parse.Instances Methods substituteTimeSpecifier :: proxy DiffTime -> TimeLocale -> Char -> Maybe String Source # parseTimeSpecifier :: proxy DiffTime -> TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String Source # buildTime :: TimeLocale -> [(Char, String)] -> Maybe DiffTime Source #
ParseTime NominalDiffTime # |
Instance details Defined in Data.Time.Format.Parse.Instances Methods substituteTimeSpecifier :: proxy NominalDiffTime -> TimeLocale -> Char -> Maybe String Source # parseTimeSpecifier :: proxy NominalDiffTime -> TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String Source # buildTime :: TimeLocale -> [(Char, String)] -> Maybe NominalDiffTime Source #
ParseTime UTCTime # |
Instance details Defined in Data.Time.Format.Parse.Instances Methods substituteTimeSpecifier :: proxy UTCTime -> TimeLocale -> Char -> Maybe String Source # parseTimeSpecifier :: proxy UTCTime -> TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String Source # buildTime :: TimeLocale -> [(Char, String)] -> Maybe UTCTime Source #
ParseTime UniversalTime # |
Instance details Defined in Data.Time.Format.Parse.Instances Methods substituteTimeSpecifier :: proxy UniversalTime -> TimeLocale -> Char -> Maybe String Source # parseTimeSpecifier :: proxy UniversalTime -> TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String Source # buildTime :: TimeLocale -> [(Char, String)] -> Maybe UniversalTime Source #
ParseTime CalendarDiffTime # |
Instance details Defined in Data.Time.Format.Parse.Instances Methods substituteTimeSpecifier :: proxy CalendarDiffTime -> TimeLocale -> Char -> Maybe String Source # parseTimeSpecifier :: proxy CalendarDiffTime -> TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String Source # buildTime :: TimeLocale -> [(Char, String)] -> Maybe CalendarDiffTime Source #
ParseTime TimeZone # |
Instance details Defined in Data.Time.Format.Parse.Instances Methods substituteTimeSpecifier :: proxy TimeZone -> TimeLocale -> Char -> Maybe String Source # parseTimeSpecifier :: proxy TimeZone -> TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String Source # buildTime :: TimeLocale -> [(Char, String)] -> Maybe TimeZone Source #
ParseTime TimeOfDay # |
Instance details Defined in Data.Time.Format.Parse.Instances Methods substituteTimeSpecifier :: proxy TimeOfDay -> TimeLocale -> Char -> Maybe String Source # parseTimeSpecifier :: proxy TimeOfDay -> TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String Source # buildTime :: TimeLocale -> [(Char, String)] -> Maybe TimeOfDay Source #
ParseTime LocalTime # |
Instance details Defined in Data.Time.Format.Parse.Instances Methods substituteTimeSpecifier :: proxy LocalTime -> TimeLocale -> Char -> Maybe String Source # parseTimeSpecifier :: proxy LocalTime -> TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String Source # buildTime :: TimeLocale -> [(Char, String)] -> Maybe LocalTime Source #
ParseTime ZonedTime # |
Instance details Defined in Data.Time.Format.Parse.Instances Methods substituteTimeSpecifier :: proxy ZonedTime -> TimeLocale -> Char -> Maybe String Source # parseTimeSpecifier :: proxy ZonedTime -> TimeLocale -> Maybe ParseNumericPadding -> Char -> ReadP String Source # buildTime :: TimeLocale -> [(Char, String)] -> Maybe ZonedTime Source #

# Locale

data TimeLocale Source #

Constructors

TimeLocale |
Fields wDays :: [(String, String)] full and abbreviated week days, starting with Sunday months :: [(String, String)] full and abbreviated months amPm :: (String, String) AM/PM symbols dateTimeFmt, dateFmt, timeFmt, time12Fmt :: String formatting strings knownTimeZones :: [TimeZone] time zones known by name

#### Instances

Instances details

Eq TimeLocale # |
Instance details Defined in Data.Time.Format.Locale Methods (==) :: TimeLocale -> TimeLocale -> Bool # (/=) :: TimeLocale -> TimeLocale -> Bool #
Ord TimeLocale # |
Instance details Defined in Data.Time.Format.Locale Methods compare :: TimeLocale -> TimeLocale -> Ordering # (<) :: TimeLocale -> TimeLocale -> Bool # (<=) :: TimeLocale -> TimeLocale -> Bool # (>) :: TimeLocale -> TimeLocale -> Bool # (>=) :: TimeLocale -> TimeLocale -> Bool # max :: TimeLocale -> TimeLocale -> TimeLocale # min :: TimeLocale -> TimeLocale -> TimeLocale #
Show TimeLocale # |
Instance details Defined in Data.Time.Format.Locale Methods showsPrec :: Int -> TimeLocale -> ShowS Source # show :: TimeLocale -> String Source # showList :: [TimeLocale] -> ShowS Source #

defaultTimeLocale :: TimeLocale Source #

Locale representing American usage.

`knownTimeZones` contains only the ten time-zones mentioned in RFC 822 sec. 5: "UT", "GMT", "EST", "EDT", "CST", "CDT", "MST", "MDT", "PST", "PDT". Note that the parsing functions will regardless parse UTC, single-letter military time-zones, and +HHMM format.

iso8601DateFormat :: Maybe String -> String Source #

Construct format string according to ISO-8601.

The `Maybe String` argument allows to supply an optional time specification. E.g.:

    iso8601DateFormat Nothing            == "%Y-%m-%d"           -- i.e. YYYY-MM-DD
    iso8601DateFormat (Just "%H:%M:%S")  == "%Y-%m-%dT%H:%M:%S"  -- i.e. YYYY-MM-DDTHH:MM:SS

rfc822DateFormat :: String Source #

Format string according to RFC822.