擅长:python、mysql、java
<p>根据<code>timezone.now()</code><a href="https://docs.djangoproject.com/en/stable/_modules/django/utils/timezone/#now" rel="noreferrer">source</a>:</p>
<pre><code>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()
</code></pre>
<p>它基于<code>utc</code>而不是默认时区。你可以通过使用</p>
<pre><code>now = timezone.make_aware(datetime.datetime.now(),timezone.get_default_timezone())
print now.astimezone(timezone.utc)
</code></pre>