Skip to content

Commit 032eeb3

Browse files
committed
Implement date_start() and date_end()
1 parent aa0123f commit 032eeb3

File tree

11 files changed

+617
-0
lines changed

11 files changed

+617
-0
lines changed

NAMESPACE

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ S3method(calendar_widen,clock_year_month_weekday)
221221
S3method(calendar_widen,clock_year_quarter_day)
222222
S3method(date_ceiling,Date)
223223
S3method(date_ceiling,POSIXt)
224+
S3method(date_end,Date)
225+
S3method(date_end,POSIXt)
224226
S3method(date_floor,Date)
225227
S3method(date_floor,POSIXt)
226228
S3method(date_format,Date)
@@ -239,6 +241,8 @@ S3method(date_set_zone,Date)
239241
S3method(date_set_zone,POSIXt)
240242
S3method(date_shift,Date)
241243
S3method(date_shift,POSIXt)
244+
S3method(date_start,Date)
245+
S3method(date_start,POSIXt)
242246
S3method(date_weekday_factor,Date)
243247
S3method(date_weekday_factor,POSIXt)
244248
S3method(date_zone,Date)
@@ -588,6 +592,7 @@ export(clock_labels_lookup)
588592
export(clock_locale)
589593
export(date_build)
590594
export(date_ceiling)
595+
export(date_end)
591596
export(date_floor)
592597
export(date_format)
593598
export(date_group)
@@ -599,6 +604,7 @@ export(date_round)
599604
export(date_seq)
600605
export(date_set_zone)
601606
export(date_shift)
607+
export(date_start)
602608
export(date_time_build)
603609
export(date_time_parse)
604610
export(date_time_parse_abbrev)

R/date.R

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,122 @@ date_today <- function(zone) {
12291229

12301230
# ------------------------------------------------------------------------------
12311231

1232+
#' Boundaries: date and date-time
1233+
#'
1234+
#' @description
1235+
#' - `date_start()` computes the date at the start of a particular
1236+
#' `precision`, such as the "start of the year".
1237+
#'
1238+
#' - `date_end()` computes the date at the end of a particular
1239+
#' `precision`, such as the "end of the month".
1240+
#'
1241+
#' There are separate help pages for computing boundaries for dates and
1242+
#' date-times:
1243+
#'
1244+
#' - [dates (Date)][date-boundary]
1245+
#'
1246+
#' - [date-times (POSIXct/POSIXlt)][posixt-boundary]
1247+
#'
1248+
#' @inheritParams date_group
1249+
#'
1250+
#' @param x `[Date / POSIXct / POSIXlt]`
1251+
#'
1252+
#' A date or date-time vector.
1253+
#'
1254+
#' @param precision `[character(1)]`
1255+
#'
1256+
#' A precision. Allowed precisions are dependent on the input used.
1257+
#'
1258+
#' @return `x` but with some components altered to be at the boundary value.
1259+
#'
1260+
#' @name date-and-date-time-boundary
1261+
#'
1262+
#' @examples
1263+
#' # See type specific documentation for more examples
1264+
#'
1265+
#' x <- date_build(2019, 2:4)
1266+
#'
1267+
#' date_end(x, "month")
1268+
#'
1269+
#' x <- date_time_build(2019, 2:4, 3:5, 4, 5, zone = "America/New_York")
1270+
#'
1271+
#' # Note that the hour, minute, and second components are also adjusted
1272+
#' date_end(x, "month")
1273+
NULL
1274+
1275+
#' @rdname date-and-date-time-boundary
1276+
#' @export
1277+
date_start <- function(x, precision, ...) {
1278+
UseMethod("date_start")
1279+
}
1280+
1281+
#' @rdname date-and-date-time-boundary
1282+
#' @export
1283+
date_end <- function(x, precision, ...) {
1284+
UseMethod("date_end")
1285+
}
1286+
1287+
1288+
#' Boundaries: date
1289+
#'
1290+
#' @description
1291+
#' This is a Date method for the [date_start()] and [date_end()] generics.
1292+
#'
1293+
#' @inheritParams date_group
1294+
#' @inheritParams invalid_resolve
1295+
#'
1296+
#' @param x `[Date]`
1297+
#'
1298+
#' A date vector.
1299+
#'
1300+
#' @param precision `[character(1)]`
1301+
#'
1302+
#' One of:
1303+
#'
1304+
#' - `"year"`
1305+
#'
1306+
#' - `"month"`
1307+
#'
1308+
#' - `"day"`
1309+
#'
1310+
#' @return `x` but with some components altered to be at the boundary value.
1311+
#'
1312+
#' @name date-boundary
1313+
#'
1314+
#' @examples
1315+
#' x <- date_build(2019:2021, 2:4, 3:5)
1316+
#' x
1317+
#'
1318+
#' # Last day of the month
1319+
#' date_end(x, "month")
1320+
#'
1321+
#' # Last day of the year
1322+
#' date_end(x, "year")
1323+
#'
1324+
#' # First day of the year
1325+
#' date_start(x, "year")
1326+
NULL
1327+
1328+
#' @rdname date-boundary
1329+
#' @export
1330+
date_start.Date <- function(x, precision, ..., invalid = NULL) {
1331+
check_dots_empty()
1332+
x <- as_year_month_day(x)
1333+
x <- calendar_start(x, precision)
1334+
as.Date(x, invalid = invalid)
1335+
}
1336+
1337+
#' @rdname date-boundary
1338+
#' @export
1339+
date_end.Date <- function(x, precision, ..., invalid = NULL) {
1340+
check_dots_empty()
1341+
x <- as_year_month_day(x)
1342+
x <- calendar_end(x, precision)
1343+
as.Date(x, invalid = invalid)
1344+
}
1345+
1346+
# ------------------------------------------------------------------------------
1347+
12321348
#' Sequences: date and date-time
12331349
#'
12341350
#' @description

R/posixt.R

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,91 @@ date_now <- function(zone) {
14271427

14281428
# ------------------------------------------------------------------------------
14291429

1430+
#' Boundaries: date-time
1431+
#'
1432+
#' @description
1433+
#' This is a POSIXct/POSIXlt method for the [date_start()] and [date_end()]
1434+
#' generics.
1435+
#'
1436+
#' @inheritParams date_group
1437+
#' @inheritParams invalid_resolve
1438+
#' @inheritParams as-zoned-time-naive-time
1439+
#'
1440+
#' @param x `[POSIXct / POSIXlt]`
1441+
#'
1442+
#' A date-time vector.
1443+
#'
1444+
#' @param precision `[character(1)]`
1445+
#'
1446+
#' One of:
1447+
#'
1448+
#' - `"year"`
1449+
#'
1450+
#' - `"month"`
1451+
#'
1452+
#' - `"day"`
1453+
#'
1454+
#' - `"hour"`
1455+
#'
1456+
#' - `"minute"`
1457+
#'
1458+
#' - `"second"`
1459+
#'
1460+
#' @return `x` but with some components altered to be at the boundary value.
1461+
#'
1462+
#' @name posixt-boundary
1463+
#'
1464+
#' @examples
1465+
#' x <- date_time_build(2019:2021, 2:4, 3:5, 4, 5, 6, zone = "America/New_York")
1466+
#' x
1467+
#'
1468+
#' # Last moment of the month
1469+
#' date_end(x, "month")
1470+
#'
1471+
#' # Notice that this is different from just setting the day to `"last"`
1472+
#' set_day(x, "last")
1473+
#'
1474+
#' # Last moment of the year
1475+
#' date_end(x, "year")
1476+
#'
1477+
#' # First moment of the hour
1478+
#' date_start(x, "hour")
1479+
NULL
1480+
1481+
#' @rdname posixt-boundary
1482+
#' @export
1483+
date_start.POSIXt <- function(x,
1484+
precision,
1485+
...,
1486+
invalid = NULL,
1487+
nonexistent = NULL,
1488+
ambiguous = x) {
1489+
check_dots_empty()
1490+
force(ambiguous)
1491+
zone <- date_zone(x)
1492+
x <- as_year_month_day(x)
1493+
x <- calendar_start(x, precision)
1494+
as.POSIXct(x, zone, invalid = invalid, nonexistent = nonexistent, ambiguous = ambiguous)
1495+
}
1496+
1497+
#' @rdname posixt-boundary
1498+
#' @export
1499+
date_end.POSIXt <- function(x,
1500+
precision,
1501+
...,
1502+
invalid = NULL,
1503+
nonexistent = NULL,
1504+
ambiguous = x) {
1505+
check_dots_empty()
1506+
force(ambiguous)
1507+
zone <- date_zone(x)
1508+
x <- as_year_month_day(x)
1509+
x <- calendar_end(x, precision)
1510+
as.POSIXct(x, zone, invalid = invalid, nonexistent = nonexistent, ambiguous = ambiguous)
1511+
}
1512+
1513+
# ------------------------------------------------------------------------------
1514+
14301515
#' Sequences: date-time
14311516
#'
14321517
#' @description

_pkgdown.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ reference:
1515
- date_time_build
1616
- date-today
1717
- date_group
18+
- date-and-date-time-boundary
1819
- date-and-date-time-rounding
1920
- date-and-date-time-shifting
2021
- date_seq
@@ -197,6 +198,8 @@ reference:
197198
contents:
198199
- date-group
199200
- posixt-group
201+
- date-boundary
202+
- posixt-boundary
200203
- date-formatting
201204
- posixt-formatting
202205
- date-rounding

man/date-and-date-time-boundary.Rd

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

man/date-boundary.Rd

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

0 commit comments

Comments
 (0)