[R] Problem to convert date to number

David Winsemius dwinsemius at comcast.net
Wed Apr 6 14:53:46 CEST 2011


On Apr 6, 2011, at 7:55 AM, Chris82 wrote:

> Hi R users,
>
> I have a maybe small problem which I cannot solve by myself.
>
> I want to convert
>
> "chron" "dates" "times"
>
> (04/30/06 11:35:00)

Using the example from help(chron)

 > as.POSIXlt(x)
# chron times are assumed to be UTC but are printed with the current  
local value
[1] "1992-02-27 18:03:20 EST" "1992-02-27 17:29:56 EST"
[3] "1992-01-13 20:03:30 EST" "1992-02-28 13:21:03 EST"
[5] "1992-02-01 11:56:26 EST"
 > as.POSIXlt(x, tz="UTC")
[1] "1992-02-27 23:03:20 UTC" "1992-02-27 22:29:56 UTC"
[3] "1992-01-14 01:03:30 UTC" "1992-02-28 18:21:03 UTC"
[5] "1992-02-01 16:56:26 UTC"
 > as.POSIXlt(x, tz="CEST")  # "not working"
[1] "1992-02-27 23:03:20 UTC" "1992-02-27 22:29:56 UTC"
[3] "1992-01-14 01:03:30 UTC" "1992-02-28 18:21:03 UTC"
[5] "1992-02-01 16:56:26 UTC"

So it makes me wonder if as.POSIXct considers CEST to be a valid tz  
value.

 > as.POSIXlt(x, tz="XYZST")
[1] "1992-02-27 23:03:20 UTC" "1992-02-27 22:29:56 UTC"
[3] "1992-01-14 01:03:30 UTC" "1992-02-28 18:21:03 UTC"
[5] "1992-02-01 16:56:26 UTC"

 > as.POSIXlt(x, tz="EST5EDT")  # where  I am, and seems to be working
[1] "1992-02-27 18:03:20 EST" "1992-02-27 17:29:56 EST"
[3] "1992-01-13 20:03:30 EST" "1992-02-28 13:21:03 EST"
[5] "1992-02-01 11:56:26 EST"

But despite the returned code from Sys.time() that TLA  (EDT) does not  
"work":
 > Sys.time()
[1] "2011-04-06 08:44:01 EDT"
 > as.POSIXlt(x, tz="EDT")
# "EDT" printed as UTC values
[1] "1992-02-27 23:03:20 UTC" "1992-02-27 22:29:56 UTC"
[3] "1992-01-14 01:03:30 UTC" "1992-02-28 18:21:03 UTC"
[5] "1992-02-01 16:56:26 UTC"

But an unambiguous version does return the expected offset. All of  
this can be specific to your system (not provided) and your locale  
setting (also not provided)

 > as.POSIXlt(x, tz="UTC+02")
[1] "1992-02-27 21:03:20 UTC" "1992-02-27 20:29:56 UTC"
[3] "1992-01-13 23:03:30 UTC" "1992-02-28 16:21:03 UTC"
[5] "1992-02-01 14:56:26 UTC"

-- 
David.
>
>
> to a number with as.POSIXct.
>
> The Problem is that I can't choose different timezones. I always get  
> "CEST"
> and not "UTC" what I need.
>
> date = as.POSIXct(y,tz="UTC")
>
> "2006-04-30 11:35:00 CEST"
> Then I tried to use as.POSIXlt.
>
> date = as.POSIXlt(y,tz="UTC")
>
> "2006-04-30 11:35:00 UTC"
>
> The advantage is I get time in UTC but now the problem is that I can
> calculate numbers.
>
> date <- as.double(date)/86400
>
> it is not working with  as.POSIXlt but with as.POSIXct
>
>
> Thanks!
>
> With best regards
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Problem-to-convert-date-to-number-tp3430571p3430571.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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list