[英]how to get local timezone datetime for date comparison in django
I am trying to fire a reminder based on a users timezone. 我正在尝试根据用户时区发出提醒。 The users timezone is 'America/New_York' 用户的时区为“ America / New_York”
I attempt to get the users time with the following: 我尝试通过以下方式使用户有时间:
now = datetime.now()
now_plus_10 = test_customer.time_zone.localize(now + timedelta(minutes = 10))
now_minus_10 = test_customer.time_zone.localize(now + timedelta(minutes = -10))
The reminder time is at 13:00. 提醒时间为13:00。 It is gathered from the customer object like this: 它是从客户对象收集的,如下所示:
reminder = customer.reminder #set to 13:00
I then want to send a reminder if within 10 minutes + or - of the reminder. 然后,如果要在提醒的10分钟内+或-内发送提醒。
I do the comparison like: 我做比较:
if reminder > now_minus_10 and gb_real_time < now_plus_10:
reminder_email.send()
else:
print 'ceiling '+str(now_plus_10)
print 'not in window don t send'
print 'floor '+str(now_minus_10)
This prints: 打印:
ceiling 2014-09-03 10:10:54.547901-04:00
not in window don t send
floor 2014-09-03 09:50:54.547901-04:00
I am in the timezone: 'America/Los_Angeles' in which it is 10:00am 我在时区:“ America / Los_Angeles”,时区为10:00 am
Why is it that even though I localized now_plus_10 and now_minus_10 dates, but they do not convert to New_York time which would make them 13:00 like the reminder and in turn fire the reminder? 为什么即使我对now_plus_10和now_minus_10日期进行了本地化,但它们却没有转换为New_York时间,这也会使它们成为13:00的提醒,进而触发提醒?
I think you've misunderstood what localize()
does. 我认为您误解了localize()
作用。 (I assume you're talking about the pytz localize()
, in which case you can see the documentation here .) (我假设您正在谈论pytz localize()
,在这种情况下,您可以在此处查看文档。)
It doesn't convert times, it takes a naive (timezone-unaware) datetime and makes it aware ( assigns it a timezone). 它不转换时间,它需要一个幼稚的(不知道时区的)日期时间并使它知道( 为它分配一个时区)。 Here's what's going on in your example: 这是您的示例中发生的事情:
now = datetime.now() # 10:00am local time (no timezone)
later = now + timedelta(minutes=10) # 10:10am (no timezone)
now_plus_10 = ny_timezone.localize(later) # 10:10am (NY timezone)
If you really want to convert between timezones directly, using pytz
, you can do something like this: 如果您真的想使用pytz
直接在时区之间进行转换,可以执行以下操作:
now = la_timzeone.localize(datetime.now()) # 10:00am local time (LA timezone)
later = now + timedelta(minutes=10) # 10:10am (LA timezone)
later_safe = la_timezone.normalize(later) # because of DST and such
later_ny = later_safe.astimezone(ny_timezone) # 13:10 (NY timezone)
later_ny_safe = ny_timezone.normalize(later_ny) # again
Now, that's complicated! 现在,这很复杂! Which is why both pytz
and Django discourage you from manually localizing and converting time zones. 这就是pytz
和Django都不鼓励您手动定位和转换时区的原因。 You'll have to review the documentation , but basically the Django approach is this: 您必须阅读文档 ,但是基本上Django方法是这样的:
django.utils.timezone.now()
to get the current time in UTC. 使用django.utils.timezone.now()
获取UTC的当前时间。 activate()
. 为此,您必须知道用户的时区,然后调用activate()
。 You can also do it manually with localtime()
. 您也可以使用localtime()
手动进行操作。 I definitely recommend doing things the Django way. 我绝对建议以Django方式进行操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.