Formatting the output of Sys.time() with timezone in R using format.Posix*

Viewed 6500

I am trying to figure out how to format the output of Sys.time() in R.

For example:

t <- Sys.time()
print(t)
# [1] "2017-07-26 09:41:29 CEST"

which is correct.

I want to make a string out of t made of the date, the hour and minute and the timezone. I can use

format(t, format = "%F %R %Z")
# [1] "2017-07-26 09:41 CEST"

Which is what I expect.

However, I am having a hard time understanding the output if I set the timezone explicitly. For example:

format(t, format = "%F %R %Z", tz = "Europe/Stockholm")
# [1] "2017-07-26 09:41 CEST"

produces what I expect, but:

format(t, format = "%F %R %Z", tz = "CEST")
# [1] "2017-07-26 07:41 CEST"

which I think is wrong, I would have expected the output to be "2017-07-26 09:41 CEST" or "2017-07-26 09:41 Europe/Stockholm"

Also

format(t, format = "%F %R %Z", tz = "UTC+02:00")
# [1] "2017-07-26 05:41 UTC"

which I find even weirder, since I would have expected the output to be "2017-07-26 10:41 UTC+02:00"

In the answer I would like to know two things:

  • why my examples give the output they give?
  • is there any way to have the timezone always written like "2017-07-26 10:41 UTC+02:00" or "2017-07-26 10:41 Europe/Stockholm"?
1 Answers
Related