Learn R Programming

dttr2

Introduction

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

  1. use existing classes (Date, POSIXct, hms)
  2. date/times are discrete (floor when encountered)
  3. times are times (wrap when encountered)
  4. maintain the current timezone (principle of least astonishment)
  5. the origin is always assumed to be 1970-01-01 UTC (keep it simple)

Installation

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")

Introduction

Time Units

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)
date <- as.Date(1.75, origin = "1970-01-01")
date
#> [1] "1970-01-02"
as.numeric(date)
#> [1] 1.75

date <- dtt_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"

Times

dttr2 wraps negative times or times greater than 23:59:59 to the actual clock time.

time <- hms::as_hms(-3)
time
#> -00:00:03
dtt_time(time)
#> 23:59:57
dtt_time(hms::as_hms("24:00:00"))
#> 00:00:00

Time Zones

The user can override the default time zone (UTC) when handling POSIXct vectors using dtt_set_default_tz().

dtt_default_tz()
#> [1] "UTC"
date_time <- dtt_date_time("1970-01-01 03:00:00")
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"

Combining Dates and Times

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"
date <- dtt_date(date_time)
date
#> [1] "1970-01-01"
time <- dtt_time(date_time)
time
#> 03:00:00
dtt_date_time(date, time)
#> [1] "1970-01-01 03:00:00 -10"

Inspiration

dttr2 was influenced by lubridate. It aims to provide an alternative for simple manipulations.

Contribution

Please report any issues.

Pull requests are always welcome.

Code of Conduct

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.

Copy Link

Version

Install

install.packages('dttr2')

Monthly Downloads

336

Version

0.5.2

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Ayla Pearson

Last Published

September 26th, 2024

Functions in dttr2 (0.5.2)

dtt_date_time_from_ints

Create Date Times from Integers
check_tz

Check Time Zone
NA_hms_

Missing hms
dtt_completed

Completed
dtt_days_in_year

Days in the Year
dtt_date_from_ints

Create Dates from Integers
dtt_complete

Complete
dtt_date_time

Floor Date/Time
dtt_dayte

Dayte
dtt_seconds

Get and Set Second Values
dtt_floored

Test Floored
dtt_hours

Get and Set Hour Values
dtt_day_decimal

Get Decimal Day Values
dtt_seq

Sequence
dtt_days_in_month

Days in the Month
dtt_day

Get and Set Day Values
dtt_doy_decimal

Day of the Year Decimal
dtt_dayte_time

Dayte Time
dtt_hour_decimal

Get Decimal Hour Values
dtt_set_time

Floor Time
dtt_minute_decimal

Get Decimal Minute Values
dtt_leap_year

Test for Leap Year
dtt_daytt

Dayte Time
dtt_doy_to_date

Day of the Year to Date
dtt_diff

Time Difference
dtt_is_date_time

Is Date Time
is_date_time

Is Date/Time
dtt_is_dtt

Is Date or DateTime Object
dtt_wrap

Wrap
dtt_minutes

Get and Set Minute Values
NA_POSIXct_

Missing POSIXct
dtt_wday

Get Week Day
dtt_excel_to_date

Convert Excel dates to dates.
dtt_years

Get and Set Year Values
params

Parameters for dttr2 functions
dtt_months

Get and Set Month Values
dtt_units_per_unit

Units per Unit
dtt_units

Get Units
dtt_date

Floor Date
dtt_doy

Day of the Year
dtt_is_date

Is Date
dtt_date_add_time

Add Time to Date
dtt_month_decimal

Get Decimal Month Values
dtt_season

Season
dtt_subtract_units

Subtract Time Units
dtt_sys_date

Get System Date
dtt_sys_date_time

Get System Date Time
dtt_excel_to_date_time

Convert Excel date times to date times.
dtt_decade

Decade
vld_time

Validate Time
dtt_set_tz

Set Time Zone
dtt_sys_time

Get System Time
dtt_time_from_ints

Create Time from Vectors
dtt_year_decimal

Get Decimal Year Values
dtt_sys_tz

Get, Set or Reset Default Time Zone
dtt_feb29_to_28

Convert Feb 29 to Feb 28
dtt_floor

Floor Date/Time
dttr2-package

dttr2: Manipulate Date, POSIXct and hms Vectors
dtt_study_year

Study Year
dtt_tz

Get, Set or Adjust Time Zone
dtt_adjust_units

Adjust Units
dtt_add_units

Add Time Units
NA_Date_

Missing Date
dtt_adjust_tz

Adjust Time Zone
dtt_aggregate

Aggregate Date/Time
chk_time

Check Time
dtt

dtt Object