2014-09-03 29 views
0

我有一段时间,我希望提醒用户何时到达各自的时区。它存储为时间字段:如何在django的时区中创建特定时间的日期时间对象

reminder = models.TimeField(blank=True, null=True) 

我有一个时区与每个用户关联。它看起来像:

customer1_timezone = customer1.time_zone #la timezone 
customer2_timezone = customer2.time_zone #ny timezone 

比方说,customer1和customer2都有提醒设置为上午7点。

我如何指示每个用户相对于他们的timzone早上7点?

回答

2

我使用这样的

def user_time(time_value, customer): 
    tz = pytz.timezone(customer.time_zone) 
    c_tz = pytz.timezone(settings.TIME_ZONE) 
    d_tz = c_tz.normalize(c_tz.localize(time_value)) 
    return tz.normalize(d_tz.astimezone(tz)) 

这会得到及时为客户,那么你只需要检查它是早上7点。你甚至可以使用strftime()。 user_time(datetime.now(),customer).strftime(“%H”)==“07”

+0

什么是“值”?我使用上面编辑的时间字段。我怎样才能将我的时间字段转换为您的价值领域? – Atma 2014-09-04 00:44:42

+0

Opps,'值'应该是'time_value' – 2014-09-04 14:04:52

+0

@Atma:从日期时间objet('user_time()'返回它们)获得一小时,使用'.hour'属性。 – jfs 2014-09-14 16:29:16

相关问题