Reputation: 767
I have offset time between UTC and local time at different locations as following (+/-HH:MM):
> x
# [1] "+00:00" "+00:00" "-03:00" "+01:00" "+03:00" "+03:00" "+00:00" "+01:00"
# [9] "+00:00" "+00:00" "+02:00" "+01:00" "+02:00" "-02:00" "+00:00" "+01:00"
How can I convert this to a time interval, then use it to shift Sys.Date()
using the numbers above?
Upvotes: 1
Views: 82
Reputation: 18749
Another solution using as.difftime
(there is a bit of gymnastics to do because it doesn't recognized signed character input):
x <- c("+00:00","+00:00","-03:00","+01:00","+03:00","+03:00","+00:00","+01:00","+00:00","+00:00","+02:00","+01:00","+02:00","-02:00","+00:00","+01:00")
as.difftime(gsub("[+-]","",x), format ="%H:%M") * ifelse(grepl("^-",x),-1,1)
#Time differences in secs
# [1] 0 0 -10800 3600 10800 10800 0 3600 0 0 7200 3600 7200 -7200 0 3600
#attr(,"tzone")
#[1] ""
So to shift from Sys.time()
, it's simply:
Sys.time()+as.difftime(gsub("[+-]","",x),format ="%H:%M") * ifelse(grepl("^-",x),-1,1)
#[1] "2015-07-10 10:36:32 CEST" "2015-07-10 10:36:32 CEST" "2015-07-10 07:36:32 CEST" "2015-07-10 11:36:32 CEST" "2015-07-10 13:36:32 CEST" "2015-07-10 13:36:32 CEST" "2015-07-10 10:36:32 CEST"
#[8] "2015-07-10 11:36:32 CEST" "2015-07-10 10:36:32 CEST" "2015-07-10 10:36:32 CEST" "2015-07-10 12:36:32 CEST" "2015-07-10 11:36:32 CEST" "2015-07-10 12:36:32 CEST" "2015-07-10 08:36:32 CEST"
#[15] "2015-07-10 10:36:32 CEST" "2015-07-10 11:36:32 CEST"
If you had your input as numbers, it would be even more straightforward, since it does recognized signed numeric input:
y <- c(-1,2,3,0,-4,-6.5)
Sys.time() + as.difftime(y, units="hours")
# [1] "2015-07-10 09:39:31 CEST" "2015-07-10 12:39:31 CEST" "2015-07-10 13:39:31 CEST" "2015-07-10 10:39:31 CEST" "2015-07-10 06:39:31 CEST" "2015-07-10 04:09:31 CEST"
Upvotes: 1
Reputation: 7190
Here I have a (clumsy) solution, but it works I think. Waiting for more elegant solutions. Steps are, gsub
to strings all punctuations and the +
sign. convert them all in numeric, divide by 100 in order to have, example -200 as -2, and them pass them all as arguments of the hours
function.
library(lubridate)
x1 <- gsub("[:punct:]|\\+", "", x)
x1 <- as.numeric(as.character(x1))
x1 <- x1 / 100
Sys.Date() + hours(x1)
[1] "2015-07-09 00:00:00 UTC" "2015-07-09 00:00:00 UTC"
[3] "2015-07-08 21:00:00 UTC" "2015-07-09 01:00:00 UTC"
[5] "2015-07-09 03:00:00 UTC" "2015-07-09 03:00:00 UTC"
[7] "2015-07-09 00:00:00 UTC" "2015-07-09 01:00:00 UTC"
[9] "2015-07-09 00:00:00 UTC" "2015-07-09 00:00:00 UTC"
[11] "2015-07-09 02:00:00 UTC" "2015-07-09 01:00:00 UTC"
[13] "2015-07-09 02:00:00 UTC" "2015-07-08 22:00:00 UTC"
[15] "2015-07-09 00:00:00 UTC" "2015-07-09 01:00:00 UTC"
data I have used:
x <- c("+00:00", "+00:00", "-03:00", "+01:00", "+03:00", "+03:00", "+00:00", "+01:00",
"+00:00", "+00:00", "+02:00", "+01:00", "+02:00", "-02:00", "+00:00", "+01:00")
Here the data used is the vector y
that contains minutes too.
y <- c("+03:30", "+02:45", "-03:50", "-05:00")
y1 <- unlist(strsplit(y, "[:punct:]"))
y1 <- gsub("\\+", "", y1)
y1 <- as.numeric(as.character(y1))
MinuteS <- y1[1:length(y1) %% 2 == 0]
HourS <- y1[1:length(y1) %% 2 == 1]
Sys.Date() + minutes(MinuteS) + hours(HourS)
[1] "2015-07-09 03:30:00 UTC"
[2] "2015-07-09 02:45:00 UTC"
[3] "2015-07-08 21:50:00 UTC"
[4] "2015-07-08 19:00:00 UTC"
Upvotes: 1