anytime: Parse POSIXct objects from input data

Description Usage Arguments Details Value Notes Operating System Impact Old Heuristic Author(s) References See Also Examples

View source: R/anytime.R


These function use the Boost Date_Time library to parse datetimes (and dates) from strings, integers, factors or even numeric values (which are cast to strings internally). They return a vector of POSIXct objects (or Date objects in the case of anydate). POSIXct objects represent dates and time as (possibly fractional) seconds since the ‘epoch’ of January 1, 1970. A timezone can be set, if none is supplied ‘UTC’ is set.


anytime(x, tz = getTZ(), asUTC = FALSE, useR = FALSE,
  oldHeuristic = getOption("anytimeOldHeuristic", FALSE))

anydate(x, tz = getTZ(), asUTC = FALSE, useR = FALSE)

utctime(x, tz = getTZ(), useR = FALSE,
  oldHeuristic = getOption("anytimeOldHeuristic", FALSE))

utcdate(x, tz = getTZ(), useR = FALSE)



A vector of type character, integer or numeric with date(time) expressions to be parsed and converted.


A string with the timezone, defaults to the result of the (internal) getTZ function if unset. The getTZ function returns the timezone values stored in local package environment, and set at package load time. Also note that this argument applies to the output: the returned object will have this timezone set. The timezone is not used for the parsing which will always be to localtime, or to UTC is the asUTC variable is set (as it is in the related functions link{utctime} amd utcdate). So one can think of the argument as ‘shift parsed time object to this timezone’. This is similar to what format() in base R does, but our return value is still a POSIXt object instead of a character value.


A logical value indicating if parsing should be to UTC; default is false implying localtime.


A logical value indicating if conversion should be done via code from R (via package RApiDatetime) or via the Boost routines.


Behave versions up to and including 0.2.2 did and interpret a numeric or integer value that could be seen as a YYYYMMDD as a date. If the default value FALSE is seen, then date offset for used for dates, and second offsets for datetimes. A default value can also be set via the anytimeOldHeuristic option.


A number of fixed formats are tried in succession. These include the standard ISO format ‘YYYY-MM-DD HH:MM:SS’ as well as different local variants including several forms popular in the United States. Two-digits years and clearly ambigous formats such as ‘03/04/05’ are ignored. In the case of parsing failure a NA value is returned.

Fractional seconds are supported as well. As R itself only supports microseconds, the Boost compile-time option for nano-second resolution has not been enabled.


A vector of POSIXct elements, or, in the case of anydate, a vector of Date objects.


By default, the (internal) conversion to (fractional) seconds since the epoch is relative to the locatime of this system, and therefore not completely independent of the settings of the local system. This is to strike a balance between ease of use and functionality. A more-full featured conversion could be possibly be added with support for arbitrary reference times, but this is (at least) currently outside the scope of this package. See the RcppCCTZ package which offers some timezone-shifting and differencing functionality. As of version 0.0.5 one can also parse relative to UTC avoiding the localtime issue,

Times and timezones can be tricky. This package offers a heuristic approach, it is likely that some input formats may not be parsed, or worse, be parsed incorrectly. This is not quite a Bobby Tables situation but care must always be taken with user-supplied input.

The Boost Date_Time library cannot parse single digit months or days. So while ‘2016/09/02’ works (as expected), ‘2016/9/2’ will not. Other non-standard formats may also fail.

There is a known issue (discussed at length in issue ticket 5) where Australian times are off by an hour. This seems to affect only Windows, not Linux.

When given a vector, R will coerce it to the type of the first element. Should that be NA, surprising things can happen: c(NA, Sys.Date()) forces both values to numeric and the date will not be parsed correctly (as its integer value becomes numeric before our code sees it). On the other hand, c(Sys.Date(), NA) works as expected parsing as type Date with one missing value. See issue ticket 11) for more.

Operating System Impact

On Windows systems, accessing the isdst flag on dates or times before January 1, 1970, can lead to a crash. Therefore, the lookup of this value has been disabled for those dates and times, which could therefore be off by an hour (the common value that needs to be corrected). It should not affect dates, but may affect datetime objects.

Old Heuristic

Up until version 0.2.2, numeric input small than an internal cutoff value was interpreted as a date, even if anytime() was called. While convenient, it is also inconsistent as we otherwise take numeric values to be offsets to the epoch. Newer version are consistent: for anydate, a value is taken as date offset relative to the epoch (of January 1, 1970). For anytime, it is taken as seconds offset. So anytime(60) is one minute past the epoch, and anydate(60) is sixty days past it. The old behaviour can be enabled by setting the oldHeuristic argument to anytime (and utctime to TRUE. Additionally, the default value can be set via getOption("anytimeOldHeuristic") which can be set to TRUE in startup file.


Dirk Eddelbuettel


This StackOverflow answer provided the initial idea:

See Also



## See the source code for a full list of formats, and the
## or the reference in help('anytime-package') for details
times <- c("2004-03-21 12:45:33.123456",
          "2004/03/21 12:45:33.123456",
          "20040321 124533.123456",
          "03/21/2004 12:45:33.123456",
          "03-21-2004 12:45:33.123456",

## show effect of tz argument
anytime("2001-02-03 04:05:06")
## adjust parsed time to given TZ argument
anytime("2001-02-03 04:05:06", tz="America/Los_Angeles")
## somewhat equvalent base R functionality
format(anytime("2001-02-03 04:05:06"), tz="America/Los_Angeles")

Example output

Warning messages:
1: In file(con, "rb") :
  cannot open file '/etc/timezone': Permission denied
2: In fun(libname, pkgname) :
  No TZ information found. Falling back to UTC.
 [1] "2004-03-21 12:45:33 UTC" "2004-03-21 12:45:33 UTC"
 [3] "2004-03-21 12:45:33 UTC" "2004-03-21 12:45:33 UTC"
 [5] "2004-03-21 12:45:33 UTC" "2004-03-21 00:00:00 UTC"
 [7] "2004-03-21 00:00:00 UTC" "2004-03-21 00:00:00 UTC"
 [9] "2004-03-21 00:00:00 UTC" "2001-01-01 00:00:00 UTC"
 [1] "2004-03-21" "2004-03-21" "2004-03-21" "2004-03-21" "2004-03-21"
 [6] "2004-03-21" "2004-03-21" "2004-03-21" "2004-03-21" "2001-01-01"
 [1] "2004-03-21 12:45:33 UTC" "2004-03-21 12:45:33 UTC"
 [3] "2004-03-21 12:45:33 UTC" "2004-03-21 12:45:33 UTC"
 [5] "2004-03-21 12:45:33 UTC" "2004-03-21 00:00:00 UTC"
 [7] "2004-03-21 00:00:00 UTC" "2004-03-21 00:00:00 UTC"
 [9] "2004-03-21 00:00:00 UTC" "2001-01-01 00:00:00 UTC"
 [1] "2004-03-21" "2004-03-21" "2004-03-21" "2004-03-21" "2004-03-21"
 [6] "2004-03-21" "2004-03-21" "2004-03-21" "2004-03-21" "2001-01-01"
[1] "2001-02-03 04:05:06 UTC"
[1] "2001-02-03 04:05:06 America"
[1] "2001-02-03 04:05:06"

anytime documentation built on June 6, 2017, 1:02 a.m.