R/clock.R

Defines functions clock

Documented in clock

#' emoji version of time
#'
#' @param time a `POSIXct` object
#'
#' @details
#' This function is vectorized.
#'
#' @return Character vector of emojis showing the closest time.
#' @export
#'
#' @examples
#' times <- as.POSIXct("2021-09-17 14:33:21 PDT") + seq(1:30) * 3500
#' clock(times)
clock <- function(time){
  hour   <- as.numeric(format(time, "%I"))
  minute <- as.numeric(format(time, "%M"))

  x <- round(1 + 2 * hour %% 12 + minute / 30) %% 24

  idx <- which(
    emoji::emojis$subgroup == "time" &
    grepl("(o.clock|thirty)", emoji::emojis$name)
  )

  emoji::emojis[idx[x], ]$emoji
}

Try the emoji package in your browser

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

emoji documentation built on Oct. 30, 2024, 9:28 a.m.