I have the following string desribing a date and time in Central European Summer Time: "2021-09-23 12:00:00". In fact, I've got a whole, long column of such time points. For certain reasons I have to handle these times with functions in the 'lubridate' package. Using 'as_datetime' I get
t0 = "2021-09-23 12:00:00"
t1 = as_datetime(t0); t1
## [1] "2021-09-23 12:00:00 UTC"
that is, as_datetime
uses the hours and minutes given and adds Universal
Time "UTC" as time zone. Supplying the time zone CEST gives instead
t1 = as_datetime(t0, tz = "CEST"); t1
## [1] "2021-09-23 10:00:00 CEST"
that is, changes the time, what I don't want.
What I would like to get (what I really need) is "2021-09-23 12:00:00 CEST", that is changing the time zone without changing the time.
I tried force_tz
and with_tz
, but that didn't work either.
What I am also wondering is why 'lubridate' converts 12:00:00 UST to 10:00:00 CEST, because CEST is the same as GMT 2 and UTC is GMT 0, so the result should actually be the other way around.
Thanks for any help.
CodePudding user response:
Try:
t0 = "2021-09-23 12:00:00"
library(lubridate)
#1
t1 = as_datetime(t0, tz = "Europe/Berlin")
[1] "2021-09-23 12:00:00 CEST"
or
#2
t1 = as_datetime(t0, tz = "CST6CDT")
[1] "2021-09-23 12:00:00 CDT"