R/time.R

#'@title Consistent ways to get system times and dates
#'@description R has Sys.Date() and Sys.time(), an incredible pair of functions. Well,
#'mostly they're incredible because despite being written by the /same group of people/
#'they're not even consistently cased. What we actually mean is \code{today} or
#'\code{now}, so let's just call it that. Also, we sometimes want to work out what this
#'time was N days ago, ("Oliver, find me all of X in the last 30 days")
#'so let's include a function that does that.
#'
#'@param days how many days back you want the timestamp to be offset from the current
#'date and time.
#'
#'@return a Date or POSIX object
#'
#'@seealso \code{\link{iftime}} and \code{\link{ifdate}} for time-based conditional
#'handlers.
#'
#'@rdname timehandlers
#'@aliases timehandlers
#'@export
today <- function(){
  Sys.Date()
}

#'@rdname timehandlers
#'@export
now <- function(){
  Sys.time()
}

#'@rdname timehandlers
#'@export
days_ago <- function(days){
  days <- (days)
  return(today()-days)
}

#'@title convert to and from common timestamp formats
#'@description convert to and from MediaWiki and request log timestamp formats
#'
#'@param x a vector of timestamps
#'
#'@name timeconverters
#'@rdname timeconverters
#'
#'@export
from_mediawiki <- function(x){
  return(strptime(substr(x, 0, 14), format = "%Y%m%d%H%M%S", tz = "UTC"))
}

#'@rdname timeconverters
#'@export
from_requestlog <- function(x){
  return(strptime(substr(iconv(x, to = "UTF-8"), 0, 19), format = "%Y-%m-%dT%H:%M:%S", tz = "UTC"))
}

#'@rdname timeconverters
#'@export
to_mediawiki <- function(x){
  fast_remove(x, "(:| |-)")
}

#'@rdname timeconverters
#'@export
to_requestlog <- function(x){
  fixed_substitute(x, " ", "T")
}
Ironholds/olivr documentation built on May 7, 2019, 6:40 a.m.