R/utc.R

Defines functions localTime currentTime utc0 utc

Documented in currentTime localTime utc utc0

#' Compute Coordinated Universal Time (UTC/GMT) for Your Local Time.
#'
#' @export

utc <- function() {
  as.POSIXlt(as.numeric(Sys.time()), origin = "1970-01-01", tz = "GMT")
}

#' Compute Coordinated Universal Time (UTC/GMT) for Specified Local Time.
#'
#' @param date Character. Date "yyyy-mm-dd".
#' @param time Character. Local time "hh:mm" or "hh:mm:ss".
#' @param tz Character. Local time zone. See OlsonNames() or use Sys.timezone().
#' @export

utc0 <- function(date = "2020-01-01", time = "12:00:00", tz = "Europe/Vienna") {
  local.date <- as.Date(date, optional = TRUE)
  if (is.na(local.date)) {
    stop('Invalid date or format "yyyy-mm-dd".', call. = FALSE)
  }
  x <- dateTime(date = local.date, time = time, tz = tz)
  as.POSIXlt(as.numeric(x), origin = "1970-01-01", tz = "GMT")
}

#' Compute Current Time in Selected Time Zone.
#'
#' @param tz Character. Local time zone. See OlsonNames() or use Sys.timezone().
#' @export

currentTime <- function(tz = "Australia/Sydney") {
  as.POSIXct(as.numeric(Sys.time()), origin = "1970-01-01", tz = tz)
}

#' Compute Local Time from Coordinated Universal Time (UTC/GMT).
#'
#' @param date Character. Date "yyyy-mm-dd".
#' @param time Character. Local time "hh:mm" or "hh:mm:ss".
#' @param tz Character. Local time zone. See OlsonNames() or use Sys.timezone().
#' @export

localTime <- function(date = "2021-1-1", time = "12:00", tz = Sys.timezone()) {
  local.date <- as.Date(date, optional = TRUE)
  if (is.na(local.date)) {
    stop('Invalid date or format "yyyy-mm-dd".', call. = FALSE)
  }
  x <- dateTime(local.date, time)
  as.POSIXlt(as.numeric(x), origin = "1970-01-01", tz = tz)
}

Try the packageRank package in your browser

Any scripts or data that you put into this service are public.

packageRank documentation built on Nov. 10, 2023, 1:07 a.m.