I thought I had solved this problem. But I am still having trouble
converting times. I am looking for a way to print out the string versions
of times in different time zones. Same time, different zones.
I have times stored as seconds since epoch and as text strings in local
time.
For instance (from an input file):
Sat Nov 3 20:25:18 2012 1351927518
The local time zone is NZDT
Browse[3]> as.POSIXct(1351927518, origin="1970-01-01", tz="NZDT")
[1] "2012-11-03 07:25:18 GMT"
Damn, that is GMT. But remembering the helpful replies to similar
queries....
Browse[3]> Sys.getenv("TZ")
[1] "GMT"
So I change the environment variable TZ and ...
Browse[3]> Sys.setenv(TZ="NZDT")
Browse[3]> as.POSIXct(1351927518, origin="1970-01-01", tz="NZDT")
[1] "2012-11-03 07:25:18 NZDT"
The labled time zone is set OK but the time is wrong.
The date string was generated in Perl as...
DB<7> p scalar(localtime(1351927518))
Sat Nov 3 20:25:18 2012
Using gmtime in Perl...
DB<8> p scalar(gmtime(1351927518))
Sat Nov 3 07:25:18 2012
cheers
Worik
On Fri, Mar 30, 2012 at 3:10 PM, Worik R <[email protected]> wrote:
>
>
> On Fri, Mar 30, 2012 at 2:53 PM, Joshua Ulrich <[email protected]>wrote:
>
>> On Thu, Mar 29, 2012 at 3:56 PM, Worik R <[email protected]> wrote:
>>
>>
>
>> I removed the (not so minimal) reproducible example because you can
>> get the same behavior via:
>> > (s <- Sys.time())
>> [1] "2012-03-29 20:43:35 CDT"
>> > as.POSIXct(as.numeric(s),origin="1970-01-01")
>> [1] "2012-03-30 02:43:35 CDT"
>>
>> sapply() attempts to simplify to an array. Arrays can only contain an
>> atomic type. POSIXct is not an atomic type, so it gets converted to
>> numeric.
>>
>> The way to get around this is to explicitly set the timezone in your R
>> session (see ?timezone). I can do this on my Ubuntu machine via:
>> > Sys.setenv(TZ="GMT")
>>
>> Now if I run the code above again, there is no difference after
>> converting from POSIXct -> numeric -> POSIXct:
>> > (s <- Sys.time())
>> [1] "2012-03-30 01:45:36 GMT"
>> > as.POSIXct(as.numeric(s),origin="1970-01-01")
>> [1] "2012-03-30 01:45:36 GMT"
>>
>> HTH,
>>
>
> Bingo! Thaks heaps. I have been working on this and had got as far as
> realising it was the conversion to numeric. I was trying to set the time
> zone in the as.POSIXct call but to no avail. But this looks good.
>
> cheers
> W
>
>
>> --
>> Joshua Ulrich | FOSS Trading: www.fosstrading.com
>>
>> R/Finance 2012: Applied Finance with R
>> www.RinFinance.com
>>
>
>
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.