Skip to contents

The catalog

timescales ships a curated catalog of named calendar designs. Every id can be built fresh with calendar(id), and the same objects come pre-built in the calendars dataset:

cal <- calendar("m12_h24")     # build on demand
cal2 <- calendars$m12_h24      # pre-built package data
identical(calendar_leaftable(cal), calendar_leaftable(cal2))
#> [1] TRUE

Coverage says how the design relates to a real year: complete (every instant has a timeslice), truncated (a stylised year that drops instants — d365 has no Feb 29), or averaged (timeslices merge recurring periods rather than partition the timeline — q4_h24 is an average day per quarter, which can serve as a representative day). Regularity flags designs whose timeslices differ in length within a timeframe (m12_md365: February is short).

The hours of every design are the timeframe HOUR, and the share of a timeslice of it is one hour. A timeslice built from h24 is one hour wide; the hour types of hp3 are timeslices of HOUR several hours wide. In the designs that enumerate dated days (d365_h24, m12_md365_h24) each hour is there once. In an averaged design it is an hour of an average day and repeats on the days it stands for, which are its weight: 31 for an hour of January in m12_h24. A subset made with filter_calendar() is not a catalog design; its coverage class is sampled.

The full table is one calendar_catalog() call; below it comes in families, each with one member drawn as an icicle (autoplot(); widths are the timeslice shares, the ANNUAL root on top).

Days of the year

Plain day counters, optionally with hours: the full-resolution workhorses (d365_h24 is the classic 8,760) and the stylised 360/364-day years some models use.

fam("^d3[0-9]{2}(_h24)?$")
id timeframes n_timeslices coverage_class regularity
d360 YDAY 360 truncated regular
d364 YDAY 364 truncated regular
d365 YDAY 365 truncated regular
d366 YDAY 366 complete regular
d360_h24 YDAY/HOUR 8640 truncated regular
d364_h24 YDAY/HOUR 8736 truncated regular
d365_h24 YDAY/HOUR 8760 truncated regular
d366_h24 YDAY/HOUR 8784 complete regular
ggplot2::autoplot(calendars$d365_h24)

Months

Twelve calendar months (m01..m12, or JAN..DEC as m12a), optionally with an average day of 24 hours.

fam("^m12(a|_h24|a_h24)?$")
id timeframes n_timeslices coverage_class regularity
m12 MONTH 12 complete regular
m12a MONTH 12 complete regular
m12_h24 MONTH/HOUR 288 averaged regular
m12a_h24 MONTH/HOUR 288 averaged regular
ggplot2::autoplot(calendars$m12_h24)

Month x day-of-month

A real month/day structure — non-Cartesian, February is short — for data keyed by month and day-of-month.

fam("^m12_md")
id timeframes n_timeslices coverage_class regularity
m12_md360 MONTH/MDAY 360 truncated regular
m12_md360_h24 MONTH/MDAY/HOUR 8640 truncated regular
m12_md365 MONTH/MDAY 365 truncated irregular
m12_md365_h24 MONTH/MDAY/HOUR 8760 truncated irregular
m12_md366 MONTH/MDAY 366 complete irregular
m12_md366_h24 MONTH/MDAY/HOUR 8784 complete irregular
ggplot2::autoplot(calendars$m12_md365)

Quarters and seasons

Four calendar quarters, or the meteorological seasons (WIN = Dec-Feb — a season straddles the year boundary, which is why it is its own axis).

fam("^(q4|s4)(_h24)?$")
id timeframes n_timeslices coverage_class regularity
q4 QUARTER 4 complete regular
s4 SEASON 4 complete regular
q4_h24 QUARTER/HOUR 96 averaged regular
s4_h24 SEASON/HOUR 96 averaged regular
ggplot2::autoplot(calendars$s4_h24)

Weeks

52 or 53 weeks, with an average day (_h24) or the full 168-hour week (_h168).

fam("^w5")
id timeframes n_timeslices coverage_class regularity
w52 WEEK 52 truncated regular
w53 WEEK 53 complete regular
w52_h24 WEEK/HOUR 1248 averaged regular
w53_h24 WEEK/HOUR 1272 averaged regular
w52_h168 WEEK/WHOUR 8736 truncated regular
w53_h168 WEEK/WHOUR 8904 complete irregular
ggplot2::autoplot(calendars$w52_h24)

Weekdays and day types

The seven weekdays, or the compact workday/weekend pair — the smallest designs that still resolve a weekly demand cycle.

fam("^(wd7|wk2)")
id timeframes n_timeslices coverage_class regularity
wd7 WDAY 7 averaged regular
wd7_h24 WDAY/HOUR 168 averaged regular
wk2 DAYTYPE 2 averaged regular
wk2_h24 DAYTYPE/HOUR 48 averaged regular
ggplot2::autoplot(calendars$wk2_h24)

Hour types

DAY/NIGHT/PEAK hour classes on their own or under a coarser axis — the “averaged periods” family.

fam("hp3$")
id timeframes n_timeslices coverage_class regularity
hp3 HOUR 3 averaged regular
d365_hp3 YDAY/HOUR 1095 averaged regular
m12a_hp3 MONTH/HOUR 36 averaged regular
s4_hp3 SEASON/HOUR 12 averaged regular
q4_hp3 QUARTER/HOUR 12 averaged regular
ggplot2::autoplot(calendars$s4_hp3)

Fiscal years (April-start)

The same designs anchored to April 1 — Indian fiscal reporting; note the member order starting at m04/Q2 (details in the Fiscal years section below).

fam("^fy04")
id timeframes n_timeslices coverage_class regularity
fy04_m12 MONTH 12 complete regular
fy04_m12_h24 MONTH/HOUR 288 averaged regular
fy04_q4 QUARTER 4 complete regular
fy04_q4_h24 QUARTER/HOUR 96 averaged regular
fy04_d365 YDAY 365 truncated regular
fy04_d365_h24 YDAY/HOUR 8760 truncated regular
ggplot2::autoplot(calendars$fy04_m12)

Timeslices are duration-proportional

Catalog calendars give every timeslice the part of the year it really stands for, the column total of the leaftable:

m12 <- calendars$m12
data.frame(timeslice = calendar_timeslices(m12), total = round(calendar_leaftable(m12)$total, 4))[1:3, ]
#>   timeslice  total
#> 1       m01 0.0849
#> 2       m02 0.0767
#> 3       m03 0.0849

January is 31/365 of the year, not 1/12. This is what makes recast_calendar(rule = "sum") conserve and weighted_mean weight correctly.

Share, width and weight

Three numbers state the length of a leaf timeslice. share is one unit of its timeframe, as a fraction of the year: one hour of the year for a timeslice counted in hours, and the timeslice’s own length for a month, a season, a day or a week. width is the length of the timeslice on the time axis, in those units, within its cycle. weight is the number of times the timeslice repeats: the days an hour of an average day stands for, or the multiplicity of a sampled timeslice; a weight of 0 leaves the timeslice out. The time a timeslice stands for is share x width x weight, kept in the derived column total; over the timeslices of positive weight it sums to the part of the year the calendar covers.

one <- function(id, timeslice) {
  w <- calendar_width(calendars[[id]])
  cbind(calendar = id, w[match(timeslice, w[[1]]), -1])
}
rbind(one("d365_h24", "d015_h12"), one("s4_h24", "WIN_h12"),
      one("s4_hp3", "WIN_DAY"), one("wd7", "MON"), one("m12", "m01")) |>
  knitr::kable(row.names = FALSE)
calendar unit_hours width weight hours
d365_h24 1 1 1.00000 1.000
s4_h24 1 1 90.00000 90.000
s4_hp3 1 11 90.00000 990.000
wd7 24 1 52.14286 1251.429
m12 744 1 1.00000 744.000

calendar_width() gives the three numbers with the unit in hours (unit_hours is share times the hours of the year) and their product, the hours the timeslice stands for. A dated hour is one hour, once. An hour of an average day is the same hour repeated on 90 days. The DAY block of s4_hp3 is 11 hours wide in a day and repeats on 90 days. A weekday is one day in each of the 52.14 weeks of the year. A month is its own unit, once.

A timeframe may declare its unit in hours in meta$units; the share of its timeslices is then that unit and width counts it. HOUR and the hour types declare one hour. A timeframe without a declared unit is its own unit. The two forms of one timeslice stand for the same time: a month as 744 hours with width 1, or as one hour with width 744 and units = c(MONTH = 1).

Full, sampled and average-day calendars

A representative or typical day can be modelled in two ways: by sampling, which keeps real days of the year and leaves the rest out, or by averaging, which merges all the days of a season into one average day.

Three calendars can describe the same hours of the day and still be different things. A full calendar enumerates every hour of the year. A sample keeps some of those hours and leaves the rest out. An average-day calendar keeps no particular day: each of its timeslices is one hour of an average day, repeated on all the days of a season. A fourth, s4_hp3, merges the hours of that day into three blocks.

full    <- calendars$d365_h24
sampled <- filter_calendar(full, "YDAY",
                           timeslice = c("d015", "d105", "d196", "d288"))
average <- calendars$s4_h24

blocks  <- calendars$s4_hp3

one <- function(cal, name, timeslice) {
  lt <- calendar_leaftable(cal)
  i <- match(timeslice, calendar_timeslices(cal))
  data.frame(calendar = name, timeslice = timeslice,
             share_hours = round(lt$share[i] * 8760, 2),
             width = lt$width[i], weight = lt$weight[i],
             covered = round(unname(calendar_coverage(cal)), 4))
}
rbind(one(full, "full", "d015_h12"),
      one(sampled, "sampled", "d015_h12"),
      one(average, "average day", "WIN_h12"),
      one(blocks, "hour types", "WIN_DAY")) |>
  knitr::kable(row.names = FALSE)
calendar timeslice share_hours width weight covered
full d015_h12 1 1 1 1.000
sampled d015_h12 1 1 1 0.011
average day WIN_h12 1 1 90 1.000
hour types WIN_DAY 1 11 90 1.000

share_hours is the unit of the timeslice in hours, width its length in units, weight the number of times it repeats, and covered the part of the year the calendar stands for. Every one of the four timeslices is counted in hours of one hour. An hour of the full calendar and of the sample is there once; the sample covers 96 of the 8,760 hours, and the rest of the year is absent from it: it is read against the calendar it was taken from. The winter noon of the average day is one hour repeated on the 90 days of winter, so the averaged calendar covers the year by itself and needs no parent calendar. The DAY block of the hour types uses width and weight together: it is 11 hours wide and repeats on the same 90 days. A sample is known by the source it records or by timeslices of weight 0; an averaged design has neither.

The figures show the same difference. The sample is drawn against the calendar it was taken from, with the hours it leaves out empty; the average day fills each season with its 24 hours.

ggplot2::autoplot(full, max_segments = 1000)

ggplot2::autoplot(sampled, max_segments = 1000)

ggplot2::autoplot(average)

The workhorses

The designs most models start from:

calendars$d365_h24    # 8,760 hourly timeslices, the full-resolution classic
#> Calendar: d365_h24 
#> Timeframes (2):
#>   - YDAY (365) [token: d365, alignment: drop_feb29]
#>   - HOUR (8760) [token: h24, unit: 1 h]
#> Leaf timeslices: 8760
#> year_fraction: 1
#> year_start: month=1, day=1
#> utc_offset_minutes: 0
calendars$m12_h24     # 288 timeslices: average day per month
#> Calendar: m12_h24 
#> Timeframes (2):
#>   - MONTH (12) [token: m12]
#>   - HOUR (288) [token: h24, unit: 1 h]
#> Leaf timeslices: 288
#> year_fraction: 1
#> year_start: month=1, day=1
#> utc_offset_minutes: 0
calendars$q4_h24      # 96 timeslices: average day per quarter
#> Calendar: q4_h24 
#> Timeframes (2):
#>   - QUARTER (4) [token: q4]
#>   - HOUR (96) [token: h24, unit: 1 h]
#> Leaf timeslices: 96
#> year_fraction: 1
#> year_start: month=1, day=1
#> utc_offset_minutes: 0

The m12_md* family carries a real month/day structure (non-Cartesian — February is short), useful when data arrives keyed by month and day-of-month:

md <- calendars$m12_md365
nrow(calendar_leaftable(md))
#> [1] 365
head(calendar_leaftable(md)[calendar_leaftable(md)$MONTH == "m02", ], 2)
#>    MONTH    MDAY       share width weight       total
#> 32   m02 m02_d01 0.002739726     1      1 0.002739726
#> 33   m02 m02_d02 0.002739726     1      1 0.002739726
tail(calendar_leaftable(md)[calendar_leaftable(md)$MONTH == "m02", ], 1)   # Feb ends at d28
#>    MONTH    MDAY       share width weight       total
#> 59   m02 m02_d28 0.002739726     1      1 0.002739726
datetime_to_timeslice(as.Date(c("2021-03-15", "2020-02-29")), md)  # Feb 29 -> NA
#> [1] "m03_d15" NA

Type axes: SEASON, DAYTYPE and the hour types

Two derived timeframes and the hour-type blocks of HOUR back the compact “averaged periods” designs; all three are datetime-convertible:

  • SEASON — meteorological: WIN = Dec–Feb, SPR, SUM, FAL
  • DAYTYPE — WORKDAY (Mon–Fri) / WEEKEND
  • the hour types (token hp3, timeframe HOUR) — DAY (h06–h16), PEAK (h17–h20), NIGHT (h21–h05), in their order within a day: timeslices of HOUR that are 11, 4 and 9 hours wide
dtm <- as.POSIXct(c("2021-01-15 03:00", "2021-01-16 18:00",
                    "2021-07-14 12:00"), tz = "UTC")
datetime_to_timeslice(dtm, calendars$s4_hp3)
#> [1] "WIN_NIGHT" "WIN_PEAK"  "SUM_DAY"
datetime_to_timeslice(dtm, calendars$wk2_h24)
#> [1] "WORKDAY_h03" "WEEKEND_h18" "WORKDAY_h12"

The mappings are defaults; a custom token (e.g. a different peak window) is registered with register_calendar_token() and a custom calendar built from it.

Fiscal years: the fy04_* family

Many reporting systems run April..March — Indian national statistics most prominently. The fy04_* entries (fy04_m12, fy04_m12_h24, fy04_q4, fy04_q4_h24, fy04_d365, fy04_d365_h24) anchor the model year to April 1: model year y spans [y-04-01, y+1-04-01), and the anchored YEAR is the starting Gregorian year (Indian “FY 2021-22” is model year 2021).

fy <- calendar("fy04_m12")
fy@members$MONTH                       # April first
#>  [1] "m04" "m05" "m06" "m07" "m08" "m09" "m10" "m11" "m12" "m01" "m02" "m03"
g <- expand_calendar(fy, 2021, by = "day")
range(as.Date(g$datetime))             # 2021-04-01 .. 2022-03-31
#> [1] "2021-04-01" "2022-03-31"
datetime_to_timeslice(as.Date("2022-01-15"), fy)   # January stays m01
#> [1] "m01"

Two rules keep fiscal calendars unambiguous:

  • Labels stay Gregorian. April is m04 and Apr–Jun is Q2, always; only the member order starts at the anchor (m04..m03, Q2,Q3,Q4,Q1).
  • The catalog is UTC. Data already in Indian local time maps as-is; to map true-UTC instants at Indian midnight boundaries, pass the IST offset: calendar("fy04_m12", utc_offset_minutes = 330L). (Constant offsets only; Olson time zones and DST are not supported.)

Leap years need no special handling: fy04_d365 drops the Feb 29 that falls in the following Gregorian year (FY2023 contains 2024-02-29) and keeps March aligned to d365. Any other anchor works by argument on any design: calendar("m12", year_start = list(month = 7L, day = 1L)).

Visualizing calendars

(For the full plot-type tour on real data – heatmap layers, wall calendars, profiles, ribbons, duration curves – see Visualization.)

autoplot() (or plot()) draws a calendar’s structure as an icicle: one band per timeframe with the ANNUAL root on top, rectangle widths equal to timeslice shares, x spanning the year on [0, 1]. The gradient restarts inside each parent (color_pattern = "within"), so nested structure is visible at a glance — the catalog tour above shows one per family. Dense calendars stay fast: rows beyond max_segments are binned before drawing:

library(ggplot2)
autoplot(calendars$d365_h24, max_segments = 1000)

The same figure carries data: pass data =/z = and every band fills with the value recast to that band’s resolution — here a year of Reykjavik wind, from the annual mean down to the month × hour grid:

wind <- merra2_cities |>
  filter(city == "Reykjavik") |>
  mutate(timeslice = datetime_to_timeslice(datetime, calendars$m12_h24)) |>
  summarise(W50M = mean(W50M), .by = timeslice)

autoplot(calendars$m12_h24, data = wind, z = "W50M",
         rule = "weighted_mean", year = 2019) +
  labs(fill = "m/s")

The geometry itself is available without ggplot2 via calendar_layout() — a plain data.frame of rectangles, usable from any plotting system:

head(calendar_layout(calendars$q4_h24), 7)
#>   timeframe  label timeslice rank       xmin       xmax ymin ymax      total
#> 1    ANNUAL ANNUAL    ANNUAL    0 0.00000000 1.00000000    2  2.9 1.00000000
#> 2   QUARTER     Q1        Q1    1 0.00000000 0.24657534    1  1.9 0.24657534
#> 3   QUARTER     Q2        Q2    1 0.24657534 0.49589041    1  1.9 0.24931507
#> 4   QUARTER     Q3        Q3    1 0.49589041 0.74794521    1  1.9 0.25205479
#> 5   QUARTER     Q4        Q4    1 0.74794521 1.00000000    1  1.9 0.25205479
#> 6      HOUR    h00    Q1_h00    2 0.00000000 0.01027397    0  0.9 0.01027397
#> 7      HOUR    h01    Q1_h01    2 0.01027397 0.02054795    0  0.9 0.01027397
#>          share width weight order within
#> 1 1.0000000000     1      1     1      1
#> 2 0.2465753425     1      1     1      1
#> 3 0.2493150685     1      1    25      2
#> 4 0.2520547945     1      1    49      3
#> 5 0.2520547945     1      1    73      4
#> 6 0.0001141553     1     90     1      1
#> 7 0.0001141553     1     90     2      2

gg_calendar(type = "heatmap") is the data-on-calendar heatmap: it takes a data.frame keyed by timeslice (or none, which shows the share structure). The layout follows the hierarchy — finest timeframe on y, next on x, coarser timeframes as facets.

The axes carry the token labels of the timeframes, so m12_h24 is a grid of 12 months by 24 hours of the day, although HOUR has 288 members (one per month and hour):

x <- data.frame(timeslice = calendar_timeslices(calendars$m12_h24))
x$load <- 80 + 40 * sin(seq(0, 6 * pi, length.out = nrow(x)))
gg_calendar(calendars$m12_h24, data = x, z = "load", palette = "C",
            type = "heatmap")

Wall calendars

gg_calendar(type = "wall") draws the familiar wall form: one facet per month, day cells in a week grid. The weekday arrangement is year-specific (which weekday a date falls on comes from the base grid), so pass year=; without it the layout falls back to a year-free 7-wide sequence with a message.

gg_calendar(calendar("m12_md365"), type = "wall", year = 2021)

Daily (or finer – aggregated by fun) data fills the cells, and the fiscal calendars facet April-first:

cal <- calendar("fy04_d365")
x <- data.frame(timeslice = calendar_timeslices(cal),
                v = cumsum(rnorm(365)))
gg_calendar(cal, data = x, z = "v", year = 2021, type = "wall")

The pieces are reusable on their own: calendar_weekdays(cal, year, week_start = "MON") tabulates the day layer’s dates, weekdays, week-of-month rows and an anchored week-of-year (fiscal weeks from April 1 for fy04_*; week_start accepts any of MON..SUN), and calendar_wall_layout() returns the plain tile frame the figure is built from.

Recasting across the catalog

Any two catalog calendars convert into each other; totals are conserved under rule = "sum":

x <- data.frame(timeslice = calendar_timeslices(calendars$m12),
                energy = c(310, 280, 300, 250, 220, 230,
                           260, 270, 240, 250, 280, 320))
recast_calendar(x, calendars$m12, calendars$s4, year = 2021, rule = "sum",
       by = "day")
#>   timeslice energy
#> 1       WIN    910
#> 2       SPR    770
#> 3       SUM    760
#> 4       FAL    770
sum(x$energy)
#> [1] 3210

Or aggregate within one calendar via the ANNUAL root:

recast_calendar(x, calendars$m12, target = "ANNUAL", year = 2021, rule = "sum",
       by = "day")
#>   timeslice energy
#> 1    ANNUAL   3210