简体   繁体   中英

How to transform a DateTime to Daylight Saving Time based on the date (not the current locale) in Python

I want to accept dates specified in a locale with daylight saving time (British Summer Time). I will be storing dates as UTC but I want to display, and accept as input, dates in whatever DST mode is relevant for the date.

The 'mode' (daylight saving time or not) will depend on the date entered, not the current mode at the time of entering or display.

I'm not sure whether this is given to me for free and or I have to do a lot of legwork. I've seen indications both ways. For example, when clocks go 'back' the same hour occurs twice, and I'm not clear what should happen in this case.

So:

1 - How do I translate a DateTime (or interpret the datetime) to UTC based on the daylight saving time at the instant the DateTime represents

2 - How do I translate a UTC DateTime to daylight saving time representation at the instant the DateTime represents

Use the tz module in dateutil (you may need to install python-dateutil):

http://labix.org/python-dateutil#head-587bd3efc48f897f55c179abc520a34330ee0a62

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