简体   繁体   English

如何根据Python中的日期(不是当前语言环境)将DateTime转换为夏令时

[英]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. 我将日期存储为UTC但我想显示,并接受任何DST模式与日期相关的日期作为输入。

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 1 - 如何根据DateTime表示的时刻的夏令时将DateTime(或解释日期时间)转换为UTC

2 - How do I translate a UTC DateTime to daylight saving time representation at the instant the DateTime represents 2 - 如何在DateTime表示的瞬间将UTC日期时间转换为夏令时表示

Use the tz module in dateutil (you may need to install python-dateutil): 在dateutil中使用tz模块(您可能需要安装python-dateutil):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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