Clean method to convert date and time between time zones keeping it in POSIXct format

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Clean method to convert date and time between time zones keeping it in POSIXct format

arnaud Mosnier-2
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Clean method to convert date and time between time zones keeping it in POSIXct format

Ivan Calandra-4
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
[hidden email]
--
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]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Clean method to convert date and time between time zones keeping it in POSIXct format

Gabor Grothendieck
In reply to this post by arnaud Mosnier-2
This involves mucking with the internals as well but it is short:

   structure(T1, tzone = "UTC")

On Mon, May 9, 2016 at 9:24 AM, Arnaud Mosnier <[hidden email]> wrote:

> 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]]
>
> ______________________________________________
> [hidden email] 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.



--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Clean method to convert date and time between time zones keeping it in POSIXct format

R help mailing list-2
In reply to this post by Ivan Calandra-4
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 <[hidden email]>
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
> [hidden email]
> --
> 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]]
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Clean method to convert date and time between time zones keeping it in POSIXct format

MacQueen, Don
In reply to this post by arnaud Mosnier-2
I think setting the attribute is the best way to "convert", and the
following will hopefully explain why. (And I would tend to agree with
William Dunlap that a function to set the attribute might help userRs.)



R always stores POSIXct objects internally in seconds since an origin in
UTC. I would not think in terms of converting; I would think in terms of
displaying.


t1 <- as.POSIXct("2016-05-09 10:00:00", tz="America/New_York")
t2 <- t1
attributes(t2)$tzone <- 'UTC'

> print(t1)
[1] "2016-05-09 10:00:00 EDT"
> print(t2)
[1] "2016-05-09 14:00:00 UTC"

> as.numeric(t1)
[1] 1462802400
> as.numeric(t2)
[1] 1462802400



The actual value of t2 is the same as t1, it has not been "converted"
(because to me conversion implies change, and there has been no change in
the value of t2). R has been merely been told to display it in UTC when
printed.


Similarly, when a character string is converted to POSIXct, R has to be
told what timezone to use to convert it to seconds since the origin in
UTC. If a timezone is not specified, the user's local (default) timezone
is used.

> t3 <- as.POSIXct("2016-05-09 10:00:00")
> t3
[1] "2016-05-09 10:00:00 PDT"
> as.numeric(t3)
[1] 1462813200


The number of seconds for t3 is different than for t1 and t2, and this is
because I am not in the America/New_York timezone

And, in fact,

> (as.numeric(t3) - as.numeric(t1))/3600
[1] 3


Indicating that PDT and America/New_York are three hours apart, as indeed
they are.

To me, this version
  t4 <- as.POSIXct(format(t1, tz="UTC"), tz="UTC")
doesn't recognize the distinction between internal storage and display,
and the fact that there is no real conversion.

> identical(t2, t4)
[1] TRUE




To go a little further,

> attributes(t3)
$class
[1] "POSIXct" "POSIXt"

$tzone
[1] ""


If a timezone is not specified when the object is created, then the tzone
attribute is set to "" and R displays using the local timezone.

> t3
[1] "2016-05-09 10:00:00 PDT"

I can change the local timezone:

> Sys.setenv(TZ='UTC')
> t3
[1] "2016-05-09 17:00:00 UTC"

t3 has not changed, but how it is displayed has changed.

Given all this, it's helpful that the format() function for POSIXt objects
has a tz argument.

> format(t3, tz='US/Pacific')
[1] "2016-05-09 10:00:00"


Indicating again that t3 has not changed; I've just manipulated to rules
for how it is displayed.


-Don

--
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 5/9/16, 6:24 AM, "R-help on behalf of Arnaud Mosnier"
<[hidden email] on behalf of [hidden email]> wrote:

>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]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://secure-web.cisco.com/1nvZKqHLPcK9TOXimc8J5movKns8BiTyuMWcZyyxtloFp
>axMIqoeoukSrVfSV5mZWIt9iP1hZc8xp4qpTd4myFmgBNUpWkT0GA8U1sEDLLDrqq4f6rBRi8l
>XA8AZCvehwZW9JmJOjsfBUHIciqVRyN2jDW3WmSTnM6vZP_pc2W1B_DxINMQlrH6d8IveSoEVz
>x5Ie8aC104HM-D5z1qkGhkmTZBDtAkwyRfij_jTlVGsYXCgM3f8umyU0J6rr0zlsQyTFAdzqmi
>rfZsH0CvNtISZVnx_h3_ErJhp7onwLO-9l4UvGOe9y7j0thranjcVCJ45UFTgaP8Jp0lEZ1XjJ
>QnrRiRwQ9m8IHckFxPXutNY/https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2F
>r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] 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.