简体   繁体   中英

Python or R timestamp conversion taking into account daylight savings automatically

Is there a way to convert a UTC timestamp to local timestamp (US/east/west/central etc..) and automatically take into account Daylight savings at the current time of execution?

For example, I have timeseries data stored in a central server in UTC format. When I extract and convert to local timezone,

import pytz
local_tz = pytz.timezone('US/Eastern')

It seems to just subtract 5hours. However we are currently in daylight saving mode, so it should only subtract 4hrs. But this should be dynamic such that when November comes around, it should automatically subtract 5 hours again. Is there a way to do this in python and/or R?

You do not need to change the timestamp itself. It's just the formatting which will be different. This is usually the locale defined in environmental variables like this in R on Linux:

time <- Sys.time()
time
#> [1] "2022-04-13 09:53:00 UTC"
Sys.setenv(TZ="US/Eastern")
time
#> [1] "2022-04-13 05:53:00 EDT"

Created on 2022-04-13 by the reprex package (v2.0.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM