Faranak Golestaneh
2014-Dec-28 14:17 UTC
[R] Changing UTC time to a time zone different from system time zone
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
Jeff Newmiller
2014-Dec-28 21:52 UTC
[R] Changing UTC time to a time zone different from system time zone
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
Prof Brian Ripley
2014-Dec-29 11:12 UTC
[R] Changing UTC time to a time zone different from system time zone
On 28/12/2014 21:52, Jeff Newmiller wrote:> 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.Correct, but for R >= 3.1.0 that help page says ' Almost all R platforms make use of a time-zone database originally compiled by Arthur David Olson and now managed by IANA, in which the preferred way to refer to a time zone is by a location ...' and "GMT" is always supported although "UTC" is preferred. All the platforms documented in 'R Installation and Administration' do make such use; however occasionally we hear of new ports, hence the cautious use of 'Almost'. However, we were also not told the R version ....> >> 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-- Brian D. Ripley, ripley at stats.ox.ac.uk Emeritus Professor of Applied Statistics, University of Oxford 1 South Parks Road, Oxford OX1 3TG, UK