[R] Fwd: strptime() problem?
Gabor Grothendieck
ggrothendieck at myway.com
Tue Aug 17 17:59:48 CEST 2004
javier garcia - CEBAS <rn001 <at> cebas.csic.es> writes:
:
: Hi all;
: I've already send a similar e-mail to the list and Prof. Brian Ripley
: answered me but my doubts remain unresolved. Thanks for the clarification,
: but perhaps I wasn't clear enough in posting my questions.
:
: I've got a postgres database which I read into R. The first column is
: Timestamp with timezone, and my data are already in UTC format. An 'printed'
: extract of R character column, resulting from the timestamptz field is:
:
: raincida$ts:
:
: [2039] "25/03/2000 22:00:00 UTC" "25/03/2000 23:00:00 UTC"
: [2041] "26/03/2000 00:00:00 UTC" "26/03/2000 01:00:00 UTC"
: [2043] "26/03/2000 02:00:00 UTC" "26/03/2000 03:00:00 UTC"
: [2045] "26/03/2000 04:00:00 UTC" "26/03/2000 05:00:00 UTC"
:
: #And I need to convert this character column into POSIXct, for eventual
work.
: #As I can see in the documentation, the process is to use strptime(), what
: #creates an object POSIXlt and doesn't allow to specify that the time zone
of
: #the data is already UTC; followed by as.POSIXct()
:
: > lluvia.strptime <- strptime(raincida$ts, format="%d/%m/%Y %H:%M:%S")
: > lluvia.strptime.POSIXct <- as.POSIXct(lluvia.strptime,tz="GMT")
:
: A "printed" extract is:
:
: [2039] "2000-03-25 22:00:00 GMT" "2000-03-25 23:00:00 GMT"
: [2041] "2000-03-26 00:00:00 GMT" "2000-03-26 01:00:00 GMT"
: [2043] "2000-03-26 03:00:00 GMT" "2000-03-26 03:00:00 GMT"
: [2045] "2000-03-26 04:00:00 GMT" "2000-03-26 05:00:00 GMT"
:
: As we can see, elements [2043] differ. Shouldn't they be similar as the rest
: of the other shown elements? I thought this was a bug, but it seems that
I've
: got and conceptual error.(?). This happens several times in my data, and
: produces eventual errors.
:
: Please, how could I resolved this?
[Sorry if this gets posted twice. I had a problem posting and
not sure if the first one ever got sent.]
I am in a different time zone, EDT, on Windows XP and can't
replicate this but you might try reading the latest R News
article on dates and times for some ideas, viz. page 32 of:
http://cran.r-project.org/doc/Rnews/Rnews_2004-1.pd
In particular, try converting the datetimes to chron and then doing
your manipulations in chron or else converting them from chron to
POSIXct rather than going through POSIXlt:
require(chron)
r.asc <- raincida$ts
r.chron <- chron(substring(r.asc, 1, 10),
substring(r.asc, 12, 19), format = c("d/m/y", "h:m:s"))
r.ct <- as.POSIXct(r.chron)
format(r.ct, tz="GMT") # display in GMT
More information about the R-help
mailing list