change_time: Change time and time zone (without changing time display).

View source: R/time_fun.R

change_timeR Documentation

Change time and time zone (without changing time display).

Description

change_time changes the time and time zone without changing the time display.

Usage

change_time(time, tz = "")

Arguments

time

Time (as a scalar or vector). If time is not a local time (of the "POSIXlt" class) the function first tries coercing time into "POSIXlt" without changing the time display.

tz

Time zone (as character string). Default: tz = "" (i.e., current system time zone, Sys.timezone()). See OlsonNames() for valid options.

Details

change_time expects inputs to time to be local time(s) (of the "POSIXlt" class) and a valid time zone argument tz (as a string) and returns the same time display (but different actual times) as calendar time(s) (of the "POSIXct" class).

Value

A calendar time of class "POSIXct".

See Also

change_tz function which preserves time but changes time display; Sys.time() function of base R.

Other date and time functions: change_tz(), cur_date(), cur_time(), days_in_month(), diff_dates(), diff_times(), diff_tz(), is_leap_year(), what_date(), what_month(), what_time(), what_wday(), what_week(), what_year(), zodiac()

Examples

change_time(as.POSIXlt(Sys.time()), tz = "UTC")

# from "POSIXlt" time:
t1 <- as.POSIXlt("2020-01-01 10:20:30", tz = "Europe/Berlin")
change_time(t1, "Pacific/Auckland")
change_time(t1, "America/Los_Angeles")

# from "POSIXct" time:
tc <- as.POSIXct("2020-07-01 12:00:00", tz = "UTC")
change_time(tc, "Pacific/Auckland")

# from "Date":
dt <- as.Date("2020-12-31", tz = "Pacific/Honolulu")
change_time(dt, tz = "Pacific/Auckland")

# from time "string":
ts <- "2020-12-31 20:30:45"
change_time(ts, tz = "America/Los_Angeles")

# from other "string" times:
tx <- "7:30:45"
change_time(tx, tz = "Asia/Calcutta")
ty <- "1:30"
change_time(ty, tz = "Europe/London")

# convert into local times:
(l1 <- as.POSIXlt("2020-06-01 10:11:12"))
change_tz(change_time(l1, "Pacific/Auckland"), tz = "UTC")
change_tz(change_time(l1, "Europe/Berlin"), tz = "UTC")
change_tz(change_time(l1, "America/New_York"), tz = "UTC")

# with vector of "POSIXlt" times:
(l2 <- as.POSIXlt("2020-12-31 23:59:55", tz = "America/Los_Angeles"))
(tv <- c(l1, l2))              # uses tz of l1
change_time(tv, "America/Los_Angeles")  # change time and tz
 

ds4psy documentation built on Sept. 15, 2023, 9:08 a.m.