[R] Changing UTC time to a time zone different from system time zone

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Sun Dec 28 22:52:56 CET 2014


Very few attachment file types are allowed through the mailing list, and 
yours did not make it.

I noticed that your format strings did not appear consistent... see the 
format string I use below, and make sure the characters between the 
numeric parts of your format are correct for your case. Did you read 
the help file ?strptime ?

Note that there are two steps... importing the character data to a POSIXct 
type, and then choosing how you want to display those instants of time in 
whatever time zones you wish. That is, regardless of time zone, the 
POSIXct value represents the same instant of time regardless of time zone 
you want to display it in, while POSIXlt will in general have different 
internal representations that match the external representations.

Note that you really do need to read ?timezone, because the below code 
only works if your system understands the "GMT" and "Australia/Darwin" 
timezones, and that is operating-system-specific behavior.  This was 
successfully run on Ubuntu... you did not mention your operating system.

> tst <- "2014-12-25 00:00" # GMT
> tstct <- as.POSIXct( tst, format="%Y-%m-%d %H:%M", tz="GMT" )
> tstct
[1] "2014-12-25 GMT"
> unclass(tstct)
[1] 1419465600
attr(,"tzone")
[1] "GMT"
> tstlt <- as.POSIXlt( tstct, tz="Australia/Darwin" )
> tstlt
[1] "2014-12-25 09:30:00 ACST"
> unclass(tstlt)
$sec
[1] 0

$min
[1] 30

$hour
[1] 9

$mday
[1] 25

$mon
[1] 11

$year
[1] 114

$wday
[1] 4

$yday
[1] 358

$isdst
[1] 0

$zone
[1] "ACST"

$gmtoff
[1] 34200

attr(,"tzone")
[1] "Australia/Darwin" "ACST"             "ACDT"
> tstct2 <- as.POSIXct( tstlt )
> tstct2
[1] "2014-12-25 09:30:00 ACST"
> unclass(tstct2)
[1] 1419465600
attr(,"tzone")
[1] "Australia/Darwin"


On Sun, 28 Dec 2014, Faranak Golestaneh wrote:

> Dear Friends, I?ve just started using R. I am working on a database
> containing date-time as well as numeric values. Firstly I have changed the
> class of the data/time column from factor to POSIXlt. Time and date are
> based on UTC time but I need to change them to a local time ( not my local
> time, my zone is Singapore but the data are from Australia) so I need to
> change the time and date to Australia time say e.g AEST. I tried one way or
> another but all failed. The following is example of the command I used.
> Also one of my .CSV files is attached.
>
> Data_Power <- read.table("train15.csv",header = TRUE, sep = ",", row.names
> = NULL);
>
> Date_clm=Data_Power$TIMESTAMP;
>
> Date_original <- strptime(Date_clm, "%Y %m %d %H:%M", tz="GMT")
>
> Date_local =format(Date_original, format="%c", tz="America/New_York")
>
> Date_local <- strptime(Date_original, "%Y %m %d %H:%M +0800")
>
> Date_local=as.POSIXlt(Date_original, "Australia/Darwin")
>
> Date_local=as.POSIXlt(Date_original, " AEST ")
>
> I would be so thankful if you help me out. Thanks
>
>
>
> Cheers,
>
> Faranak
> ______________________________________________
> 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.

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k



More information about the R-help mailing list