Skip to content

Commit 0b246ba

Browse files
authored
Implement date_now() and date_today() (#210)
1 parent c8a97c5 commit 0b246ba

File tree

8 files changed

+123
-0
lines changed

8 files changed

+123
-0
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ export(date_format)
563563
export(date_group)
564564
export(date_leap_year)
565565
export(date_month_factor)
566+
export(date_now)
566567
export(date_parse)
567568
export(date_round)
568569
export(date_set_zone)
@@ -571,6 +572,7 @@ export(date_time_build)
571572
export(date_time_parse)
572573
export(date_time_parse_abbrev)
573574
export(date_time_parse_complete)
575+
export(date_today)
574576
export(date_weekday_factor)
575577
export(date_zone)
576578
export(duration_cast)

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
a direct conversion impossible, `nonexistent` and `ambiguous` can be used
2828
to resolve any issues.
2929

30+
* Added two new convenient helpers (#197):
31+
32+
* `date_today()` for getting the current date (Date)
33+
34+
* `date_now()` for getting the current date-time (POSIXct)
35+
3036
* Fixed a bug where converting from a time point to a Date or POSIXct could
3137
round incorrectly (#205).
3238

R/date.R

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,3 +1086,45 @@ date_build <- function(year, month = 1L, day = 1L, ..., invalid = NULL) {
10861086
x <- invalid_resolve(x, invalid = invalid)
10871087
as.Date(x)
10881088
}
1089+
1090+
# ------------------------------------------------------------------------------
1091+
1092+
#' Current date and date-time
1093+
#'
1094+
#' @description
1095+
#' - `date_today()` returns the current date in the specified `zone` as a Date.
1096+
#'
1097+
#' - `date_now()` returns the current date-time in the specified `zone` as a
1098+
#' POSIXct.
1099+
#'
1100+
#' @details
1101+
#' clock assumes that Date is a _naive_ type, like naive-time. This means that
1102+
#' `date_today()` first looks up the current date-time in the specified `zone`,
1103+
#' then converts that to a Date, retaining the printed time while dropping any
1104+
#' information about that time zone.
1105+
#'
1106+
#' @inheritParams zoned_time_now
1107+
#'
1108+
#' @return
1109+
#' - `date_today()` a single Date.
1110+
#'
1111+
#' - `date_now()` a single POSIXct.
1112+
#'
1113+
#' @name date-today
1114+
#'
1115+
#' @examples
1116+
#' # Current date in the local time zone
1117+
#' date_today("")
1118+
#'
1119+
#' # Current date in a specified time zone
1120+
#' date_today("Europe/London")
1121+
#'
1122+
#' # Current date-time in that same time zone
1123+
#' date_now("Europe/London")
1124+
NULL
1125+
1126+
#' @rdname date-today
1127+
#' @export
1128+
date_today <- function(zone) {
1129+
as.Date(zoned_time_now(zone))
1130+
}

R/posixt.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,3 +1276,11 @@ date_time_build <- function(year,
12761276
x <- invalid_resolve(x, invalid = invalid)
12771277
as.POSIXct(x, tz = zone, nonexistent = nonexistent, ambiguous = ambiguous)
12781278
}
1279+
1280+
# ------------------------------------------------------------------------------
1281+
1282+
#' @rdname date-today
1283+
#' @export
1284+
date_now <- function(zone) {
1285+
as.POSIXct(zoned_time_now(zone))
1286+
}

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ reference:
1313
contents:
1414
- date_build
1515
- date_time_build
16+
- date-today
1617
- date_group
1718
- date-and-date-time-rounding
1819
- date-and-date-time-shifting

man/date-today.Rd

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-date.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,15 @@ test_that("can handle invalid dates", {
311311
)
312312
})
313313

314+
# ------------------------------------------------------------------------------
315+
# date_today()
316+
317+
test_that("can get the current date", {
318+
x <- date_today("America/New_York")
319+
expect_length(x, 1)
320+
expect_s3_class(x, "Date")
321+
})
322+
314323
# ------------------------------------------------------------------------------
315324
# date_zone()
316325

tests/testthat/test-posixt.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,15 @@ test_that("can handle ambiguous times", {
389389
)
390390
})
391391

392+
# ------------------------------------------------------------------------------
393+
# date_now()
394+
395+
test_that("can get the current date-time", {
396+
x <- date_now("America/New_York")
397+
expect_length(x, 1)
398+
expect_s3_class(x, "POSIXct")
399+
})
400+
392401
# ------------------------------------------------------------------------------
393402
# vec_arith()
394403

0 commit comments

Comments
 (0)