dttr2
(Dates, Times and dateTimes in R) is an R package
to perform simple manipulations on date (Date), datetime (POSIXct) and
time (hms) vectors (collectively referred to as date/times).
Key design principles include
To install the latest release version from CRAN
install.packages("dttr2")
To install the latest development version from GitHub
# install.packages("pak")
pak::pak("poissonconsulting/dttr2")
dttr2
floors any date/time objects it encounters to the
nearest time unit (by default days for Date and seconds for POSIXct and
hms).
library(dttr2)
<- as.Date(1.75, origin = "1970-01-01")
date
date#> [1] "1970-01-02"
as.numeric(date)
#> [1] 1.75
<- dtt_date(date)
date
date#> [1] "1970-01-02"
as.numeric(date)
#> [1] 1
Similarly, the time unit accessor (dtt_second()
,
dtt_minute()
etc) and settor functions as well as the
functions to add (ie dtt_add_seconds()
) and subtract time
units return or require integers.
dtt_day(date)
#> [1] 2
dtt_add_days(date, 2L)
#> [1] "1970-01-04"
dtt_add_months(date, 24L)
#> [1] "1972-01-02"
dttr2
wraps negative times or times greater than
23:59:59 to the actual clock time.
<- hms::as_hms(-3)
time
time#> -00:00:03
dtt_time(time)
#> 23:59:57
dtt_time(hms::as_hms("24:00:00"))
#> 00:00:00
The user can override the default time zone (UTC) when handling
POSIXct vectors using dtt_set_default_tz()
.
dtt_default_tz()
#> [1] "UTC"
<- dtt_date_time("1970-01-01 03:00:00")
date_time dtt_set_default_tz("Etc/GMT+10")
dtt_default_tz()
#> [1] "Etc/GMT+10"
date_time#> [1] "1970-01-01 03:00:00 UTC"
They can assign a new time zone to a POSIXct object whilst leaving
the clock time unchanged using dtt_set_tz()
or adjust the
time zone so that clock (but not the actual) time is altered using
dtt_adjust_tz()
date_time#> [1] "1970-01-01 03:00:00 UTC"
dtt_set_tz(date_time)
#> [1] "1970-01-01 03:00:00 -10"
dtt_adjust_tz(date_time)
#> [1] "1969-12-31 17:00:00 -10"
dttr2
makes it easy to decompose POSIXct vectors into
Date and hms vectors and then recombine them again.
As neither dates nor times have associated time zones unless the user passes a value they are assigned the default time zone.
date_time#> [1] "1970-01-01 03:00:00 UTC"
<- dtt_date(date_time)
date
date#> [1] "1970-01-01"
<- dtt_time(date_time)
time
time#> 03:00:00
dtt_date_time(date, time)
#> [1] "1970-01-01 03:00:00 -10"
dttr2 was influenced by lubridate. It aims to provide an alternative for simple manipulations.
Please report any issues.
Pull requests are always welcome.
Please note that the dttr2 project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.