time-exts-2.1.0: Efficient Timestamps

Safe HaskellNone
LanguageHaskell2010

Data.Time.Exts.Parser

Contents

Description

Timestamp parsers and related utilities.

Synopsis

Utilities

type FormatText = Text Source

The format string is composed of various %-codes, each representing time-related information described below.

%%
A literal % character.
%A
The full weekday name according to the current locale.
%a
The abbreviated weekday name according to the current locale.
%B
The full month name according to the current locale.
%b
The abbreviated month name according to the current locale.
%D
Equivalent to %m/%d/%y.
%d
The day of the month as a decimal number (range 01 to 31).
%e
Like %d, the day of the month as a decimal number, but a leading zero is replaced by a space.
%F
Equivalent to %Y-%m-%d (the ISO 8601 date format).
%H
The hour as a decimal number using a 24-hour clock (range 00 to 23).
%h
Equivalent to %b.
%I
The hour as a decimal number using a 12-hour clock (range 01 to 12).
%l
Like %I, the hour as a decimal number using a 12-hour clock, but a leading zero is replaced by a space.
%M
The minute as a decimal number (range 00 to 59).
%m
The month as a decimal number (range 01 to 12).
%P
Like %p, the period of the day according to the current locale, but lowercase.
%p
The period of the day according to the current locale.
%Q
The fraction of the second as a decimal number (range 0 to 999999999999).
%R
Equivalent to %H:%M.
%r
Equivalent to %I:%M:%S %p.
%S
The second as a decimal number (range 00 to 60).
%T
Equivalent to %H:%M:%S.
%Y
The year as a decimal number (range 1970 to 9999).
%y
The year as a decimal number without a century (range 00 to 99).
%Z
The timezone abbreviation.

Parse Unix Timestamps

parseUnixDate :: FormatText -> Text -> Either ParseError UnixDate Source

Parse a Unix date.

>>> parseUnixDate "%A, %B %e, %Y" "Tuesday, March  4, 2014"
Right 2014-03-04

parseUnixTime :: FormatText -> Text -> Either ParseError UnixTime Source

Parse a Unix time.

>>> parseUnixTime "%T" "15:32:19"
Right 15:32:19

parseUnixTimeMillis :: FormatText -> Text -> Either ParseError UnixTimeMillis Source

Parse a Unix time with millisecond granularity.

>>> parseUnixTimeMillis "%I:%M:%S.%Q %p" "09:41:09.313 PM"
Right 21:41:09.313

parseUnixTimeMicros :: FormatText -> Text -> Either ParseError UnixTimeMicros Source

Parse a Unix time with microsecond granularity.

>>> parseUnixTimeMicros "%R:%S.%Q" "03:15:50.513439"
Right 03:15:50.513439

parseUnixTimeNanos :: FormatText -> Text -> Either ParseError UnixTimeNanos Source

Parse a Unix time with nanosecond granularity.

>>> parseUnixTimeNanos "%l:%M:%S.%Q %P" " 1:27:44.001256754 pm"
Right 13:27:44.001256754

parseUnixTimePicos :: FormatText -> Text -> Either ParseError UnixTimePicos Source

Parse a Unix time with picosecond granularity.

>>> parseUnixTimePicos "%T.%Q" "13:09:23.247795919586"
Right 13:09:23.247795919586

parseUnixDateTime :: FormatText -> Text -> Either ParseError UnixDateTime Source

Parse a Unix date and time.

>>> parseUnixDateTime "%FT%TZ" "2014-02-27T11:31:20Z"
Right 2014-02-27 11:31:20

parseUnixDateTimeMillis :: FormatText -> Text -> Either ParseError UnixDateTimeMillis Source

Parse a Unix date and time with millisecond granularity.

>>> parseUnixDateTimeMillis "%a %B %e %I:%M:%S.%Q %p %Y" "Wed March  5 06:53:04.475 PM 2014"
Right 2014-03-05 18:53:04.475

parseUnixDateTimeMicros :: FormatText -> Text -> Either ParseError UnixDateTimeMicros Source

Parse a Unix date and time with microsecond granularity.

>>> parseUnixDateTimeMicros "%D %T.%Q" "03/06/14 17:26:55.148415"
Right 2014-03-06 17:26:55.148415

parseUnixDateTimeNanos :: FormatText -> Text -> Either ParseError UnixDateTimeNanos Source

Parse a Unix date and time with nanosecond granularity.

>>> parseUnixDateTimeNanos "%d.%m.%Y %I:%M:%S.%Q %p" "18.03.2014 07:06:43.774295132 PM"
Right 2014-03-18 19:06:43.774295132

parseUnixDateTimePicos :: FormatText -> Text -> Either ParseError UnixDateTimePicos Source

Parse a Unix date and time with picosecond granularity.

>>> parseUnixDateTimePicos "%FT%T.%QZ" "2014-03-03T17:58:15.916795765305Z"
Right 2014-03-03 17:58:15.916795765305

Parse UTC and Local Timestamps

parseLocalDate :: FormatText -> Text -> Either ParseError LocalDate Source

Parse a local date.

>>> parseLocalDate "%A, %B %e, %Y (%Z)" "Monday, March 17, 2014 (PST)"
Right 2014-03-17 PST

parseLocalDateTime :: FormatText -> Text -> Either ParseError LocalDateTime Source

Parse a local date and time.

>>> parseLocalDateTime "%a %b %e %H:%M:%S %Z %Y" "Fri Mar 14 09:29:53 EST 2014"
Right 2014-03-14 09:29:53 EST

parseLocalDateTimeMillis :: FormatText -> Text -> Either ParseError LocalDateTimeMillis Source

Parse a local date and time with millisecond granularity.

>>> parseLocalDateTimeMillis "%B %e %Y %I:%M:%S.%Q %p %Z" "July  1 2012 01:59:60.215 AM EET"
Right 2012-07-01 01:59:60.215 EET

Note that the timestamp in the example above corresponds to a leap second.

parseLocalDateTimeMicros :: FormatText -> Text -> Either ParseError LocalDateTimeMicros Source

Parse a local date and time with microsecond granularity.

>>> parseLocalDateTimeMicros "%F %T.%Q (%Z)" "2014-03-04 02:45:42.827495 (HKT)"
Right 2014-03-04 02:45:42.827495 HKT

parseLocalDateTimeNanos :: FormatText -> Text -> Either ParseError LocalDateTimeNanos Source

Parse a local date and time with nanosecond granularity.

>>> parseLocalDateTimeNanos "%b. %d, %T.%Q %Z %Y" "Mar. 09, 18:53:55.856423459 UTC 2014"
Right 2014-03-09 18:53:55.856423459 UTC

parseLocalDateTimePicos :: FormatText -> Text -> Either ParseError LocalDateTimePicos Source

Parse a local date and time with picosecond granularity.

>>> parseLocalDateTimePicos "%d.%m.%Y %T.%Q %Z" "09.04.2014 05:22:56.587234905781 SGT"
Right 2014-04-09 05:22:56.587234905781 SGT

Parse Unix Timestamps With Parameters

parseUnixDate' :: TimeLocale -> FormatText -> Text -> Either ParseError UnixDate Source

Same as parseUnixDate, except takes an additional locale parameter.

>>> let german = defaultTimeLocale { wDays = [("Sonntag","So"),("Montag","Mo")...
>>> parseUnixDate' german "%A, %B %e, %Y" "Dienstag, März  4, 2014"
Right 2014-03-04

parseUnixTime' :: TimeLocale -> FormatText -> Text -> Either ParseError UnixTime Source

Same as parseUnixTime, except takes an additional locale parameter.

>>> let albanian = defaultTimeLocale { wDays = [("e diel","Die"),("e hënë ","Hën")...
>>> parseUnixTime' albanian "%l:%M:%S %p" "12:28:47 PD"
Right 00:28:47

parseUnixTimeMillis' :: TimeLocale -> FormatText -> Text -> Either ParseError UnixTimeMillis Source

Same as parseUnixTimeMillis, except takes an additional locale parameter.

>>> let urdu = defaultTimeLocale { wDays = [("پير","پير"),("اتوار","اتوار")...
>>> parseUnixTimeMillis' urdu "%l:%M:%S.%Q %p" " 3:12:47.624 ش"
Right 15:12:47.624

parseUnixTimeMicros' :: TimeLocale -> FormatText -> Text -> Either ParseError UnixTimeMicros Source

Same as parseUnixTimeMicros, except takes an additional locale parameter.

>>> let chinese = defaultTimeLocale { wDays = [("星期日","日"),("星期一","一")...
>>> parseUnixTimeMicros' chinese "%p%I:%M:%S.%Q" "下午11:46:18.130561"
Right 23:46:18.130561

parseUnixTimeNanos' :: TimeLocale -> FormatText -> Text -> Either ParseError UnixTimeNanos Source

Same as parseUnixTimeNanos, except takes an additional locale parameter.

>>> let swahili = defaultTimeLocale { wDays = [("Jumapili","J2"),("Jumatatu","J3")...
>>> parseUnixTimeNanos' swahili "%H:%M:%S.%Q %p" "12:05:50.547621324 asubuhi"
Right 00:05:50.547621324

parseUnixTimePicos' :: TimeLocale -> FormatText -> Text -> Either ParseError UnixTimePicos Source

Same as parseUnixTimePicos, except takes an additional locale parameter.

>>> let japanese = defaultTimeLocale { wDays = [("日曜日","日"),("月曜日","月")...
>>> parseUnixTimePicos' japanese "%I:%M:%S.%Q %p" "04:20:15.340563315063 午前"
Right 04:20:15.340563315063

parseUnixDateTime' :: TimeLocale -> FormatText -> Text -> Either ParseError UnixDateTime Source

Same as parseUnixDateTime, except takes an additional locale parameter.

>>> let somali = defaultTimeLocale { wDays = [("Axad","Axa"),("Isniin","Isn")...
>>> parseUnixDateTime' somali "%A, %B %e, %r %Y" "Salaaso, Bisha Saddexaad 11, 03:41:33 galabnimo 2014"
Right 2014-03-11 15:41:33

parseUnixDateTimeMillis' :: TimeLocale -> FormatText -> Text -> Either ParseError UnixDateTimeMillis Source

Same as parseUnixDateTimeMillis, except takes an additional locale parameter.

>>> let turkish = defaultTimeLocale { wDays = [("Pazar","Paz"),("Pazartesi","Pzt")...
>>> parseUnixDateTimeMillis' turkish "%a %B %e %I:%M:%S.%Q %p %Y" "Prş Mart 13 07:22:54.324 ÖS 2014"
Right 2014-03-13 19:22:54.324

parseUnixDateTimeMicros' :: TimeLocale -> FormatText -> Text -> Either ParseError UnixDateTimeMicros Source

Same as parseUnixDateTimeMicros, except takes an additional locale parameter.

>>> let angika = defaultTimeLocale { wDays = [("रविवार","रवि"),("सोमवार","सोम")...
>>> parseUnixDateTimeMicros' angika "%A %d %B %Y %I:%M:%S.%Q %p" "शुक्रवार 07 मार्च 2014 07:10:50.283025 अपराह्न"
Right 2014-03-07 19:10:50.283025

parseUnixDateTimeNanos' :: TimeLocale -> FormatText -> Text -> Either ParseError UnixDateTimeNanos Source

Same as parseUnixDateTimeNanos, except takes an additional locale parameter.

>>> let russian = defaultTimeLocale { wDays = [("Воскресенье","Вс"),("Понедельник","Пн")...
>>> parseUnixDateTimeNanos' russian "%a %d %b %Y %T.%Q" "Ср 11 дек 2013 22:17:42.146648836"
Right 2013-12-11 22:17:42.146648836

parseUnixDateTimePicos' :: TimeLocale -> FormatText -> Text -> Either ParseError UnixDateTimePicos Source

Same as parseUnixDateTimePicos, except takes an additional locale parameter.

>>> let norwegian = defaultTimeLocale { wDays = [("søndag","sø."),("mandag","ma.")...
>>> parseUnixDateTimePicos' norwegian "%a %d. %b %Y kl. %H.%M.%S.%Q"  "fr. 07. mars 2014 kl. 21.11.55.837472109433"
Right 2014-03-07 21:11:55.837472109433

Parse UTC and Local Timestamps With Parameters

parseLocalDate' :: TimeLocale -> City -> FormatText -> Text -> Either ParseError LocalDate Source

Same as parseLocalDate, except takes an additional locale and city parameter.

>>> parseLocalDate' defaultTimeLocale Kolkata "%A, %B %e, %Y (%Z)" "Monday, March 17, 2014 (IST)"
Right 2014-03-17 IST

Note that the city parameter is required to distinguish between the India and Israel.

parseLocalDateTime' :: TimeLocale -> City -> FormatText -> Text -> Either ParseError LocalDateTime Source

Same as parseLocalDateTime, except takes an additional locale and city parameter.

>>> let french = defaultTimeLocale { wDays = [("dimanche","dim."),("lundi","lun.")...
>>> parseLocalDateTime' french Paris "%a %d %b %T %Z %Y" "ven. 07 mars 22:49:03 UTC 2014"
Right 2014-03-07 22:49:03 UTC

parseLocalDateTimeMillis' :: TimeLocale -> City -> FormatText -> Text -> Either ParseError LocalDateTimeMillis Source

Same as parseLocalDateTimeMillis, except takes an additional locale and city parameter.

>>> parseLocalDateTimeMillis' defaultTimeLocale Chicago "%B %e %Y %I:%M:%S.%Q %p %Z" "July 13 2013 12:15:30.985 AM CDT"
Right 2013-07-13 00:15:30.985 CDT

Note that the city parameter is required to distinguish between the United States and China.

parseLocalDateTimeMicros' :: TimeLocale -> City -> FormatText -> Text -> Either ParseError LocalDateTimeMicros Source

Same as parseLocalDateTimeMicros, except takes an additional locale and city parameter.

>>> let spanish = defaultTimeLocale { wDays = [("domingo","dom"),("lunes","lun")...
>>> parseLocalDateTimeMicros' spanish Paris "%a %d %b %I:%M:%S.%Q %P %Y %Z" "dom 26 ene 04:27:16.743312 pm 2014 CET"
Right 2014-01-26 16:27:16.743312 CET

parseLocalDateTimeNanos' :: TimeLocale -> City -> FormatText -> Text -> Either ParseError LocalDateTimeNanos Source

Same as parseLocalDateTimeNanos, except takes an additional locale and city parameter.

>>> let italian = defaultTimeLocale { wDays = [("domenica","dom"),("lunedì","lun")...
>>> parseLocalDateTimeNanos' italian Paris "%a %e %b %Y %T.%Q %Z" "sab 12 apr 2014 04:59:21.528207540 CEST"
Right 2014-04-12 04:59:21.528207540 CEST

parseLocalDateTimePicos' :: TimeLocale -> City -> FormatText -> Text -> Either ParseError LocalDateTimePicos Source

Same as parseLocalDateTimePicos, except takes an additional locale and city parameter.

>>> parseLocalDateTimePicos' defaultTimeLocale Shanghai "%a %b %d %Y %T.%Q %Z" "Sat Mar 08 2014 22:51:47.264356423524 CST"
Right 2014-03-08 22:51:47.264356423524 CST

Note that the city parameter is required to distinguish between the United States and China.