# NOT RUN {
# Create a POSIXct with tz="GMT"
ct("2019-01-01")
class(ct("2019-01-01"))
ct("2019-01-01 01:00:05")
# Convert to POSIXct
class(ct(as.POSIXlt("2019-01-01")))
# To seconds and back again
ct(as.numeric(1000, units="sec"))
# --------
# Convert character of time which has summer time leaps
# Example from CET (with CEST which is winter time)
#
# The point of shifting to and from summer time:
# DST Start (Clock Forward) DST End (Clock Backward)
# Sunday, March 31, 02:00 Sunday, October 27, 03:00
# --------
# From to winter time to summer time
txt <- c("2019-03-31 01:00",
"2019-03-31 01:30",
"2019-03-31 03:00",
"2019-03-31 03:30")
x <- ct(txt, tz="CET")
x
ct(x, tz="GMT")
# BE AWARE of this conversion of the 02:00: to 02:59:59 (exact time of shift) will lead to a
# wrong conversion
txt <- c("2019-03-31 01:30",
"2019-03-31 02:00",
"2019-03-31 03:30")
x <- ct(txt, tz="CET")
x
ct(x, tz="GMT")
# Which a diff on the time can detect, since all steps are not equal
plot(diff(ct(x, tz="GMT")))
# --------
# Shift to winter time is more problematic
# It works like this
txt <- c("2019-10-27 01:30",
"2019-10-27 02:00",
"2019-10-27 02:30",
"2019-10-27 03:00",
"2019-10-27 03:30")
x <- ct(txt, tz="CET")
x
ct(x, tz="GMT")
# however, timestamps can be given like this
txt <- c("2019-10-27 01:30",
"2019-10-27 02:00",
"2019-10-27 02:30",
"2019-10-27 02:00",
"2019-10-27 02:30",
"2019-10-27 03:00",
"2019-10-27 03:30")
x <- ct(txt, tz="CET")
x
ct(x, tz="GMT")
# Again can be detected, since all steps are not equal
plot(diff(ct(x, tz="GMT")))
# This can be fixed by (note that it can go wrong, e.g. with gaps around convertion etc.)
ct(x, tz="GMT", duplicatedadd=3600)
# }
Run the code above in your browser using DataLab