[R] Clean method to convert date and time between time zones keeping it in POSIXct format
William Dunlap
wdunlap at tibco.com
Mon May 9 16:36:07 CEST 2016
I think as.POSIXct will just pass through a POSIXct object without any
changes. E.g.,
> dput(as.POSIXct( structure( list(quote(foo)),
class=c("POSIXct","POSIXt"))))
structure(list(foo), class = c("POSIXct", "POSIXt"))
If as.POSIXct( POSIXctObject, tz="ZONE") changed the time zone then a fair
bit of code would have to be changed from
t <- as.POSIXct(t)
to
if (!is.POSIXct(t)) {
t <- as.POSIXct(t)
}
so that existing POSIXct objects would not have their time zones changed.
Having a tzone<- or tz<- function could be handy.
Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Mon, May 9, 2016 at 6:37 AM, Ivan Calandra <ivan.calandra at univ-reims.fr>
wrote:
> I don't have an answer, but actually, I would have expected
> as.POSIXct(T1, tz="UTC")
> to work...
>
> Looks like as.POSIXct cannot convert from class "POSIXct"
>
> Ivan
>
> --
> Ivan Calandra, PhD
> Scientific Mediator
> University of Reims Champagne-Ardenne
> GEGENAA - EA 3795
> CREA - 2 esplanade Roland Garros
> 51100 Reims, France
> +33(0)3 26 77 36 89
> ivan.calandra at univ-reims.fr
> --
> https://www.researchgate.net/profile/Ivan_Calandra
> https://publons.com/author/705639/
>
>
> Le 09/05/2016 à 15:24, Arnaud Mosnier a écrit :
>
>> Dear UseRs,
>>
>> I know two ways to convert dates and time from on time zone to another but
>> I am pretty sure that there is a better (cleaner) way to do that.
>>
>>
>> Here are the methods I know:
>>
>>
>> ## The longest way ...
>>
>> T1 <- as.POSIXct("2016-05-09 10:00:00", format="%Y-%m-%d %H:%M:%S",
>> tz="America/New_York")
>>
>> print(T1)
>>
>> T2 <- as.POSIXct(format(T1, tz="UTC"), tz="UTC") # format convert it to
>> character, so I have to convert it back to POSIXct afterward.
>>
>> print(T2)
>>
>>
>>
>> ## The shortest but probably not the cleanest ...
>>
>> attributes(T1)$tzone <- "UTC"
>>
>> print(T1)
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list