Uses incoming parameters to return a seqeunce of POSIXct times at local midnight in the specified timezone. The first returned time will be midnight of the requested startdate. The final returned time will be midnight (at the beginning) of the requested enddate.

The ceilingEnd argument addresses the ambiguity of a phrase like: "August 1-8". With ceilingEnd = FALSE (default) this pharse means "through the beginning of Aug 8". With ceilingEnd = TRUE it means "through the end of Aug 8".

The required timezone parameter must be one of those found in OlsonNames.

Dates can be anything that is understood by lubrdiate::parse_date_time() using the Ymd[HMS] orders. This includes:

  • "YYYYmmdd"

  • "YYYYmmddHHMMSS"

  • "YYYY-mm-dd"

  • "YYYY-mm-dd H"

  • "YYYY-mm-dd H:M"

  • "YYYY-mm-dd H:M:S"

All hour-minute-second information is removed after parsing.

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

Arguments

startdate

Desired start datetime (ISO 8601).

enddate

Desired end datetime (ISO 8601).

timezone

Olson timezone used to interpret dates (required).

ceilingEnd

Logical instruction to apply ceiling_date to the enddate rather than floor_date

Value

A vector of POSIXcts at midnight local time.

Note

The main utility of this function is that it respects "clock time" and returns times associated with midnight regardless of daylight savings. This is in contrast to `seq.Date(from, to, by = "day")` which creates a sequence of datetimes always separated by 24 hours.

POSIXct inputs

When startdate or enddate are already POSIXct values, they are converted to the timezone specified by timezone without altering the physical instant in time the input represents. Only after conversion are they floored to midnight local time

Examples

library(MazamaCoreUtils)

dateSequence("2019-11-01", "2019-11-08", timezone = "America/Los_Angeles")
#> [1] "2019-11-01 PDT" "2019-11-02 PDT" "2019-11-03 PDT" "2019-11-04 PST"
#> [5] "2019-11-05 PST" "2019-11-06 PST" "2019-11-07 PST" "2019-11-08 PST"
dateSequence("2019-11-01", "2019-11-07", timezone = "America/Los_Angeles",
             ceilingEnd = TRUE)
#> [1] "2019-11-01 PDT" "2019-11-02 PDT" "2019-11-03 PDT" "2019-11-04 PST"
#> [5] "2019-11-05 PST" "2019-11-06 PST" "2019-11-07 PST" "2019-11-08 PST"

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

datetime
#> [1] "2019-11-01 PDT" "2019-11-02 PDT" "2019-11-03 PDT" "2019-11-04 PST"
#> [5] "2019-11-05 PST" "2019-11-06 PST" "2019-11-07 PST" "2019-11-08 PST"
lubridate::with_tz(datetime, "UTC")
#> [1] "2019-11-01 07:00:00 UTC" "2019-11-02 07:00:00 UTC"
#> [3] "2019-11-03 07:00:00 UTC" "2019-11-04 08:00:00 UTC"
#> [5] "2019-11-05 08:00:00 UTC" "2019-11-06 08:00:00 UTC"
#> [7] "2019-11-07 08:00:00 UTC" "2019-11-08 08:00:00 UTC"

# Passing in POSIXct values preserves the instant in time before flooring --
#   midnight Tokyo time is the day before in UTC
jst <- dateSequence(20190307, 20190315, timezone = "Asia/Tokyo")
jst
#> [1] "2019-03-07 JST" "2019-03-08 JST" "2019-03-09 JST" "2019-03-10 JST"
#> [5] "2019-03-11 JST" "2019-03-12 JST" "2019-03-13 JST" "2019-03-14 JST"
#> [9] "2019-03-15 JST"
dateSequence(jst[1], jst[7], timezone = "UTC")
#> [1] "2019-03-06 UTC" "2019-03-07 UTC" "2019-03-08 UTC" "2019-03-09 UTC"
#> [5] "2019-03-10 UTC" "2019-03-11 UTC" "2019-03-12 UTC"