I've found a million posts about converting from GMT to local time, but not one that first checks if the date-time is in GMT, and then converting it to its representative local time. As folks are suggesting a duplicate, I am looking to convert from GMT to local time, but more importantly, specifically want to check whether or not a conversion needs to be made. I'm using lubridate
, dplyr
, zoo
, and stringr
.
Take this example data (my data is in a larger data frame of nums and POSXIct formats):
for store 1000
, the df
dataframe has the column Time
:
Time<-c("2017-02-02 06:05:00 GMT", "2017-02-04 00:06:10 GMT", "2017-02-05 00:06:15 GMT")
df<-as.data.frame(Time)
time_zone_info
is as follows:
x<-1000
timezone<-"US/Eastern"
time_zone_info<-cbind(x,timezone)
time_zone_info<-as.data.frame(time_zone_info)
my current attempt is below:
correct_to_local_time<-function(df){
if("GMT" %in% df$Time){
df$Time<-as.POSIXct(df$Time, tz ="GMT",usetz=TRUE)
df$Time<-with_tz(df$Time,tz=time_zone_info$timezone)
}
else{
as.POSIXct(df$Time)}
}
correct_to_local_time(df)
which gives me the following which is computationally incorrect (it should be 1:05:00EDT, 01:10:00EDT, etc., but it did change the GMT to EDT, so I'm quite confused...):
Time
2017-02-02 06:05:00 EDT
2017-02-02 06:10:00 EDT
2017-02-02 06:10:00 EDT
Ideally I'd like something like:
Local Time
2017-02-02 01:05:00 EDT
2017-02-04 01:10:00 EDT
2017-02-05 01:15:00 EDT
Once a conversion from GMT to EDT or MST is made, are daylight savings accounted for?
Thanks in advance ya'll