Dates and Time Series
UTC Time Shift
Fixed Instants
POSIXct stores date-time instants. Using an explicit UTC time zone keeps examples deterministic across machines.
Program
Play the script to shift a fixed UTC instant by selected hours.
utc_time_shift.R
Replay: real traced execution (multi-file project)
base_time <- as.POSIXct("2026-01-01 08:00:00", tz = "UTC")
hour_shift <- 0
event_time <- base_time + hour_shift * 3600
stamp <- format(event_time, "%Y-%m-%d %H:%M", tz = "UTC")
label <- paste("UTC", stamp)
cat(label, "\n", sep = "")
base_time <- as.POSIXct("2026-01-01 08:00:00", tz = "UTC")
hour_shift <- 6
event_time <- base_time + hour_shift * 3600
stamp <- format(event_time, "%Y-%m-%d %H:%M", tz = "UTC")
label <- paste("UTC", stamp)
cat(label, "\n", sep = "")
base_time <- as.POSIXct("2026-01-01 08:00:00", tz = "UTC")
hour_shift <- 12
event_time <- base_time + hour_shift * 3600
stamp <- format(event_time, "%Y-%m-%d %H:%M", tz = "UTC")
label <- paste("UTC", stamp)
cat(label, "\n", sep = "")
base_time ← 2026-01-01 08:00 UTC
1base_time <- as.POSIXct("2026-01-01 08:00:00", tz = "UTC")2hour_shift <- 0values this step2026-01-01 08:00 UTCbase_timehour_shift ← 0
1base_time <- as.POSIXct("2026-01-01 08:00:00", tz = "UTC")2hour_shift <- 03event_time <- base_time + hour_shift * 3600values this step0hour_shiftevent_time ← 2026-01-01 08:00 UTC
2hour_shift <- 03event_time <- base_time + hour_shift * 36004stamp <- format(event_time, "%Y-%m-%d %H:%M", tz = "UTC")values this step2026-01-01 08:00 UTCevent_time2026-01-01 08:00 UTCbase_time0hour_shiftstamp ← 2026-01-01 08:00
3event_time <- base_time + hour_shift * 36004stamp <- format(event_time, "%Y-%m-%d %H:%M", tz = "UTC")5label <- paste("UTC", stamp)values this step2026-01-01 08:00stamp2026-01-01 08:00 UTCevent_timelabel ← UTC 2026-01-01 08:00
4stamp <- format(event_time, "%Y-%m-%d %H:%M", tz = "UTC")5label <- paste("UTC", stamp)6cat(label, "\n", sep = "")values this stepUTC 2026-01-01 08:00label2026-01-01 08:00stampcat(label, " ", sep = "")
5label <- paste("UTC", stamp)6cat(label, "\n", sep = "")outputUTC 2026-01-01 08:00values this stepUTC 2026-01-01 08:00label
base_time ← 2026-01-01 08:00 UTC
1base_time <- as.POSIXct("2026-01-01 08:00:00", tz = "UTC")2hour_shift <- 6values this step2026-01-01 08:00 UTCbase_timehour_shift ← 6
1base_time <- as.POSIXct("2026-01-01 08:00:00", tz = "UTC")2hour_shift <- 63event_time <- base_time + hour_shift * 3600values this step6hour_shiftevent_time ← 2026-01-01 14:00 UTC
2hour_shift <- 63event_time <- base_time + hour_shift * 36004stamp <- format(event_time, "%Y-%m-%d %H:%M", tz = "UTC")values this step2026-01-01 14:00 UTCevent_time2026-01-01 08:00 UTCbase_time6hour_shiftstamp ← 2026-01-01 14:00
3event_time <- base_time + hour_shift * 36004stamp <- format(event_time, "%Y-%m-%d %H:%M", tz = "UTC")5label <- paste("UTC", stamp)values this step2026-01-01 14:00stamp2026-01-01 14:00 UTCevent_timelabel ← UTC 2026-01-01 14:00
4stamp <- format(event_time, "%Y-%m-%d %H:%M", tz = "UTC")5label <- paste("UTC", stamp)6cat(label, "\n", sep = "")values this stepUTC 2026-01-01 14:00label2026-01-01 14:00stampcat(label, " ", sep = "")
5label <- paste("UTC", stamp)6cat(label, "\n", sep = "")outputUTC 2026-01-01 14:00values this stepUTC 2026-01-01 14:00label
base_time ← 2026-01-01 08:00 UTC
1base_time <- as.POSIXct("2026-01-01 08:00:00", tz = "UTC")2hour_shift <- 12values this step2026-01-01 08:00 UTCbase_timehour_shift ← 12
1base_time <- as.POSIXct("2026-01-01 08:00:00", tz = "UTC")2hour_shift <- 123event_time <- base_time + hour_shift * 3600values this step12hour_shiftevent_time ← 2026-01-01 20:00 UTC
2hour_shift <- 123event_time <- base_time + hour_shift * 36004stamp <- format(event_time, "%Y-%m-%d %H:%M", tz = "UTC")values this step2026-01-01 20:00 UTCevent_time2026-01-01 08:00 UTCbase_time12hour_shiftstamp ← 2026-01-01 20:00
3event_time <- base_time + hour_shift * 36004stamp <- format(event_time, "%Y-%m-%d %H:%M", tz = "UTC")5label <- paste("UTC", stamp)values this step2026-01-01 20:00stamp2026-01-01 20:00 UTCevent_timelabel ← UTC 2026-01-01 20:00
4stamp <- format(event_time, "%Y-%m-%d %H:%M", tz = "UTC")5label <- paste("UTC", stamp)6cat(label, "\n", sep = "")values this stepUTC 2026-01-01 20:00label2026-01-01 20:00stampcat(label, " ", sep = "")
5label <- paste("UTC", stamp)6cat(label, "\n", sep = "")outputUTC 2026-01-01 20:00values this stepUTC 2026-01-01 20:00label
POSIXct
`as.POSIXct(..., tz = "UTC")` creates a date-time instant with an explicit zone.
seconds
`hour_shift * 3600` converts hours into seconds for time arithmetic.
UTC
Formatting with `tz = "UTC"` avoids host-local time-zone differences.