Define a new S7 class representing a time unit for use in a mixtime calendar.
Time units are the building blocks of calendars: each unit represents a
specific temporal component (e.g., day, month, year) and carries a step size
n. Units are combined via new_calendar() to form a complete calendar
system.
Usage
new_time_unit(
name,
parent = mt_unit,
package = topNamespaceName(parent.frame()),
properties = list(),
abstract = FALSE,
constructor = NULL,
validator = NULL
)Arguments
- name
A string naming the new S7 class (e.g.,
"tu_my_year"). By convention, time unit class names are prefixed withtu_.- parent
The parent S7 class. Should be one of
mt_unit,mt_tz_unit, ormt_loc_unit, or a class that itself inherits from one of these. Defaults tomt_unit.- package
A string giving the package name that owns the class. Defaults to the name of the calling namespace, so typically does not need to be set explicitly.
- properties
A named list of additional
S7::new_property()definitions beyond those inherited fromparent. Each entry becomes a slot on instances of the new class. Defaults to an empty list.- abstract
Logical. If
TRUEthe class cannot be instantiated directly; it serves only as a base for further subclassing.- constructor
A function to use as the class constructor, or
NULL(default) to let S7 generate one from the parent constructor and properties. Custom constructors must acceptcalendar = attr(sys.function(), "cal")and forward it to the parent constructor ascalendar = calendarto retain calendar membership.- validator
A function of one argument (
self) that returnsNULLwhen the object is valid, or a character string describing the problem. SeeS7::new_class()for details.
Value
An S7 class object, as returned by S7::new_class().
Details
Choose the parent class based on the type of time the unit represents:
mt_unit— abstract or calendar-only time (no timezone or location context; e.g., Gregorian year or ISO week).mt_tz_unit— civil time with a timezone (e.g., a clock hour that needstzto resolve wall-clock ambiguity).mt_loc_unit— astronomical time with a geographic location (e.g., a solar day tied to observer longitude/latitude).
Use new_time_unit() for units included in new_calendar(). Its generated
constructor records the calendar on each instance, including when the parent
has a custom constructor. A bare S7::new_class() constructor may only
forward ... to its parent and cannot infer the calling calendar.
See also
mt_unit,mt_tz_unit,mt_loc_unitfor the base classes to inherit from.new_calendar()for assembling time units into a calendar.unit_scheme()for naming a time unit (e.g. "day", "days" and "D").S7::new_class()for the underlying S7 class constructor.
Examples
# An abstract unit with no properties
tu_my_seq <- new_time_unit("tu_my_seq", parent = mt_unit)
# A civil-time unit with an extra property
tu_my_quarter <- new_time_unit(
"tu_my_quarter",
parent = mt_tz_unit,
properties = list(
fiscal = S7::new_property(S7::class_logical, default = FALSE)
)
)