Create a set of labels for a time period based on the start and duration of
the period. The format is configurable using the start and end dates and the
dfmt
and ifmt
parameters, however if the time period has names then these
are used in preference.
Usage
# S3 method for class 'time_period'
labels(
object,
...,
dfmt = "%d/%b",
ifmt = "{start} — {end}",
na.value = "Unknown"
)
Examples
eg = as.time_period(Sys.Date()+0:10*7, anchor="start")
#> No unit given. Guessing a sensible value from the dates gives: 7d 0H 0M 0S
#> (N.B. this message will only be displayed once.)
labels(eg)
#> [1] "21/Aug — 27/Aug" "28/Aug — 03/Sep" "04/Sep — 10/Sep" "11/Sep — 17/Sep"
#> [5] "18/Sep — 24/Sep" "25/Sep — 01/Oct" "02/Oct — 08/Oct" "09/Oct — 15/Oct"
#> [9] "16/Oct — 22/Oct" "23/Oct — 29/Oct" "30/Oct — 05/Nov"
labels(eg, ifmt="{start}", dfmt="%d/%b/%y")
#> [1] "21/Aug/25" "28/Aug/25" "04/Sep/25" "11/Sep/25" "18/Sep/25" "25/Sep/25"
#> [7] "02/Oct/25" "09/Oct/25" "16/Oct/25" "23/Oct/25" "30/Oct/25"
labels(eg, ifmt="until {end}", dfmt="%d %b %Y")
#> [1] "until 27 Aug 2025" "until 03 Sep 2025" "until 10 Sep 2025"
#> [4] "until 17 Sep 2025" "until 24 Sep 2025" "until 01 Oct 2025"
#> [7] "until 08 Oct 2025" "until 15 Oct 2025" "until 22 Oct 2025"
#> [10] "until 29 Oct 2025" "until 05 Nov 2025"
# labels retained in constructor:
eg2 = Sys.Date()+0:10*7
names(eg2) = paste0("week ",0:10)
labels(eg2)
#> [1] "week 0" "week 1" "week 2" "week 3" "week 4" "week 5" "week 6"
#> [8] "week 7" "week 8" "week 9" "week 10"
labels(as.time_period(eg2, anchor="start"))
#> [1] "week 0" "week 1" "week 2" "week 3" "week 4" "week 5" "week 6"
#> [8] "week 7" "week 8" "week 9" "week 10"