Skip to contents

Tokens are named recipes for the labels (and within-year shares) of one timeframe in a calendar hierarchy. In a calendar, a token's labels name the members of its timeframe inside each member of the timeframe before it (h00 becomes m01_h00, m02_h00, ...). A handful of built-in tokens covers the common cases (d365, m12, m12a, q4, w52, w53, wd7, h24, h168, min60, d360, d364, d366). Custom tokens may be added with register_calendar_token().

Usage

register_calendar_token(name, timeframe, expand, alignment = NULL, unit = NULL)

get_calendar_token(name)

list_calendar_tokens()

Arguments

name

Character scalar — the token name (e.g. "m12").

timeframe

One of CORE_TIMEFRAMES.

expand

A zero-argument function returning a data.frame with columns label (character, unique) and share (numeric > 0, summing to 1).

alignment

Optional default alignment rule (one of ALIGNMENT_RULES) declaring how real instants beyond this token's vocabulary map onto it (e.g. "drop_feb29" for d365). Calendars built from the token inherit it in meta$alignment.

unit

Optional length of one unit of the timeframe, in hours (a single positive number). Calendars whose finest timeframe comes from the token record it in meta$units.

Value

register_calendar_token() invisibly returns the token name. get_calendar_token() returns the token definition (a list with timeframe, expand, and optionally alignment and unit). list_calendar_tokens() returns a character vector of all registered token names.

Details

A token may state the unit of its timeframe, in hours. The share of a leaf timeslice of that timeframe is then one unit, and the calendar records the unit in meta$units. The built-in h24 and hp3 state one hour; a token of quarter-hours would state 0.25. A token without a unit leaves each timeslice its own unit (see the section on share, width and weight in Calendar).

Examples

# Register a custom 4-period day-of-year partition
register_calendar_token("d4q", "YDAY", function() {
  data.frame(label = c("Q1d", "Q2d", "Q3d", "Q4d"),
             share = c(90, 91, 92, 92) / 365)
})
"d4q" %in% list_calendar_tokens()
#> [1] TRUE