Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
42 changes: 42 additions & 0 deletions R/date.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
8 changes: 8 additions & 0 deletions R/posixt.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions man/date-today.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions tests/testthat/test-date.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-posixt.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down