Skip to main content

string_to_datetime

Function string_to_datetime 

pub fn string_to_datetime<T>(
    timezone: &T,
    s: &str,
) -> Result<DateTime<T>, ArrowError>
where T: TimeZone,
Expand description

Accepts a string and parses it relative to the provided timezone

In addition to RFC3339 / ISO8601 standard timestamps, it also accepts strings that use a space to separate the date and time as well as strings that have no explicit timezone offset.

Examples of accepted inputs:

  • 1997-01-31T09:26:56.123Z # RCF3339
  • 1997-01-31T09:26:56.123-05:00 # RCF3339
  • 1997-01-31T09:26Z # ISO8601 without seconds
  • 1997-01-31 09:26:56.123-05:00 # close to RCF3339 but with a space rather than T
  • 2023-01-01 04:05:06.789 -08 # close to RCF3339, no fractional seconds or time separator
  • 1997-01-31T09:26:56.123 # close to RCF3339 but no timezone offset specified
  • 1997-01-31 09:26:56.123 # close to RCF3339 but uses a space and no timezone offset
  • 1997-01-31 09:26:56 # close to RCF3339, no fractional seconds
  • 1997-01-31 092656 # close to RCF3339, no fractional seconds
  • 1997-01-31 092656+04:00 # close to RCF3339, no fractional seconds or time separator
  • 1997-01-31 # close to RCF3339, only date no time

IANA timezones are only supported if the arrow-array/chrono-tz feature is enabled

  • 2023-01-01 040506 America/Los_Angeles

A timestamp with no explicit offset is read as a wall clock time in the relevant timezone, which for an IANA timezone does not always identify a unique instant. Daylight savings transitions are resolved the same way PostgreSQL and DuckDB resolve them: an ambiguous reading (the repeated hour when the clocks go back) takes the later instant, and a nonexistent reading (the skipped hour when the clocks go forward) is shifted forward by the length of the gap.

// 02:30 does not exist in America/New_York on 2024-03-10; it is read as 03:30 EDT
let gap = string_to_datetime(&Utc, "2024-03-10 02:30:00 America/New_York").unwrap();
assert_eq!(gap.to_rfc3339(), "2024-03-10T07:30:00+00:00");
// 01:30 happens twice on 2024-11-03; the later (EST) instant is used
let ambiguous = string_to_datetime(&Utc, "2024-11-03 01:30:00 America/New_York").unwrap();
assert_eq!(ambiguous.to_rfc3339(), "2024-11-03T06:30:00+00:00");

Some formats supported by PostgresSql https://www.postgresql.org/docs/current/datatype-datetime.html#DATATYPE-DATETIME-TIME-TABLE are not supported, like

  • “2023-01-01 04:05:06.789 +07:30:00”,
  • “2023-01-01 040506 +07:30:00”,
  • “2023-01-01 04:05:06.789 PST”,