[R] Simple as.Date question dealing with a timezone offset

Gabor Grothendieck ggrothendieck at gmail.com
Fri Sep 18 02:48:26 CEST 2009


Using strapply() from the gsubfn package, apply the
pattern (2nd arg) to string (1st arg).   The matches
to the back references (i.e. the portions of the pattern
in parens) are passed to separate arguments of the
These arguments are date, offset and year respectively.
Then the function
calculates the appropriate date/time.  (If you wanted
to ignore the offset or interpret it in some other way,
particularly in relation to time zones, then change the
function appropriately. Also the way its set up here it returns
a chron object since as.chron.POSIXt specifically
supports an offset= arg but you could change that.
Dealing with time zones is tricky and it wasn't clear
what you wanted precisely so you may have to fix
that up.)

library(gsubfn)
library(chron)

strapply(X = "Tue Sep 15 09:22:09 -0600 2009",
   pattern = "(.*:..) ([-0-9]+) ([0-9]+)$",
   FUN = function(date, offset, year) {
       p <- as.POSIXct(paste(date, year),
               format = "%a %b %d %H:%M:%S %Y")
       as.chron(p, offset = as.numeric(offset))
   },
   simplify = c)

See R News 4/1 for info on dates and times and
http://gsubfn.googlecode.com for info on strapply.

On Thu, Sep 17, 2009 at 11:25 AM, esawdust <landon at 360vl.com> wrote:
>
> I've been trying to understand the as.Date functionality and I have a date
> and time stamp field that looks like this:
>
> "Tue Sep 15 09:22:09 -0600 2009"
>
> and I need to turn it into an R Date object for analysis.
>
> Simple date conversions I have down, no problem:
>
>> adate = c("7/30/1959")
>> as.Date(adate,"%m/%d/%Y")
> [1] "1959-07-30"
>>
>
> But when it comes to the type of date/time string format I have above, I
> can't figure out a format string that will work.
>
> The timezone offset is one part that causes problems.  Building up to a
> working format string for the full time stamp string, I can make it as far
> as:
>
>> adate = c("Tue Sep 15 09:22:09 -0600 2009")
>> as.Date(adate,format="%a %b %d %H:%M:%S")
> [1] "2009-09-15"
>
> (apparently year defaults to current year when it's not specified in the
> format string).  Because the Year comes after the timezone offset, I have to
> deal with the timezone offset in the format string.
>
> But when I get to the timezone offset value I can't use "%z" or "%Z" because
> those are "output only"
>
>> as.Date(adate,format="%a %b %d %H:%M:%S %z %Y")
> [1] NA
>
> I'm close, but can't incorporate the timezone offset field in the date/time
> stamp string.
>
> What am I missing?   I suppose one workaround is to split the date/time
> string into its component parts, reassemble it into a string as.Date can
> deal with, but that seems to defeat one of the purposes of as.Date's format
> capability.
>
> Any advice for how to translate a "Tue Sep 15 09:22:09 -0600 2009" into an R
> Date object?
>
> Landon
>
> --
> View this message in context: http://www.nabble.com/Simple-as.Date-question-dealing-with-a-timezone-offset-tp25491955p25491955.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list