10

PHP has this wonderful function, strtotime, that takes any string containing just about any date format and returns a time (secs-since-1970). It is more future-proof than strptime, for instance, because if the date format changes my script does not break. Does R have anything similar?

(I don't need the time-relative feature of strtotime as of today, but I'm sure at some point I'll need strtotime("next Thursday"), or strtotime("first day of last month"), so if you know R extensions that do that too, then I'd love to hear about it!)

UPDATE: If anyone (possibly me at some point in the future) want to try implementing this in R, or any other language, I tracked down the source code for it. The relevant files are timelib.h, timelib.re and timelib_structs.h. It appears to all be standard C and standalone, no PHP headers to bring in. However the compile process compiles the *.re file into real C, so you will need to install and compile PHP at least once.

The code that calls it is also quite straightforward (see lines 1428 to 1433, at the time of writing; the longer code above it in the same function is just to get the current time, for use in relative times).

Darren Cook
  • 27,837
  • 13
  • 117
  • 217
  • 4
    I used to use the Perl equivalents of this too, and no, I am not aware of anyone having written such as parser. Maybe you want to port one? – Dirk Eddelbuettel Sep 02 '11 at 03:40
  • 2
    That is an interesting idea. If it doesn't exist, why not just call PHP from R, or vice versa? There are some very easy ways to pass short text strings like this. – Iterator Sep 02 '11 at 04:30
  • 1
    `quantmod` allows a limited version of this in `chartSeries`: `chartSeries(FTSE,subset="last 3 months")` – James Sep 02 '11 at 09:43

1 Answers1

2

I love that function in PHP, but unfortunately there seems to not be an equivalent in R (and none is mentioned in the language reference, most notably http://rss.acs.unt.edu/Rdoc/library/base/html/DateTimeClasses.html).

MacMac
  • 34,294
  • 55
  • 151
  • 222
Dustin Oprea
  • 9,673
  • 13
  • 65
  • 105
  • (Sorry for the delay with the tick! it wasn't what I wanted to hear, but it is, of course, the correct answer.) – Darren Cook May 25 '12 at 00:10