Skip to content

Instantly share code, notes, and snippets.

@ateucher
Last active June 26, 2018 00:21
Show Gist options
  • Save ateucher/86d8d5795cd8ed0b8155ee0f147fc507 to your computer and use it in GitHub Desktop.
Save ateucher/86d8d5795cd8ed0b8155ee0f147fc507 to your computer and use it in GitHub Desktop.
get_tz_list <- function() {
big_list <- lapply(OlsonNames(), function(tz) {
dates <- as.POSIXlt(seq(as.POSIXlt("2000-01-01 12:00:00", tz = tz),
as.POSIXlt("2000-12-31 12:00:00", tz = tz),
by = "1 day"))
gmt_offset <- dates$gmtoff
tz_code <- dates$zone
is_dst <- c(NA, FALSE, TRUE)[dates$isdst + 2]
gmt_offset[is.null(gmt_offset)] <- 0L
tz_code[is.null(tz_code)] <- NA_character_
# Possibly get start and end dates of DST?
unique(data.frame(tz,
tz_code,
is_dst = is_dst,
gmt_offset_h = gmt_offset / 3600,
stringsAsFactors = FALSE))
})
do.call("rbind", big_list)
}
olson_names <- OlsonNames()
get_tz_list <- function() {
yr <- format(Sys.Date(), "%Y")
big_list <- lapply(olson_names, function(tz) {
dates <- as.POSIXlt(seq(as.POSIXct(paste0(yr,"-01-01"), tz = tz),
as.POSIXct(paste0(yr,"-12-31"), tz = tz),
by = "1 day"))
offs <- get_tz_offset(tz, as.character(dates))
unique(offs[, setdiff(names(offs), "date")])
})
do.call("rbind", big_list)
}
get_tz_offset <- function(tz, dates) {
if (!tz %in% olson_names) {
stop("tz is not a valid timezone. See ?OlsonNames")
}
if (!is.character(dates)) {
stop("'dates' must be a charcter vector of dates in YYYY-MM-DD format")
}
dates <- as.POSIXlt(as.POSIXct(dates, tz = tz))
gmt_offset <- dates$gmtoff
zone <- dates$zone
is_dst <- c(NA, FALSE, TRUE)[dates$isdst + 2]
gmt_offset[is.null(gmt_offset)] <- 0L
zone[is.null(zone)] <- NA_character_
data.frame(tz_name = tz,
date = dates,
zone,
is_dst = is_dst,
gmt_offset_h = gmt_offset / 3600,
stringsAsFactors = FALSE)
}
get_tz_list()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment