Learn R Programming

clock

clock is an R package for working with date-times. It is packed with features, including utilities for: parsing, formatting, arithmetic, rounding, and extraction/updating of individual components. In addition to these tools for manipulating date-times, clock provides entirely new date-time types which are structured to reduce the agony of working with time zones as much as possible. At a high-level, clock:

  • Provides a new family of date-time classes (durations, time points, zoned-times, and calendars) that partition responsibilities so that you only have to think about time zones when you need them.

  • Implements a high level API for R’s native date (Date) and date-time (POSIXct) classes that lets you get productive quickly without having to learn the details of clock’s new date-time types.

  • Requires explicit handling of invalid dates (e.g. what date is one month after January 31st?) and nonexistent or ambiguous times (caused by daylight saving time issues).

  • Is built on the C++ date library, which provides a correct and high-performance backend.

There are four key classes in clock, inspired by the design of the C++ date and chrono libraries. Some types are more efficient than others at particular operations, and it is only when all 4 are taken as a whole do you get a complete date time library.

  • A duration counts units of time, like “5 years” or “6 nanoseconds”. Bigger units are defined in terms of seconds, i.e. 1 day is 86400 seconds and 1 year is 365.2425 days. Durations are important because they form the backbone of clock; it’s relatively rare to use them directly.

  • A time point records an instant in time, like “1:24pm January 1st 2015”. It combines a duration with a “clock” that defines when to start counting and what exactly to count. There are two important types of time in clock: sys-time and naive-time. They’re equivalent until you start working with zoned-times.

  • A zoned-time is a time point paired with a time zone. You can create them from either a sys-time or a naive-time, depending on whether you want to convert the time point from UTC (leaving the underlying duration unchanged, but changing the printed time), or declare that the time point is in a specific time zone (leaving the printed time unchanged, but changing the underlying duration). Zoned-times are primarily needed for communication with humans.

  • A calendar represents a date using a combination of fields like year-month-day, year-month-weekday, year-quarter-day, year-day, or iso-year-week-day, along with hour/minute/second fields to represent time within a day (so they’re similar to R’s POSIXlt). Calendar objects are extremely efficient at arithmetic involving irregular periods such as months, quarters, and years and at getting and setting specified components. A calendar can represent invalid dates (like 2020-02-31) which only need to be resolved when converting back to a time point.

Installation

Install the released version from CRAN with:

install.packages("clock")

Install the development version from GitHub with:

# install.packages("pak")
pak::pak("r-lib/clock")

Learning

With clock, there is a high-level API for manipulating R’s native date and date-time types (Date, POSIXct, and POSIXlt), as well as a low-level API for more advanced date-time manipulations. The high-level API should allow you to quickly get productive with clock without having to learn many of the new types. An easy way to get an overview of this is by looking at the High Level API section of the pkgdown reference page.

You’ll also want to take a look at clock’s vignettes:

You can also watch this 5 minute lightning talk about clock:

Acknowledgments

The ideas used to build clock have come from a number of places:

  • First and foremost, clock depends on and is inspired by the date library by Howard Hinnant, a variant of which has been voted in to C++20.

  • The R “record” types that clock is built on come from vctrs.

  • The nanotime package was the first to implement a nanosecond resolution time type for R.

  • The zoo package was the first to implement year-month and year-quarter types, and has functioned as a very successful time series infrastructure package for many years.

Additionally, I’d like to thank my wife for being a power user of clock, and having a number of discussions with me around bugs and missing features. You can thank her for date_count_between().

Copy Link

Version

Install

install.packages('clock')

Monthly Downloads

157,044

Version

0.7.1

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Last Published

July 18th, 2024

Functions in clock (0.7.1)

as-zoned-time-sys-time

Convert to a zoned-time from a sys-time
as_date_time

Convert to a date-time
Date-setters

Setters: date
Date-getters

Getters: date
as-zoned-time-naive-time

Convert to a zoned-time from a naive-time
as_zoned_time

Convert to a zoned-time
as_year_week_day

Convert to year-week-day
as_year_month_weekday

Convert to year-month-weekday
as_naive_time

Convert to a naive-time
as_sys_time

Convert to a sys-time
as_year_month_day

Convert to year-month-day
as_year_day

Convert to year-day
as_year_quarter_day

Convert to year-quarter-day
as_iso_year_week_day

Convert to iso-year-week-day
as_weekday

Convert to a weekday
calendar_precision

Precision: calendar
calendar_widen

Widen a calendar to a more precise precision
calendar-boundary

Boundaries: calendars
calendar-count-between

Counting: calendars
vec_arith.clock_year_day

Support for vctrs arithmetic
calendar_narrow

Narrow a calendar to a less precise precision
clock-setters

Calendar setters
calendar_month_factor

Convert a calendar to an ordered factor of month names
clock-package

clock: Date-Time Types and Tools
clock_labels

Create or retrieve date related labels
clock_locale

Create a clock locale
calendar_group

Group calendar components
calendar_leap_year

Is the calendar year a leap year?
clock-getters

Calendar getters
calendar_spanning_seq

Spanning sequence: calendars
clock-arithmetic

Clock arithmetic
clock-codes

Integer codes
date-and-date-time-shifting

Shifting: date and date-time
date-time-parse

Parsing: date-time
date-time-zone

Get or set the time zone
clock-invalid

Invalid calendar dates
date-count-between

Counting: date
date-formatting

Formatting: date
date-rounding

Rounding: date
date-group

Group date components
date-boundary

Boundaries: date
date_build

Building: date
date-today

Current date and date-time
date-zone

Get or set the time zone
date_leap_year

Is the year a leap year?
date-and-date-time-rounding

Date and date-time rounding
date-and-date-time-boundary

Boundaries: date and date-time
date-sequence

Sequences: date
date_month_factor

Convert a date or date-time to an ordered factor of month names
date_count_between

Counting: date and date-time
duration_precision

Precision: duration
date_parse

Parsing: date
duration_spanning_seq

Spanning sequence: duration
date_spanning_seq

Spanning sequence: date and date-time
date_time_build

Building: date-time
date-shifting

Shifting: date
date_format

Formatting: date and date-time
date_time_info

Info: date-time
duration-rounding

Duration rounding
date_seq

Sequences: date and date-time
is_year_month_weekday

Is x a year-month-weekday?
date_group

Group date and date-time components
is_year_day

Is x a year-day?
date_weekday_factor

Convert a date or date-time to a weekday factor
is_year_month_day

Is x a year-month-day?
iso-year-week-day-count-between

Counting: iso-year-week-day
duration-arithmetic

Arithmetic: duration
duration_cast

Cast a duration between precisions
is_sys_time

Is x a sys-time?
iso-year-week-day-getters

Getters: iso-year-week-day
is_weekday

Is x a weekday?
iso-year-week-day-setters

Setters: iso-year-week-day
iso-year-week-day-arithmetic

Arithmetic: iso-year-week-day
duration-helper

Construct a duration
format.clock_zoned_time

Formatting: zoned-time
iso_year_week_day

Calendar: iso-year-week-day
naive_time_info

Info: naive-time
iso-year-week-day-boundary

Boundaries: iso-year-week-day
is_year_quarter_day

Is x a year-quarter-day?
posixt-formatting

Formatting: date-time
iso-year-week-day-widen

Widen: iso-year-week-day
is_duration

Is x a duration?
posixt-boundary

Boundaries: date-time
posixt-getters

Getters: date-time
is_iso_year_week_day

Is x a iso-year-week-day?
is_year_week_day

Is x a year-week-day?
posixt-count-between

Counting: date-times
is_naive_time

Is x a naive-time?
posixt-shifting

Shifting: date and date-time
reexports

Objects exported from other packages
iso-year-week-day-group

Grouping: iso-year-week-day
is_zoned_time

Is x a zoned-time?
posixt-group

Group date-time components
posixt-sequence

Sequences: date-time
iso-year-week-day-narrow

Narrow: iso-year-week-day
seq.clock_year_month_day

Sequences: year-month-day
naive_time_parse

Parsing: naive-time
posixt-rounding

Rounding: date-time
seq.clock_time_point

Sequences: time points
posixt-arithmetic

Arithmetic: date-time
posixt-setters

Setters: date-time
seq.clock_duration

Sequences: duration
time-point-rounding

Time point rounding
seq.clock_iso_year_week_day

Sequences: iso-year-week-day
seq.clock_year_month_weekday

Sequences: year-month-weekday
sys-parsing

Parsing: sys-time
time_point_count_between

Counting: time point
sys_time_info

Info: sys-time
weekday-arithmetic

Arithmetic: weekday
seq.clock_year_day

Sequences: year-day
time_point_cast

Cast a time point between precisions
weekday

Construct a weekday vector
time_point_precision

Precision: time point
seq.clock_year_quarter_day

Sequences: year-quarter-day
year-day-count-between

Counting: year-day
year-day-getters

Getters: year-day
year-day-setters

Setters: year-day
time_point_spanning_seq

Spanning sequence: time points
time_point_shift

Shifting: time point
year-day-widen

Widen: year-day
year-month-day-count-between

Counting: year-month-day
year-month-weekday-group

Grouping: year-month-weekday
year-month-weekday-narrow

Narrow: year-month-weekday
year-month-weekday-setters

Setters: year-month-weekday
year-week-day-arithmetic

Arithmetic: year-week-day
year-quarter-day-getters

Getters: year-quarter-day
year-month-day-group

Grouping: year-month-day
year-month-day-getters

Getters: year-month-day
year-quarter-day-count-between

Counting: year-quarter-day
year-week-day-boundary

Boundaries: year-week-day
year-week-day-setters

Setters: year-week-day
year-month-weekday-widen

Widen: year-month-weekday
year-week-day-count-between

Counting: year-week-day
year-week-day-getters

Getters: year-week-day
year-month-day-narrow

Narrow: year-month-day
year-month-day-setters

Setters: year-month-day
year-month-day-widen

Widen: year-month-day
seq.clock_year_week_day

Sequences: year-week-day
weekday_factor

Convert a weekday to an ordered factor
year-week-day-widen

Widen: year-week-day
year-day-group

Grouping: year-day
weekday_code

Extract underlying weekday codes
sys_time_now

What is the current sys-time?
time-point-arithmetic

Arithmetic: Time points
year-day-narrow

Narrow: year-day
year_quarter_day

Calendar: year-quarter-day
year-day-arithmetic

Arithmetic: year-day
year-day-boundary

Boundaries: year-day
zoned_time_precision

Precision: zoned-time
year-month-day-arithmetic

Arithmetic: year-month-day
year_month_weekday

Calendar: year-month-weekday
year_month_day_parse

Parsing: year-month-day
year-quarter-day-group

Grouping: year-quarter-day
year-quarter-day-narrow

Narrow: year-quarter-day
year-month-weekday-arithmetic

Arithmetic: year-month-weekday
year-month-weekday-boundary

Boundaries: year-month-weekday
year_week_day

Calendar: year-week-day
year-month-weekday-count-between

Counting: year-month-weekday
year-quarter-day-setters

Setters: year-quarter-day
year-month-day-boundary

Boundaries: year-month-day
year-month-weekday-getters

Getters: year-month-weekday
year-quarter-day-widen

Widen: year-quarter-day
year-quarter-day-arithmetic

Arithmetic: year-quarter-day
year-quarter-day-boundary

Boundaries: year-quarter-day
year_day

Calendar: year-day
year-week-day-narrow

Narrow: year-week-day
zoned-parsing

Parsing: zoned-time
year-week-day-group

Grouping: year-week-day
year_month_day

Calendar: year-month-day
zoned_time_info

Info: zoned-time
zoned-zone

Get or set the time zone
zoned_time_now

What is the current zoned-time?
as_duration

Convert to a duration
as-zoned-time-posixt

Convert to a zoned-time from a date-time
as_date

Convert to a date
as-zoned-time-Date

Convert to a zoned-time from a date
Date-arithmetic

Arithmetic: date