as.tzone: Get the Same Date and Time in a Different Time Zone

View source: R/tzone.R

as.tzoneR Documentation

Get the Same Date and Time in a Different Time Zone

Description

This method allows to determine time index representing the same date and time in a different time zone.

Usage

as.tzone(x, tz)

## S3 method for class 'tind'
as.tzone(x, tz)

## S3 method for class 'POSIXct'
as.tzone(x, tz)

## S3 method for class 'POSIXlt'
as.tzone(x, tz)

## S3 method for class 'tinterval'
as.tzone(x, tz)

Arguments

x

an object of tind class or of POSIXct/POSIXlt classes (or of other class for which the method was implemented).

tz

a character value determining the new time zone. See tzone documentation for information on time zones.

Details

The underlying time (as measured by number of seconds since the Unix epoch in UTC) will change so that the date-time components in the new and old time zones are the same. For tind arguments, if (due to DST time changes or UTC offset changes) date-time indices do not occur in the new time zone, NAs are introduced with a warning. For tinterval arguments, the result is adjusted with a warning, in order not to create open-ended time intervals.

The method is implemented for objects of tind class of type "t" (date-time), objects of tinterval class of type "t" (time intervals), as well as base POSIXct and POSIXlt classes.

List of time zones supported by the particular R installation can be obtained via a call to OlsonNames function.

Value

An object of the same class and length as x with adjusted underlying date-time representation and time zone set to tz.

See Also

tzone method and date_time for construction od date-time indices from its components.

Examples

if (all(c("Europe/Warsaw", "America/New_York") %in% OlsonNames())) {
# check time in one time zone
print(nw <- now(tz = "Europe/Warsaw"))
# the same date-time in a new time zone
print(nw2 <- as.tzone(nw, "America/New_York"))
# note the time difference (equal to the difference of UTC offsets)
# warning on different time zones will be issued
print(suppressWarnings(nw2 - nw))
try(nw2 - nw)
}


tind documentation built on Dec. 28, 2025, 1:06 a.m.