1

Im working with django and a some python code, but now i get the TypeError:can't subtract offset-naive and offset-aware datetimes

Now i guess that django uses Europe/Amsterdam timezone and i don't think i use one with 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

how can i add the timezone using python 2.7 ?

Hans de Jong
  • 2,030
  • 6
  • 35
  • 55

1 Answers1

2

Docs: link

import datetime
from django.utils.timezone import utc

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

    return remaining
Alex Parakhnevich
  • 5,172
  • 4
  • 26
  • 30