Learn R Programming

datetimeoffset (version 1.0.0)

datetime_at_tz: Change time zones while preserving UTC time

Description

datetime_at_tz() changes time zones while preserving UTC time (instead of clock time).

Usage

datetime_at_tz(x, tz = "", ...)

# S3 method for datetimeoffset datetime_at_tz( x, tz = "", ..., ambiguous = "error", nonexistent = "error", fill = NA_character_ )

# S3 method for clock_zoned_time datetime_at_tz(x, tz = "", ...)

# S3 method for POSIXt datetime_at_tz(x, tz = "", ...)

# S3 method for default datetime_at_tz(x, tz = "", ...)

Value

A datetime object. The UTC time should be the same but with a different time zone.

Arguments

x

A datetime object.

tz

The target timezone to change to.

...

Ignored

ambiguous

What to do when the "clock time" in the new time zone is ambiguous. See clock::as_zoned_time.clock_naive_time().

nonexistent

What to do when the "clock time" in the new time zone doesn't exist. See clock::as_zoned_time.clock_naive_time().

fill

If timezone and UTC offset info is missing what timezone to assume. See fill_tz().

See Also

set_tz() changes time zones while preserving clock time (instead of UTC time).

Examples

Run this code
if(all(c("America/Los_Angeles", "America/New_York") %in% OlsonNames())) {
  dt0 <- as_datetimeoffset("2020-01-01T01:01[America/Los_Angeles]")
  dt <- datetime_at_tz(dt0, "America/New_York")
  print(dt)
  dt <- datetime_at_tz(as.POSIXct(dt0), "America/New_York")
  print(dt)
  dt <- datetime_at_tz(clock::as_zoned_time(dt0), "America/New_York")
  print(dt)

  # Can also use `lubridate::with_tz()`
  if (requireNamespace("lubridate")) {
    dt <- lubridate::with_tz(dt0, "America/New_York")
    print(dt)
  }
}

Run the code above in your browser using DataLab