tzone: Read or Set Time Zone for Date-Time Indices

View source: R/tzone.R

tzoneR Documentation

Read or Set Time Zone for Date-Time Indices

Description

Date-time indices (objects of tind class of type "t") always have the time zone attribute set. The time zone setting affects how time (measured relative to the Unix epoch in UTC) is translated to local time. Objects of base POSIXct and POSIXlt classes also have an optional time zone attribute. tzone method is also implemented for some other classes supported by the tind package.

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

Usage

tzone(x)

tzone(x) <- value

## S3 method for class 'tind'
tzone(x)

## S3 replacement method for class 'tind'
tzone(x) <- value

## S3 method for class 'tinterval'
tzone(x)

## S3 replacement method for class 'tinterval'
tzone(x) <- value

## S3 method for class 'POSIXct'
tzone(x)

## S3 replacement method for class 'POSIXct'
tzone(x) <- value

## S3 method for class 'POSIXlt'
tzone(x)

## S3 replacement method for class 'POSIXlt'
tzone(x) <- value

Arguments

x

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

value

a character value, the new time zone attribute.

Details

If the provided name is not in the list of supported time zones, an attempt is made to identify it via approximate match. If the result is a single time zone, it is accepted with a warning.

Unambiguous city names are automatically recognised, see Examples.

An attempt to set time zone attribute of a time index of different type than date-time ("t") will lead to an error. In case of POSIXct/POSIXlt objects with no time zone attribute, the extractor returns the system time zone.

Value

For the extractor, the time zone as a character value (or NULL for classes without time zone attribute). For the replacement, the argument with the modified time zone.

See Also

as.tzone.

Examples

# check time in the system time zone
(nw <- now())
# get time zone
tzone(nw)
# set time zone to UTC
tzone(nw) <- "UTC"
nw
tzone(nw)
# check time in different time zones
if ("Asia/Tokyo" %in% OlsonNames()) {
tzone(nw) <- "Asia/Tokyo"
nw
}
if ("Europe/Warsaw" %in% OlsonNames()) {
tzone(nw) <- "Europe/Warsaw"
nw
}
if ("America/New_York" %in% OlsonNames()) {
tzone(nw) <- "America/New_York"
nw
}
# try invalid time zone => error
try(
tzone(nw) <- "Hasdfg/Qwerty"
)
# unambiguous city names are automatically recognised
tzone(nw) <- "Tokyo"
nw
tzone(nw) <- "Warsaw"
nw
tzone(nw) <- "New York"
nw
# incomplete names and approximate matches are also recognised with a warning
if ("Europe/Warsaw" %in% OlsonNames()) try({
tzone(nw) <- "Warsa"
nw
})
if ("America/New_York" %in% OlsonNames()) try({
tzone(nw) <- "NewYork"
nw
})

# list first 6 supported time zones using base::OlsonNames
head(OlsonNames())
# list first 6 supported time zones with string "Europe"
head(grep("Europe", OlsonNames(), value = TRUE))
# list first 6 supported time zones with string "Asia"
head(grep("Asia", OlsonNames(), value = TRUE))
# list first 6 supported time zones with string "Africa"
head(grep("Africa", OlsonNames(), value = TRUE))
# list first 6 supported time zones with string "America"
head(grep("America", OlsonNames(), value = TRUE))


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