Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较python中不同时区的相同时间_Python_Django_Datetime_Timezone_Python Datetime - Fatal编程技术网

比较python中不同时区的相同时间

比较python中不同时区的相同时间,python,django,datetime,timezone,python-datetime,Python,Django,Datetime,Timezone,Python Datetime,我想每天早上7点发一封电子邮件。例如,用户1位于美国/洛杉矶时区。客户2在美国/纽约时区,将在7点收到电子邮件,但在用户1的时间是凌晨4点 我的客户对象如下所示: class Customer(models.Model): user = models.OneToOneField(User, related_name="customer") first_name = models.CharField(max_length=128) last_name = models.Ch

我想每天早上7点发一封电子邮件。例如,用户1位于美国/洛杉矶时区。客户2在美国/纽约时区,将在7点收到电子邮件,但在用户1的时间是凌晨4点

我的客户对象如下所示:

class Customer(models.Model):
    user = models.OneToOneField(User, related_name="customer")
    first_name = models.CharField(max_length=128)
    last_name = models.CharField(max_length=128)
    reminder = models.TimeField(blank=True, null=True)
    time_zone = TimeZoneField()
我可以通过以下方式获取用户时区中的当前时间:

    now = datetime.now()
    settingstime_zone = timezone(settings.TIME_ZONE)
    now = settingstime_zone.localize(datetime.now())
    relative_time = now.astimezone(customer.time_zone)
这为我提供了用户相对于时区的正确时间

现在如何检查该时区是否为上午7点?

检查datetime对象的hour属性就足够了

>>> from datetime import datetime
>>> from pytz import timezone
>>> 
>>> customer_time_zone = 'America/Los_Angeles'
>>> 
>>> #gets a non naive datetime (utc)
>>> utcnow = datetime.utcnow()
>>> utcnow = utcnow.replace(tzinfo=pytz.utc)
>>> 
>>> #localize current utc with customer_time_zone
>>> relative_time = utcnow.astimezone(timezone(customer_time_zone))
>>> utcnow, relative_time
(datetime.datetime(2014, 9, 4, 5, 41, 2, 294757, tzinfo=<UTC>), datetime.datetime(2014, 9, 3, 22, 41, 2, 294757, tzinfo=<DstTzInfo 'America/Los_Angeles' PDT-1 day, 17:00:00 DST>))
>>> relative_time.hour
22
>>> utcnow.hour
5
你试过使用相对时间吗?