Learn R Programming

MazamaCoreUtils (version 0.6.2)

dateSequence: Create a POSIXct date sequence

Description

Create a sequence of local-midnight POSIXct datetimes in a specified timezone.

Usage

dateSequence(
  startdate = NULL,
  enddate = NULL,
  timezone = NULL,
  ceilingEnd = FALSE
)

Value

A vector of POSIXct datetimes at local midnight.

Arguments

startdate

Desired start datetime.

enddate

Desired end datetime.

timezone

Olson timezone used to interpret incoming dates.

ceilingEnd

Logical specifying whether to include the end of the final day.

POSIXct inputs

When startdate or enddate are already POSIXct values, they are first converted to timezone with lubridate::with_tz() without changing the represented instant in time. They are then floored to local midnight.

Details

The returned sequence begins at midnight local time on startdate and ends at midnight local time on enddate, i.e. the beginning of enddate.

The ceilingEnd argument addresses ambiguity in date ranges such as "August 1-8". With ceilingEnd = FALSE (default), the sequence ends at the beginning of August 8. With ceilingEnd = TRUE, the sequence includes the entirety of August 8, ending at the midnight that begins August 9.

Input dates are parsed with parseDatetime() using the specified timezone. Any hour-minute-second information is removed after parsing.

Examples

Run this code
dateSequence(
  "2019-11-01",
  "2019-11-08",
  timezone = "America/Los_Angeles"
)

dateSequence(
  "2019-11-01",
  "2019-11-07",
  timezone = "America/Los_Angeles",
  ceilingEnd = TRUE
)

# Observe daylight savings handling
datetime <- dateSequence(
  "2019-11-01",
  "2019-11-08",
  timezone = "America/Los_Angeles"
)

datetime
lubridate::with_tz(datetime, "UTC")

# POSIXct inputs preserve the represented instant before flooring
jst <- dateSequence(
  20190307,
  20190315,
  timezone = "Asia/Tokyo"
)

jst

dateSequence(
  jst[1],
  jst[7],
  timezone = "UTC"
)

Run the code above in your browser using DataLab