Create an ordered two-element POSIXct time range from start and end datetime values.

timeRange(
  starttime = NULL,
  endtime = NULL,
  timezone = NULL,
  unit = "sec",
  ceilingStart = FALSE,
  ceilingEnd = FALSE
)

Arguments

starttime

Desired start datetime.

endtime

Desired end datetime.

timezone

Olson timezone used to interpret incoming datetimes.

unit

Unit used for rounding. Passed to lubridate::floor_date() or lubridate::ceiling_date().

ceilingStart

Logical specifying whether to round the start time up instead of down.

ceilingEnd

Logical specifying whether to round the end time up instead of down.

Value

Two-element POSIXct vector ordered from earliest to latest.

Details

Input values are converted with parseDatetime() using the required timezone argument. The resulting start and end times are sorted so the earlier time is always returned first.

By default, both times are rounded down with lubridate::floor_date() using the requested unit. Set ceilingStart = TRUE or ceilingEnd = TRUE to round either endpoint up with lubridate::ceiling_date() instead.

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.

Examples

timeRange(
  starttime = "2019-01-08 10:12:15",
  endtime = 20190109102030,
  timezone = "UTC"
)
#> [1] "2019-01-08 10:12:15 UTC" "2019-01-09 10:20:30 UTC"

timeRange(
  starttime = "2019-01-08 10:12:15",
  endtime = "2019-01-09 10:20:30",
  timezone = "UTC",
  unit = "hour"
)
#> [1] "2019-01-08 10:00:00 UTC" "2019-01-09 10:00:00 UTC"