django python datetime天真且意识到

时间:2013-12-02 17:10:37

标签: django datetime timezone

我正在使用django和一些python代码,但现在我得到了TypeError:无法减去offset-naive和offset-aware datetimes

现在我猜django使用欧洲/阿姆斯特丹时区,我不认为我使用python。

class SkillTrainingTimer(models.Model):
    character = models.ForeignKey(Character, unique=True)
    skill = models.ForeignKey(Skill, blank=True)
    trainingground = models.ForeignKey(TrainingGround)
    timer = models.DateTimeField()


    def time_remaining(self):
        remaining = datetime.datetime.now() - self.timer

        return remaining

如何使用python 2.7添加时区?

1 个答案:

答案 0 :(得分:2)

文档:link

import datetime
from django.utils.timezone import utc

def time_remaining(self):
    remaining = datetime.datetime.utcnow().replace(tzinfo=utc) - self.timer

    return remaining