Parsing dates
Lubridate's parsing functions read strings into R as
POSIXct date-time objects. Users should choose the
function whose name models the order in which the year
('y'), month ('m') and day ('d') elements appear the
string to be parsed: dmy
,
myd
, ymd
, ydm
,
dym
, mdy
,
ymd_hms
).
Lubridate can also parse partial dates from strings into
period
objects with the functions
hm
, hms
and ms
.
Manipulating dates
Lubridate distinguishes between moments in time (known as
instants
) and spans of time (known as
timespans
). Time spans are further
separated into durations
,
periods
and intervals
.
Instants
Instants are specific moments of time. Date, POSIXct, and
POSIXlt are the three object classes Base R recognizes as
instants. is.Date
tests whether an object
inherits from the Date class. is.POSIXt
tests whether an object inherits from the POSIXlt or
POSIXct classes. is.instant
tests whether
an object inherits from any of the three classes.
now
returns the current system time as a
POSIXct object. today
returns the current
system date. For convenience, 1970-01-01 00:00:00 is
saved to origin
. This is the instant from
which POSIXct times are calculated from.
Instants are a combination of measurements on different
units (i.e, years, months, days, etc.). The individual
values for these units can be extracted from an instant
and set with the accessor functions second
,
minute
, hour
,
day
, yday
,
mday
, wday
,
week
, month
,
year
, tz
, and
dst
. Note: the accessor functions are
named after the singular form of an element. They
shouldn't be confused with the period helper functions
that have the plural form of the units as a name (e.g,
seconds
).
Rounding dates
Instants can be rounded to a convenient unit using the
functions ceiling_date
,
floor_date
andround_date
.
Time zones
Lubridate provides two helper functions for working with
time zones. with_tz
changes the time zone
in which an instant is displayed. The clock time
displayed for the instant changes, but the moment of time
described remains the same. force_tz
changes only the time zone element of an instant. The
clock time displayed remains the same, but the resulting
instant describes a new moment of time.
Timespans
A timespan is a length of time that may or not be
connected to a particular instant. For example, three
months or an hour and a half. Base R uses difftime class
objects to record timespans. Lubridate creates three
more timespan classes, intervals
,
periods
and durations
, to
better navigate the nuances of time.
is.difftime
tests whether an object
inherits from the difftime class.
is.timespan
tests whether an object
inherits from any of the four timespan classes. Lubridate
alters the subtraction method for dates to create
interval objects, which can be used like difftimes but
preserve more information than difftimes. To create a
difftime with lubridate use make_difftime
instead of subtraction.
Durations
durations
measure the exact time of span
that occurs between two instants. This can create
unexpected results if a leap second, leap year, or change
in daylight savings time (DST) occurs in the interval.
Duration objects also inherit from the difftime class.
Functions for working with durations include
is.duration
, as.duration
and
new_duration
. dseconds
,
dminutes
, dhours
,
ddays
, dweeks
,
dyears
and new_duration
quickly create durations of convenient lengths.
Periods
periods
measure the change in clock time
that occurs between two instants. Periods provide robust
predictions #' of clock time in the presence of leap
seconds, leap years, and #' changes in DST.
Functions for working with periods include
is.period
, as.period
and
new_period
. seconds
,
minutes
, hours
,
days
, weeks
,
months
and years
quickly
create periods of convenient lengths.
Intervals
Intervals are timespans that begin at a specific instant. Intervals provide the only reliable way to convert between periods and durations. Intervals inherit from both difftime and duration object classes. They can be used as a difftime but contain an extra attribute which records the start time of the interval. When lubridate is loaded, subtracting two dates creates an interval by default.
Functions for working with intervals include
is.interval
, as.interval
and
new_interval
.
Miscellaneous
decimal_date
converts an instant to a
decimal of its year. leap_year
tests
whether an instant occurs during a leap year.
pretty.dates
provides a method of making
pretty breaks for date-times lakers
is a
data set that contains information about the Los Angeles
Lakers 2008-2009 basketball season.