django 1.4 timezone.now() vs datetime.datetime.now()

前端 未结 4 1995
自闭症患者
自闭症患者 2020-12-08 06:03

I\'m a bit confused by the daylight savings handling

settings.py:

TIME_ZONE = \'Europe/London\'
USE_TZ = True

in the django shell:<

4条回答
  •  臣服心动
    2020-12-08 07:09

    According to timezone.now() source:

    def now():
        """
        Returns an aware or naive datetime.datetime, depending on settings.USE_TZ.
        """
        if settings.USE_TZ:
            # timeit shows that datetime.now(tz=utc) is 24% slower
            return datetime.utcnow().replace(tzinfo=utc)
        else:
            return datetime.now()
    

    It's based on utc instead of your default timezone. You could achieve same value by using

    now = timezone.make_aware(datetime.datetime.now(),timezone.get_default_timezone())
    print now.astimezone(timezone.utc)
    

提交回复
热议问题