Rev 50237 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/DateTimeClasses.Rd% Part of the R package, http://www.R-project.org% Copyright 1995-2009 R Core Development Team% Distributed under GPL 2 or later\name{DateTimeClasses}% implementation mostly in ../R/datetime.R\alias{DateTimeClasses}\alias{POSIXct}\alias{POSIXlt}\alias{POSIXt}\alias{print.POSIXct}\alias{print.POSIXlt}\alias{summary.POSIXct}\alias{summary.POSIXlt}\alias{+.POSIXt}\alias{-.POSIXt}\alias{Ops.POSIXt}\alias{Math.POSIXt}\alias{Summary.POSIXct}\alias{Math.POSIXlt}\alias{Summary.POSIXlt}\alias{[.POSIXct}\alias{[<-.POSIXct}\alias{[[.POSIXct}\alias{[.POSIXlt}\alias{[<-.POSIXlt}\alias{as.data.frame.POSIXct}\alias{as.data.frame.POSIXlt}\alias{.leap.seconds}\alias{is.na.POSIXlt}\alias{all.equal.POSIXct}\alias{c.POSIXct}\alias{c.POSIXlt}\alias{as.matrix.POSIXlt}\alias{mean.POSIXct}\alias{mean.POSIXlt}\alias{str.POSIXt}\alias{check_tzones}\alias{duplicated.POSIXlt}\alias{unique.POSIXlt}\alias{split.POSIXct}\alias{date-time} % linked to in difftime.Rd\title{Date-Time Classes}\description{Description of the classes \code{"POSIXlt"} and \code{"POSIXct"}representing calendar dates and times (to the nearest second).}\usage{\method{print}{POSIXct}(x, \dots)\method{summary}{POSIXct}(object, digits = 15, \dots)\special{time + z}\special{z + time}\special{time - z}\special{time1 lop time2}}\arguments{\item{x, object}{An object to be printed or summarized from one of thedate-time classes.}\item{digits}{Number of significant digits for the computations:should be high enough to represent the least important time unitexactly.}\item{\dots}{Further arguments to be passed from or to other methods.}\item{time}{date-time objects}\item{time1, time2}{date-time objects or character vectors. (Charactervectors are converted by \code{\link{as.POSIXct}}.)}\item{z}{a numeric vector (in seconds)}\item{lop}{One of \code{==}, \code{!=}, \code{<}, \code{<=}, \code{>}or \code{>=}.}}\details{There are two basic classes of date/times. Class \code{"POSIXct"}represents the (signed) number of seconds since the beginning of 1970as a numeric vector. Class \code{"POSIXlt"} is a named list ofvectors representing\describe{\item{\code{sec}}{0--61: seconds}\item{\code{min}}{0--59: minutes}\item{\code{hour}}{0--23: hours}\item{\code{mday}}{1--31: day of the month}\item{\code{mon}}{0--11: months after the first of the year.}\item{\code{year}}{years since 1900.}\item{\code{wday}}{0--6 day of the week, starting on Sunday.}\item{\code{yday}}{0--365: day of the year.}\item{\code{isdst}}{Daylight Savings Time flag. Positive if inforce, zero if not, negative if unknown.}}The classes correspond to the POSIX/C99 constructs of \sQuote{calendartime} (the \code{time_t} data type) and \sQuote{local time} (orbroken-down time, the \code{struct tm} data type), from which theyalso inherit their names. The components of \code{"POSIXlt"} areinteger vectors, except \code{sec}.\code{"POSIXct"} is more convenient for including in data frames, and\code{"POSIXlt"} is closer to human-readable forms.A virtual class \code{"POSIXt"} inherits from both of the classes: itis used to allow operations such as subtraction to mix the two classes.Components \code{wday} and \code{yday} of \code{"POSIXlt"} are forinformation, and are not used in the conversion to calendar time.However, \code{isdst} is needed to distinguish times at the end ofDST: typically 1am to 2am occurs twice, first in DST and then instandard time. At all other times \code{isdst} can be deduced fromthe first six values, but the behaviour if it is set incorrectly isplatform-dependent.% POSIX says used 'initially', so it should probably be ignored.% glibc has a complicated fixup.Logical comparisons and limited arithmetic are available for bothclasses. One can add or subtract a number of seconds from a date-timeobject, but not add two date-time objects. Subtraction of twodate-time objects is equivalent to using \code{\link{difftime}}. Beaware that \code{"POSIXlt"} objects will be interpreted as being inthe current timezone for these operations, unless a timezone has beenspecified.\code{"POSIXlt"} objects will often have an attribute \code{"tzone"},a character vector of length 3 giving the timezone name from the\env{TZ} environment variable and the names of the base timezoneand the alternate (daylight-saving) timezone. Sometimes this mayjust be of length one, giving the timezone name.\code{"POSIXct"} objects may also have an attribute \code{"tzone"}, acharacter vector of length one. If set to a non-empty value, it willdetermine how the object is converted to class \code{"POSIXlt"} and inparticular how it is printed. This is usually desirable, but if youwant to specify an object in a particular timezone but to be printedin the current timezone you may want to remove the \code{"tzone"}attribute (e.g. by \code{c(x)}).Unfortunately, the conversion is complicated by the operation of timezones and leap seconds (24 days have been 86401 seconds long so far:the times of the extra seconds are in the object \code{.leap.seconds}).The details of this are entrusted to the OS services where possible.This always covers the period 1970--2037, and on most machinesback to 1902 (when time zones were in their infancy). Outsidethe platform limits we use our own C code. This uses the offset fromGMT in use either for 1902 (when there was no DST) or that predictedfor one of 2030 to 2037 (chosen so that the likely DST transition daysare Sundays), and uses the alternate (daylight-saving) timezone onlyif \code{isdst} is positive or (if \code{-1}) if DST was predicted tobe in operation in the 2030s on that day. (There is no reason tosuppose that the DST rules will remain the same in the future, andindeed the US legislated in 2005 to change its rules as from 2007,with a possible future reversion.)It seems that some rare systems use leap seconds, but most ignorethem (as required by POSIX). This is detected and corrected for atbuild time, so all \code{"POSIXct"} times used by \R do not includeleap seconds. (Conceivably this could be wrong if the system haschanged since build time, just possibly by changing locales or the\file{zoneinfo} database.)Using \code{\link{c}} on \code{"POSIXlt"} objects converts them to thecurrent time zone, and on \code{"POSIXct"} objects drops any\code{"tzone"} attributes (even if they are all marked with the sametime zone).A few times have specific issues. First, the leapseconds are ignored,and real times such as \code{"2005-12-31 23:59:60"} are (probably)treated as the next second. However, they will never be generated by\R, and are unlikely to arise as input. Second, on some OSes there isa problem in the POSIX/C99 standard with \code{"1969-12-31 23:59:59"},which is \code{-1} in calendar time and that value is on those OSesalso used as an error code. Thus \code{as.POSIXct("1969-12-3123:59:59", format="\%Y-\%m-\%d \%H:\%M:\%S", tz="UTC")} may give\code{NA}, and hence \code{as.POSIXct("1969-12-31 23:59:59",tz="UTC")} will give \code{"1969-12-31 23:59:50"}. Other OSes(including the code used by \R on Windows) report errors separatelyand so are able to handle that time as valid.}\section{Sub-second Accuracy}{Classes \code{"POSIXct"} and \code{"POSIXlt"} are able to expressfractions of a second. (Conversion of fractions between the two formsmay not be exact, but will have better than microsecond accuracy.)Fractional seconds are printed only if\code{\link{options}("digits.secs")} is set: see \code{\link{strftime}}.}\seealso{\link{Dates} for dates without times.\code{\link{as.POSIXct}} and \code{\link{as.POSIXlt}} for conversionbetween the classes.\code{\link{strptime}} for conversion to and from characterrepresentations.\code{\link{Sys.time}} for clock time as a \code{"POSIXct"} object.\code{\link{difftime}} for time intervals.\code{\link{cut.POSIXt}}, \code{\link{seq.POSIXt}},\code{\link{round.POSIXt}} and \code{\link{trunc.POSIXt}} for methodsfor these classes.\code{\link{weekdays}} for convenience extraction functions.}\section{Warning}{Some Unix-like systems (especially Linux ones) do not have \code{"TZ"}set, yet have internal code that expects it (as does POSIX). We havetried to work around this, but if you get unexpected results trysetting \code{"TZ"}. See \code{\link{as.POSIXlt}} for valid settings.}\examples{(z <- Sys.time()) # the current date, as class "POSIXct"Sys.time() - 3600 # an hour agoas.POSIXlt(Sys.time(), "GMT") # the current time in GMTformat(.leap.seconds) # all 24 leapseconds in your timezoneprint(.leap.seconds, tz="PST8PDT") # and in Seattle's}\keyword{utilities}\keyword{chron}