具有相同时区但不同的utcoffset()的Datetime对象
In my django application i'm confronting two datetime objects (self.dueDate is a date object):
在我的django应用程序中,我遇到了两个日期时间对象(self.dueDate是一个日期对象):
ref_time = timezone.localtime(timezone.now(), timezone.get_default_timezone() )
threshold = datetime.combine( self.dueDate,
time(tzinfo=timezone.get_default_timezone()))
- timedelta(days = 1)
I'm constructing them to have the same timezone (which they have), but they end up having two different UTC offsets.
我正在构建它们以具有相同的时区(它们具有相同的时区),但它们最终具有两个不同的UTC偏移。
>>>print threshold, threshold.tzinfo
2015-03-13 12:08:00+00:50 Europe/Rome
>>>print ref_time, ref_time.tzinfo
2015-03-13 12:48:29.372984+01:00 Europe/Rome
Why is this happening? How can it be that there are two different offsets for the same tz? (and why would that offest be 50 minutes?)
为什么会这样?对于相同的tz,有两种不同的偏移量怎么样? (为什么那会是50分钟?)
1 个解决方案
#1
0
A timezone may have different utc offsets at different times. time(tzinfo=tz)
uses a default utc offset e.g., for the earliest date that is most likely is not what you want. See:
时区在不同时间可能具有不同的utc偏移。 time(tzinfo = tz)使用默认的utc偏移量,例如,对于最有可能不是您想要的最早日期。看到:
- Datetime Timezone conversion using pytz
- datetime with pytz timezone. Different offset depending on how tzinfo is set
- pytz localize vs datetime replace
使用pytz的Datetime时区转换
与pytz时区的datetime。不同的偏移量取决于tzinfo的设置方式
pytz localize vs datetime replace
To get the current time in tz
timezone as an aware datetime object:
要将tz timezone中的当前时间作为有意识的日期时间对象获取:
from datetime import datetime
ref_time = datetime.now(tz)
To get a midnight a day before self.dueDate
as an aware datetime object:
要在self.dueDate作为知晓日期时间对象之前的一天午夜:
from datetime import time as datetime_time, timedelta
from django.utils import timezone
midnight_yesterday = datetime.combine(self.dueDate, datetime_time()) - timedelta(1)
threshold = timezone.make_aware(midnight_yesterday)
Note: threshold
may be more/less than 24 hours ago, see How can I subtract a day from a python date?.
注意:阈值可能多于/少于24小时前,请参阅如何从python日期减去一天?
更多相关文章
- python基础编程_12_日期格式的使用和转换
- Python -在文本文件中添加日期戳
- 查看/修改Linux时区和时间
- Linux时区设置和时间同步-基于CentOS 6(最小安装)
- 请问用PL/SQL如何判断两个日期之间相差的天数?
- [置顶] SQL Server 日期相关
- 如何用sql语言只获得数据库当前日期,且格式为"yyyy-mm-dd"?
- 我想在每次更新或在SQL Server中插入行时获取当前日期时间
- oracle --sql--转换表字段日期格式(年月日时分秒转换年月日)