diff_tz: Get the time zone difference between two times.

View source: R/time_util_fun.R

diff_tzR Documentation

Get the time zone difference between two times.

Description

diff_tz computes the time difference between two times t1 and t2 that is exclusively due to both times being in different time zones.

Usage

diff_tz(t1, t2, in_min = FALSE)

Arguments

t1

First time (required, as "POSIXt" time point/moment).

t2

Second time (required, as "POSIXt" time point/moment).

in_min

Return time-zone based time difference in minutes (Boolean)? Default: in_min = FALSE.

Details

diff_tz ignores all differences in nominal times, but allows adjusting time-based computations for time shifts that are due to time zone differences (e.g., different locations, or changes to/from daylight saving time, DST), rather than differences in actual times.

Internally, diff_tz determines and contrasts the POSIX conversion specifications " (in numeric form).

If the lengths of t1 and t2 differ, the shorter vector is recycled to the length of the longer one.

Value

A character (in "HH:MM" format) or numeric vector (number of minutes).

See Also

days_in_month for the number of days in given months; is_leap_year to check for leap years.

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

Examples

# Time zones differences:
tm <- "2020-01-01 01:00:00"  # nominal time
t1 <- as.POSIXct(tm, tz = "Pacific/Auckland")
t2 <- as.POSIXct(tm, tz = "Europe/Berlin")
t3 <- as.POSIXct(tm, tz = "Pacific/Honolulu")

# as character (in "HH:MM"):
diff_tz(t1, t2)
diff_tz(t2, t3)
diff_tz(t1, t3)

# as numeric (in minutes):
diff_tz(t1, t3, in_min = TRUE)

# Compare local times (POSIXlt): 
t4 <- as.POSIXlt(Sys.time(), tz = "Pacific/Auckland")
t5 <- as.POSIXlt(Sys.time(), tz = "Europe/Berlin")
diff_tz(t4, t5)
diff_tz(t4, t5, in_min = TRUE)

# DSL shift: Spring ahead (on 2020-03-29: 02:00:00 > 03:00:00):
s6 <- "2020-03-29 01:00:00 CET"   # before DSL switch
s7 <- "2020-03-29 03:00:00 CEST"  # after DSL switch
t6 <- as.POSIXct(s6, tz = "Europe/Berlin")  # CET
t7 <- as.POSIXct(s7, tz = "Europe/Berlin")  # CEST

diff_tz(t6, t7)  # 1 hour forward
diff_tz(t6, t7, in_min = TRUE)


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