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

130,733

Version

0.7.2

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Davis Vaughan

Last Published

January 21st, 2025

Functions in clock (0.7.2)

as-zoned-time-posixt

Convert to a zoned-time from a date-time
as-zoned-time-sys-time

Convert to a zoned-time from a sys-time
as-zoned-time-naive-time

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

Convert to a date
as_sys_time

Convert to a sys-time
as_year_day

Convert to year-day
as_iso_year_week_day

Convert to iso-year-week-day
as_zoned_time

Convert to a zoned-time
as_naive_time

Convert to a naive-time
as_year_quarter_day

Convert to year-quarter-day
as_year_month_weekday

Convert to year-month-weekday
as_year_month_day

Convert to year-month-day
as_weekday

Convert to a weekday
calendar_month_factor

Convert a calendar to an ordered factor of month names
as_year_week_day

Convert to year-week-day
calendar-boundary

Boundaries: calendars
calendar-count-between

Counting: calendars
calendar_narrow

Narrow a calendar to a less precise precision
date-and-date-time-boundary

Boundaries: date and date-time
clock_locale

Create a clock locale
calendar_group

Group calendar components
clock_labels

Create or retrieve date related labels
calendar_leap_year

Is the calendar year a leap year?
clock-package

clock: Date-Time Types and Tools
vec_arith.clock_year_day

Support for vctrs arithmetic
calendar_precision

Precision: calendar
clock-setters

Calendar setters
calendar_widen

Widen a calendar to a more precise precision
clock-invalid

Invalid calendar dates
clock-getters

Calendar getters
calendar_spanning_seq

Spanning sequence: calendars
date-and-date-time-rounding

Date and date-time rounding
clock-arithmetic

Clock arithmetic
clock-codes

Integer codes
date-and-date-time-shifting

Shifting: date and date-time
date-group

Group date components
date-sequence

Sequences: date
date-time-zone

Get or set the time zone
date-time-parse

Parsing: date-time
date-boundary

Boundaries: date
date-shifting

Shifting: date
date-rounding

Rounding: date
date-count-between

Counting: date
date-formatting

Formatting: date
date_build

Building: date
date_leap_year

Is the year a leap year?
date_count_between

Counting: date and date-time
date_parse

Parsing: date
date_month_factor

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

Formatting: date and date-time
date_group

Group date and date-time components
date_time_info

Info: date-time
duration_precision

Precision: duration
duration_spanning_seq

Spanning sequence: duration
date_weekday_factor

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

Spanning sequence: date and date-time
date_time_build

Building: date-time
is_weekday

Is x a weekday?
is_sys_time

Is x a sys-time?
date_seq

Sequences: date and date-time
duration_cast

Cast a duration between precisions
duration-rounding

Duration rounding
date-today

Current date and date-time
duration-arithmetic

Arithmetic: duration
date-zone

Get or set the time zone
duration-helper

Construct a duration
format.clock_zoned_time

Formatting: zoned-time
is_year_month_day

Is x a year-month-day?
iso-year-week-day-group

Grouping: iso-year-week-day
iso-year-week-day-narrow

Narrow: iso-year-week-day
is_year_day

Is x a year-day?
is_iso_year_week_day

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

Is x a naive-time?
is_year_month_weekday

Is x a year-month-weekday?
iso-year-week-day-getters

Getters: iso-year-week-day
iso-year-week-day-count-between

Counting: iso-year-week-day
is_duration

Is x a duration?
is_year_week_day

Is x a year-week-day?
is_zoned_time

Is x a zoned-time?
is_year_quarter_day

Is x a year-quarter-day?
iso-year-week-day-arithmetic

Arithmetic: iso-year-week-day
iso-year-week-day-boundary

Boundaries: iso-year-week-day
iso-year-week-day-widen

Widen: iso-year-week-day
iso-year-week-day-setters

Setters: iso-year-week-day
naive_time_parse

Parsing: naive-time
posixt-count-between

Counting: date-times
posixt-formatting

Formatting: date-time
posixt-getters

Getters: date-time
posixt-boundary

Boundaries: date-time
naive_time_info

Info: naive-time
posixt-group

Group date-time components
iso_year_week_day

Calendar: iso-year-week-day
posixt-rounding

Rounding: date-time
posixt-arithmetic

Arithmetic: date-time
seq.clock_year_month_day

Sequences: year-month-day
seq.clock_time_point

Sequences: time points
posixt-shifting

Shifting: date and date-time
seq.clock_iso_year_week_day

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

Sequences: year-day
posixt-sequence

Sequences: date-time
seq.clock_duration

Sequences: duration
posixt-setters

Setters: date-time
reexports

Objects exported from other packages
seq.clock_year_month_weekday

Sequences: year-month-weekday
time-point-arithmetic

Arithmetic: Time points
time_point_cast

Cast a time point between precisions
sys_time_now

What is the current sys-time?
seq.clock_year_week_day

Sequences: year-week-day
time-point-rounding

Time point rounding
sys-parsing

Parsing: sys-time
time_point_precision

Precision: time point
time_point_count_between

Counting: time point
seq.clock_year_quarter_day

Sequences: year-quarter-day
sys_time_info

Info: sys-time
weekday

Construct a weekday vector
year-day-count-between

Counting: year-day
year-day-arithmetic

Arithmetic: year-day
year-day-boundary

Boundaries: year-day
weekday_code

Extract underlying weekday codes
weekday-arithmetic

Arithmetic: weekday
weekday_factor

Convert a weekday to an ordered factor
year-day-getters

Getters: year-day
year-day-setters

Setters: year-day
year-day-group

Grouping: year-day
time_point_spanning_seq

Spanning sequence: time points
time_point_shift

Shifting: time point
year-month-day-arithmetic

Arithmetic: year-month-day
year-month-day-group

Grouping: year-month-day
year-day-narrow

Narrow: year-day
year-month-day-boundary

Boundaries: year-month-day
year-month-weekday-setters

Setters: year-month-weekday
year-month-weekday-widen

Widen: year-month-weekday
year-month-day-narrow

Narrow: year-month-day
year-month-day-count-between

Counting: year-month-day
year-month-weekday-arithmetic

Arithmetic: year-month-weekday
year-month-day-getters

Getters: year-month-day
year-month-weekday-boundary

Boundaries: year-month-weekday
year-month-day-setters

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

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

Counting: year-month-weekday
year-month-weekday-getters

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

Widen: year-quarter-day
year-quarter-day-setters

Setters: year-quarter-day
year-day-widen

Widen: year-day
year-month-weekday-group

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

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

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

Boundaries: year-quarter-day
year-week-day-arithmetic

Arithmetic: year-week-day
year-quarter-day-narrow

Narrow: year-quarter-day
year-quarter-day-group

Grouping: year-quarter-day
year-week-day-boundary

Boundaries: year-week-day
year_day

Calendar: year-day
year-quarter-day-getters

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

Counting: year-quarter-day
year-week-day-setters

Setters: year-week-day
year-week-day-widen

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

Grouping: year-week-day
year_month_day

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

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

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

Narrow: year-week-day
year_month_weekday

Calendar: year-month-weekday
year_quarter_day

Calendar: year-quarter-day
zoned_time_precision

Precision: zoned-time
year_month_day_parse

Parsing: year-month-day
zoned-zone

Get or set the time zone
zoned-parsing

Parsing: zoned-time
year_week_day

Calendar: year-week-day
zoned_time_now

What is the current zoned-time?
zoned_time_info

Info: zoned-time
Date-getters

Getters: date
as_date_time

Convert to a date-time
as-zoned-time-Date

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

Setters: date
as_duration

Convert to a duration
Date-arithmetic

Arithmetic: date