IDate is a date class derived from Date. It has the same
internal representation as the Date class, except the storage
mode is integer. IDate is a relatively simple wrapper, and it
should work in almost all situations as a replacement for
Date. The main limitations of integer storage are (1) fractional
dates are not supported (use IDateTime() instead) and (2) the
range of supported dates is bounded by .Machine$integer.max
dates away from January 1, 1970 (a rather impractical limitation as
these dates are roughly 6 million years in the future/past, but
consider this your caveat).
Functions that use Date objects generally work for
IDate objects. This package provides specific methods for
IDate objects for mean, cut, seq, c,
rep, and split to return an IDate object.
ITime is a time-of-day class stored as the integer number of
seconds in the day. as.ITime does not allow days longer than 24
hours. Because ITime is stored in seconds, you can add it to a
POSIXct object, but you should not add it to a Date
object.
We also provide S3 methods to convert to and from Date and POSIXct.
ITime is time zone-agnostic. When converting ITime and
IDate to POSIXct with as.POSIXct, a time zone may be specified.
Inputs like '2018-05-15 12:34:56.789' are ambiguous from the perspective of an ITime object -- the method of coercion of the 789 milliseconds is controlled by the ms argument to relevant methods. The default behavior (ms = 'truncate') is to use as.integer, which has the effect of truncating anything after the decimal. Alternatives are to round to the nearest integer (ms = 'nearest') or to round up (ms = 'ceil').
In as.POSIXct methods for ITime and IDate, the
second argument is required to be tz based on the generic
template, but to make converting easier, the second argument is
interpreted as a date instead of a time zone if it is of type
IDate or ITime. Therefore, you can use either of the
following: as.POSIXct(time, date) or as.POSIXct(date,
time).
IDateTime takes a date-time input and returns a data table with
columns date and time.
Using integer storage allows dates and/or times to be used as data table
keys. With positive integers with a range less than 100,000, grouping
and sorting is fast because radix sorting can be used (see
sort.list).
Several convenience functions like hour and quarter are
provided to group or extract by hour, month, and other date-time
intervals. as.POSIXlt is also useful. For example,
as.POSIXlt(x)$mon is the integer month. The R base convenience
functions weekdays, months, and quarters can also
be used, but these return character values, so they must be converted to
factors for use with data.table. isoweek is ISO 8601-consistent.
The round method for IDate's is useful for grouping and plotting.
It can round to weeks, months, quarters, and years. Similarly, the round
and trunc methods for ITime's are useful for grouping and plotting.
They can round or truncate to hours and minutes.
Note for ITime's with 30 seconds, rounding is inconsistent due to rounding off a 5.
See 'Details' in round for more information.
Functions like week() and isoweek() provide week numbering functionality.
week() computes completed or fractional weeks within the year,
while isoweek() calculates week numbers according to ISO 8601 standards,
which specify that the first week of the year is the one containing the first Thursday.
This convention ensures that week boundaries align consistently with year boundaries,
accounting for both year transitions and varying day counts per week.