From 796d9e70b9b799af25120f81400b75830ba20318 Mon Sep 17 00:00:00 2001 From: DavisVaughan Date: Wed, 7 Apr 2021 15:32:54 -0400 Subject: [PATCH] Implement `date_now()` and `date_today()` --- NAMESPACE | 2 ++ NEWS.md | 6 +++++ R/date.R | 42 ++++++++++++++++++++++++++++++++ R/posixt.R | 8 +++++++ _pkgdown.yml | 1 + man/date-today.Rd | 46 ++++++++++++++++++++++++++++++++++++ tests/testthat/test-date.R | 9 +++++++ tests/testthat/test-posixt.R | 9 +++++++ 8 files changed, 123 insertions(+) create mode 100644 man/date-today.Rd diff --git a/NAMESPACE b/NAMESPACE index 585f6c0a..0ce070f4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -563,6 +563,7 @@ export(date_format) export(date_group) export(date_leap_year) export(date_month_factor) +export(date_now) export(date_parse) export(date_round) export(date_set_zone) @@ -571,6 +572,7 @@ export(date_time_build) export(date_time_parse) export(date_time_parse_abbrev) export(date_time_parse_complete) +export(date_today) export(date_weekday_factor) export(date_zone) export(duration_cast) diff --git a/NEWS.md b/NEWS.md index 3579e76e..d41c8e20 100644 --- a/NEWS.md +++ b/NEWS.md @@ -27,6 +27,12 @@ a direct conversion impossible, `nonexistent` and `ambiguous` can be used to resolve any issues. +* Added two new convenient helpers (#197): + + * `date_today()` for getting the current date (Date) + + * `date_now()` for getting the current date-time (POSIXct) + * Fixed a bug where converting from a time point to a Date or POSIXct could round incorrectly (#205). diff --git a/R/date.R b/R/date.R index 7cb66cac..d68892be 100644 --- a/R/date.R +++ b/R/date.R @@ -1086,3 +1086,45 @@ date_build <- function(year, month = 1L, day = 1L, ..., invalid = NULL) { x <- invalid_resolve(x, invalid = invalid) as.Date(x) } + +# ------------------------------------------------------------------------------ + +#' Current date and date-time +#' +#' @description +#' - `date_today()` returns the current date in the specified `zone` as a Date. +#' +#' - `date_now()` returns the current date-time in the specified `zone` as a +#' POSIXct. +#' +#' @details +#' clock assumes that Date is a _naive_ type, like naive-time. This means that +#' `date_today()` first looks up the current date-time in the specified `zone`, +#' then converts that to a Date, retaining the printed time while dropping any +#' information about that time zone. +#' +#' @inheritParams zoned_time_now +#' +#' @return +#' - `date_today()` a single Date. +#' +#' - `date_now()` a single POSIXct. +#' +#' @name date-today +#' +#' @examples +#' # Current date in the local time zone +#' date_today("") +#' +#' # Current date in a specified time zone +#' date_today("Europe/London") +#' +#' # Current date-time in that same time zone +#' date_now("Europe/London") +NULL + +#' @rdname date-today +#' @export +date_today <- function(zone) { + as.Date(zoned_time_now(zone)) +} diff --git a/R/posixt.R b/R/posixt.R index 60916f80..ab2167a5 100644 --- a/R/posixt.R +++ b/R/posixt.R @@ -1276,3 +1276,11 @@ date_time_build <- function(year, x <- invalid_resolve(x, invalid = invalid) as.POSIXct(x, tz = zone, nonexistent = nonexistent, ambiguous = ambiguous) } + +# ------------------------------------------------------------------------------ + +#' @rdname date-today +#' @export +date_now <- function(zone) { + as.POSIXct(zoned_time_now(zone)) +} diff --git a/_pkgdown.yml b/_pkgdown.yml index 6b727978..e9d5cee8 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -13,6 +13,7 @@ reference: contents: - date_build - date_time_build + - date-today - date_group - date-and-date-time-rounding - date-and-date-time-shifting diff --git a/man/date-today.Rd b/man/date-today.Rd new file mode 100644 index 00000000..878efeb4 --- /dev/null +++ b/man/date-today.Rd @@ -0,0 +1,46 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/date.R, R/posixt.R +\name{date-today} +\alias{date-today} +\alias{date_today} +\alias{date_now} +\title{Current date and date-time} +\usage{ +date_today(zone) + +date_now(zone) +} +\arguments{ +\item{zone}{\verb{[character(1)]} + +A time zone to get the current time for.} +} +\value{ +\itemize{ +\item \code{date_today()} a single Date. +\item \code{date_now()} a single POSIXct. +} +} +\description{ +\itemize{ +\item \code{date_today()} returns the current date in the specified \code{zone} as a Date. +\item \code{date_now()} returns the current date-time in the specified \code{zone} as a +POSIXct. +} +} +\details{ +clock assumes that Date is a \emph{naive} type, like naive-time. This means that +\code{date_today()} first looks up the current date-time in the specified \code{zone}, +then converts that to a Date, retaining the printed time while dropping any +information about that time zone. +} +\examples{ +# Current date in the local time zone +date_today("") + +# Current date in a specified time zone +date_today("Europe/London") + +# Current date-time in that same time zone +date_now("Europe/London") +} diff --git a/tests/testthat/test-date.R b/tests/testthat/test-date.R index dbc81eb6..eb68bf97 100644 --- a/tests/testthat/test-date.R +++ b/tests/testthat/test-date.R @@ -311,6 +311,15 @@ test_that("can handle invalid dates", { ) }) +# ------------------------------------------------------------------------------ +# date_today() + +test_that("can get the current date", { + x <- date_today("America/New_York") + expect_length(x, 1) + expect_s3_class(x, "Date") +}) + # ------------------------------------------------------------------------------ # date_zone() diff --git a/tests/testthat/test-posixt.R b/tests/testthat/test-posixt.R index 5609ab14..2be43797 100644 --- a/tests/testthat/test-posixt.R +++ b/tests/testthat/test-posixt.R @@ -389,6 +389,15 @@ test_that("can handle ambiguous times", { ) }) +# ------------------------------------------------------------------------------ +# date_now() + +test_that("can get the current date-time", { + x <- date_now("America/New_York") + expect_length(x, 1) + expect_s3_class(x, "POSIXct") +}) + # ------------------------------------------------------------------------------ # vec_arith()