Skip to content

Instantly share code, notes, and snippets.

@mpettis
Last active September 2, 2020 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpettis/bc42cbb97691529a9f5dcaeabf73cade to your computer and use it in GitHub Desktop.
Save mpettis/bc42cbb97691529a9f5dcaeabf73cade to your computer and use it in GitHub Desktop.
Converting timestamps from a wrong timezone to a right timezone
### Let's say you get timestamps that do not have their tzone info as part of the timestamp string,
### but stored in a separate field. Further, assume your system has read them in using your local
### tzone. How do use knowledge of our local timezone and that of the the intended timezone to
### correct the timestamp to be in the intended timezone?
###
### In this exercise, we have the following:
### - The timestamp, with no explicit timezone which was read into system using default initial
### timezone of the first user.
### - A second system that has timezone different from the first timezone.
### - The intended timezone of the timestamp, which is what the timestamp should be in.
###
### This is real. I have data read into a system in America/Chicago timezone, which becomes
### the default timezone of unzoned timestamps. This is the *wrong* timezone for the timestamp.
### Further, the data was ported to an Amazon instance that has UTC as its local timezone. It
### is here where I need to cast the timestamp back to it's correct timezone, which in this case,
### is America/New_York.
###
### Here, we have 1:15pm, on days in standard and daylight time, converted from an initial
### timezone to an intended one.
library(lubridate)
library(tidyverse)
## Initial and intended timezone
initial_timezone <- "America/Chicago"
intended_timezone <- "America/New_York"
## Sample timestamps to load and convert
ts_standard_time <- "2020-01-02 13:15:00"
ts_daylight_time <- "2020-06-02 13:15:00"
## Make timestamp objects in the initial timezone, but ultimately have UTC as their representation time zone.
dt_standard <- ymd_hms(ts_standard_time, tz = initial_timezone) %>% with_tz("UTC")
dt_daylight <- ymd_hms(ts_daylight_time, tz = initial_timezone) %>% with_tz("UTC")
## Look at them:
## Standard time, inital tz
dt_standard
#> [1] "2020-01-02 19:15:00 UTC"
## Daylight time, inital tz:
dt_daylight
#> [1] "2020-06-02 18:15:00 UTC"
### Convert:
## Standard time, inital tz:
dt_standard
#> [1] "2020-01-02 19:15:00 UTC"
## Standard time, intended tz:
dt_standard %>%
with_tz(initial_timezone) %>%
force_tz("UTC") %>%
force_tz(intended_timezone)
#> [1] "2020-01-02 13:15:00 EST"
## Daylight time, inital tz:
dt_daylight
#> [1] "2020-06-02 18:15:00 UTC"
## Daylight time, intended tz:
dt_daylight %>%
with_tz(initial_timezone) %>%
force_tz("UTC") %>%
force_tz(intended_timezone)
#> [1] "2020-06-02 13:15:00 EDT"
# <sup>Created on 2020-09-02 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0)</sup>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment