Rev 78076 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/as.POSIXlt.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2018 R Core Team% Distributed under GPL 2 or later\name{as.POSIX*}\alias{as.POSIXct}\alias{as.POSIXct.default}\alias{as.POSIXct.POSIXlt}\alias{as.POSIXct.date}\alias{as.POSIXct.dates}\alias{as.POSIXct.Date}\alias{as.POSIXct.numeric}\alias{as.POSIXlt}\alias{as.POSIXlt.Date}\alias{as.POSIXlt.date}\alias{as.POSIXlt.dates}\alias{as.POSIXlt.POSIXct}\alias{as.POSIXlt.factor}\alias{as.POSIXlt.character}\alias{as.POSIXlt.default}\alias{as.POSIXlt.numeric}\alias{as.double.POSIXlt}\title{Date-time Conversion Functions}\description{Functions to manipulate objects of classes \code{"POSIXlt"} and\code{"POSIXct"} representing calendar dates and times.}\usage{as.POSIXct(x, tz = "", \dots)as.POSIXlt(x, tz = "", \dots)\method{as.POSIXlt}{character}(x, tz = "", format,tryFormats = c("\%Y-\%m-\%d \%H:\%M:\%OS","\%Y/\%m/\%d \%H:\%M:\%OS","\%Y-\%m-\%d \%H:\%M","\%Y/\%m/\%d \%H:\%M","\%Y-\%m-\%d","\%Y/\%m/\%d"),optional = FALSE, \dots)\method{as.POSIXlt}{default}(x, tz = "",optional = FALSE, \dots)\method{as.POSIXlt}{numeric}(x, tz = "", origin, \dots)\method{as.double}{POSIXlt}(x, \dots)}\arguments{\item{x}{\R object to be converted.}\item{tz}{time zone specification to be used for the conversion,\emph{if one is required}. System-specific (see \link{time zones}),but \code{""} is the current time zone, and \code{"GMT"} is UTC(Universal Time, Coordinated). Invalid values are most commonlytreated as UTC, on some platforms with a warning.}\item{\dots}{further arguments to be passed to or from other methods.}\item{format}{character string giving a date-time format as usedby \code{\link{strptime}}.}\item{tryFormats}{\code{\link{character}} vector of \code{format}strings to try if \code{format} is not specified.}\item{optional}{\code{\link{logical}} indicating to return \code{NA}(instead of signalling an error) if the format guessing does not succeed.}\item{origin}{a date-time object, or something which can be coerced by\code{as.POSIXct(tz = "GMT")} to such an object.}}\details{The \code{as.POSIX*} functions convert an object to one of the twoclasses used to represent date/times (calendar dates plus time to thenearest second). They can convert objects of the other class and ofclass \code{"Date"} to these classes. Dates without times aretreated as being at midnight UTC.They can also convert character strings of the formats\code{"2001-02-03"} and \code{"2001/02/03"} optionally followed bywhite space and a time in the format \code{"14:52"} or\code{"14:52:03"}. (Formats such as \code{"01/02/03"} are ambiguousbut can be converted via a format specification by\code{\link{strptime}}.) Fractional seconds are allowed.Alternatively, \code{format} can be specified for character vectors orfactors: if it is not specified and no standard format works forall non-\code{NA} inputs an error is thrown.If \code{format} is specified, remember that some of the formatspecifications are locale-specific, and you may need to set the\code{LC_TIME} category appropriately \emph{via}\code{\link{Sys.setlocale}}. This most often affects the use of\code{\%b}, \code{\%B} (month names) and \code{\%p} (AM/PM).Logical \code{NA}s can be converted to either of the classes, but noother logical vectors can be.If you are given a numeric time as the number of seconds since anepoch, see the examples.Character input is first converted to class \code{"POSIXlt"} by\code{\link{strptime}}: numeric input is first converted to\code{"POSIXct"}. Any conversion that needs to go between the twodate-time classes requires a time zone: conversion from\code{"POSIXlt"} to \code{"POSIXct"} will validate times in theselected time zone. One issue is what happens at transitionsto and from DST, for example in the UK\preformatted{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:00BST) and ambiguous (the clocks went back at 2:00 BST to 1:00 GMT). Whathappens in such cases is OS-specific: one should expect the first tobe \code{NA}, but the second could be interpreted as either BST orGMT (and common OSes give both possible values). Note too (see\code{\link{strftime}}) that OS facilities may not format invalidtimes correctly.}\value{\code{as.POSIXct} and \code{as.POSIXlt} return an object of theappropriate class. If \code{tz} was specified, \code{as.POSIXlt}will give an appropriate \code{"tzone"} attribute. Date-times knownto be invalid will be returned as \code{NA}.}\note{Some of the concepts used have to be extended backwards in time (theusage is said to be \sQuote{proleptic}). For example, the origin oftime for the \code{"POSIXct"} class, \sQuote{1970-01-01 00:00.00 UTC},is before UTC was defined. More importantly, conversion is doneassuming the Gregorian calendar which was introduced in 1582 and notused universally until the 20th century. One of there-interpretations assumed by ISO 8601:2004 is that there was a yearzero, even though current year numbering (and zero) is a much laterconcept (525 AD for year numbers from 1 AD).Conversions between \code{"POSIXlt"} and \code{"POSIXct"} of futuretimes are speculative except in UTC. The main uncertainty is in theuse of and transitions to/from DST (most systems will assume thecontinuation of current rules but these can be changed at shortnotice).If you want to extract specific aspects of a time (such as the day ofthe week) just convert it to class \code{"POSIXlt"} and extract therelevant component(s) of the list, or if you want a characterrepresentation (such as a named day of the week) use the\code{\link[base:strptime]{format}} method.If a time zone is needed and that specified is invalid on your system,what happens is system-specific but attempts to set it will probablybe ignored.Conversion from character needs to find a suitable format unless oneis supplied (by trying common formats in turn): this can be slow forlong inputs.}\seealso{\link{DateTimeClasses} for details of the classes;\code{\link{strptime}} for conversion to and from characterrepresentations.\code{\link{Sys.timezone}} for details of the (system-specific) namingof time zones.\link{locales} for locale-specific aspects.}\examples{\donttest{(z <- Sys.time()) # the current datetime, as class "POSIXct"unclass(z) # a large integerfloor(unclass(z)/86400) # the number of days since 1970-01-01 (UTC)(now <- as.POSIXlt(Sys.time())) # the current datetime, as class "POSIXlt"unlist(unclass(now)) # a list shown as a named vectornow$year + 1900 # see ?DateTimeClassesmonths(now); weekdays(now) # see ?months## suppose we have a time in seconds since 1960-01-01 00:00:00 GMT## (the origin used by SAS)z <- 1472562988# ways to convert thisas.POSIXct(z, origin = "1960-01-01") # localas.POSIXct(z, origin = "1960-01-01", tz = "GMT") # in UTC## SPSS dates (R-help 2006-02-16)z <- c(10485849600, 10477641600, 10561104000, 10562745600)as.Date(as.POSIXct(z, origin = "1582-10-14", tz = "GMT"))## Stata date-times: milliseconds since 1960-01-01 00:00:00 GMT## format %tc excludes leap-seconds, assumed here## For format %tC including leap seconds, see foreign::read.dta()z <- 1579598122120op <- options(digits.secs = 3)# avoid rounding down: milliseconds are not exactly representableas.POSIXct((z+0.1)/1000, origin = "1960-01-01")options(op)## Matlab 'serial day number' (days and fractional days)z <- 7.343736909722223e5 # 2010-08-23 16:35:00as.POSIXct((z - 719529)*86400, origin = "1970-01-01", tz = "UTC")as.POSIXlt(Sys.time(), "GMT") # the current time in UTC}\donttest{## These may not be correct names on your systemas.POSIXlt(Sys.time(), "America/New_York") # in New Yorkas.POSIXlt(Sys.time(), "EST5EDT") # alternative.as.POSIXlt(Sys.time(), "EST" ) # somewhere in Eastern Canadaas.POSIXlt(Sys.time(), "HST") # in Hawaiias.POSIXlt(Sys.time(), "Australia/Darwin")}#ifdef windowscols <- c("code", "coordinates", "TZ", "comments")tmp <- read.delim(file.path(R.home("share"), "zoneinfo", "zone.tab"),header = FALSE, comment.char = "#", col.names = cols)if(interactive()) View(tmp)#endif}\keyword{utilities}\keyword{chron}