The as.POSIX*
functions convert an object to one of the two
classes used to represent date/times (calendar dates plus time to the
nearest second). They can convert a wide variety of objects,
including objects of the other class and of classes "Date"
,
"date"
(from package date),
"chron"
and "dates"
(from package
chron) to these classes. Dates without times are
treated as being at midnight UTC. They can also convert character strings of the formats
"2001-02-03"
and "2001/02/03"
optionally followed by
white space and a time in the format "14:52"
or
"14:52:03"
. (Formats such as "01/02/03"
are ambiguous
but can be converted via a format specification by
strptime
.) Fractional seconds are allowed.
Alternatively, format
can be specified for character vectors or
factors: if it is not specified and no standard format works for
all non-NA
inputs an error is thrown.
If format
is specified, remember that some of the format
specifications are locale-specific, and you may need to set the
LC_TIME
category appropriately via
Sys.setlocale
. This most often affects the use of
%b
, %B
(month names) and %p
(AM/PM).
Logical NA
s can be converted to either of the classes, but no
other logical vectors can be.
If you are given a numeric time as the number of seconds since an
epoch, see the examples.
Character input is first converted to class "POSIXlt"
by
strptime
: numeric input is first converted to
"POSIXct"
. Any conversion that needs to go between the two
date-time classes requires a time zone: conversion from
"POSIXlt"
to "POSIXct"
will validate times in the
selected time zone. One issue is what happens at transitions
to and from DST, for example in the UK
as.POSIXct(strptime("2011-03-27 01:30:00", "%Y-%m-%d %H:%M:%S"))
as.POSIXct(strptime("2010-10-31 01:30:00", "%Y-%m-%d %H:%M:%S"))
are respectively invalid (the clocks went forward at 1:00 GMT to 2:00
BST) and ambiguous (the clocks went back at 2:00 BST to 1:00 GMT). What
happens in such cases is OS-specific: one should expect the first to
be NA
, but the second could be interpreted as either BST or
GMT (and common OSes give both possible values). Note too (see
strftime
), OS facilities may not format invalid times correctly.