0

I have a vector of time (Uk time) with dates included and wish to change these times to local times. To do this I have already worked out what country the time needs to be set to and the time change in that country. So for example we know the time in the UK is 2023-04-24 07:49:00 and this needs to be changed to local time in Spain which is UTC +2 at this time of year. This can get a bit complex as the UK is also UTC +1 so I think the different will have to be worked out first, but this should give a new time of 2023-04-24 07:49:00 or 07:49:00. My main issues comes when doing this as I'm unsure how to do the change, the vectors im working with are below and both are in character format.

timestochange

"2023-04-24 07:49:00" 
"2023-04-24 07:53:00"
"2023-04-24 07:56:00" 
"2023-04-24 08:01:00" 
"2023-04-24 09:12:00"

tzchange #this is compared with UTC not current UK time
"+2:00"
Joe
  • 795
  • 1
  • 11
  • 1
    Does this answer your question? [How do you convert dates/times from one time zone to another in R?](https://stackoverflow.com/questions/1395117/how-do-you-convert-dates-times-from-one-time-zone-to-another-in-r). Or if you simply want to add/subtract hours without actually changing the timezone attribute, try `timestochange + lubridate::hours(x)`, where x is a numeric vector of number of hours. – zephryl May 19 '23 at 12:19

1 Answers1

2

lubridate got you cover:

# list of valid timezones strings for europe (it varies from operating system to operating system)
grep("Europe", OlsonNames(), value=TRUE)
# seting a datetime with timezone
time_london <- lubridate::ymd_hms("2023-04-24 07:49:00", tz = "Europe/London")
time_london

"2023-04-24 07:49:00 BST"

# how to transform btw one to another
time_madrid <- lubridate::with_tz(time = time_london, tzone = "Europe/Madrid")
time_madrid

"2023-04-24 08:49:00 CEST"

Now this yields one hour difference.

Arthur Welle
  • 586
  • 5
  • 15
  • 2
    As datetime types and timezones can get confusing at times, may be worth noting that actual numeric values of `time_london` & `time_madrid` are still identical and only difference is `$tzone` attribute. Meaning that `time_london - time_madrid == 0` – margusl May 19 '23 at 12:33