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
  • 使用pytz的Datetime时区转换

  • datetime with pytz timezone. Different offset depending on how tzinfo is set
  • 与pytz时区的datetime。不同的偏移量取决于tzinfo的设置方式

  • pytz localize vs datetime replace
  • 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日期减去一天?

更多相关文章

  1. python基础编程_12_日期格式的使用和转换
  2. Python -在文本文件中添加日期戳
  3. 查看/修改Linux时区和时间
  4. Linux时区设置和时间同步-基于CentOS 6(最小安装)
  5. 请问用PL/SQL如何判断两个日期之间相差的天数?
  6. [置顶] SQL Server 日期相关
  7. 如何用sql语言只获得数据库当前日期,且格式为"yyyy-mm-dd"?
  8. 我想在每次更新或在SQL Server中插入行时获取当前日期时间
  9. oracle --sql--转换表字段日期格式(年月日时分秒转换年月日)

随机推荐

  1. Android事件处理之监听事件
  2. Android异步处理二:使用AsyncTask异步更新
  3. 《Android/OPhone 开发完全讲义》目录
  4. Android 面试题总结之Android 基础(三)
  5. Android 并发之CountDownLatch、CyclicBa
  6. Android 自定义RadioButton样式 RadioGro
  7. 搭建 Android 2.2 开发环境
  8. Android如何让TextView子元素随着父元素
  9. android 开发技巧(9)--为文本添加发亮的
  10. Android - OpenCv - 解决预览图像自动旋