简体   繁体   English

在Django中获取系统时间w / timezone绕过默认时区

[英]Get system time w/timezone in Django bypassing default timezone

As long as I'm using plain ol' Python shell, the datetime.datetime.now() command works fine to get system's local (non-UTC) time. 只要我使用普通的'Python shell', datetime.datetime.now()命令就可以正常工作以获得系统的本地(非UTC)时间。

But I'm working on a Django project where the time zone is changed in settings.py with TIME_ZONE = 'UTC' . 但是我正在开发一个Django项目,其中在settings.py使用TIME_ZONE = 'UTC'更改了时区。

I've tried many solutions from django.utils timezone to tzlocal module, but none of them works. 我尝试django.utils timezonetzlocal模块的许多解决方案,但它们都tzlocal All of them return either incorrect or UTC time. 所有这些都返回错误或UTC时间。

All of the solutions work if I change the timezone in settings.py to my local timezone. 如果我将settings.py的时区更改为我的本地时区,则所有解决方案都有效。 But I can't do that, so is there any way to bypass the default timezone option in settings.py ? 但我不能这样做,那么有没有办法绕过settings.py的默认时区选项? Or any way the settings.py 's timezone can be automatically updated? 或者任何方式settings.py的时区都可以自动更新? If I remove the TIME_ZONE line, I don't know why, but it seems to get a random timezone. 如果我删除TIME_ZONE行,我不知道为什么,但它似乎得到一个随机的时区。

EDIT - 编辑 -

I know that the timezone can be entered manually with pytz , but I don't want to do that. 我知道的是,时区可以手动输入pytz ,但我并不想这样做。 I want to get the local system timezone, but WITHOUT Django's moderation of the timezone. 我想获得本地系统时区,但没有Django对时区的调节。

Thanks. 谢谢。

To get the time for a different timezone, use this code: 要获得不同时区的时间,请使用以下代码:

# create another timezone instance
import pytz
new_york_tz = pytz.timezone("America/New_York")

# get the current date and time, with the timezone set in your settings.py
from django.utils import timezone
d = timezone.now()
print(d)

# get the date and time in your other timezone
d2 = new_york_tz.normalize(d)
print(d2)

See: https://docs.djangoproject.com/en/1.10/topics/i18n/timezones/#naive-and-aware-datetime-objects 请参阅: https//docs.djangoproject.com/en/1.10/topics/i18n/timezones/#naive-and-aware-datetime-objects

Django seems to be putting its timezone in the TZ environment variable. Django似乎将其时区放在TZ环境变量中。 Try del os.environ['TZ'] then using tzlocal. 尝试del os.environ['TZ']然后使用tzlocal。

You can use Javascript to get the Timezone Offset from browser to UTC. 您可以使用Javascript从浏览器获取时区偏移到UTC。 Here is the code. 这是代码。

var d = new Date()
var n = d.getTimezoneOffset()
alert(n)

The result is the amount of minutes. 结果是分钟数。 For example, you will get 480 in Los Angeles. 例如,你将在洛杉矶获得480。 if the result is negative number, the timezone should be the "West", opposite, the positive number means "East". 如果结果为负数,则时区应为“西”,相反,正数表示“东”。 AND 1 timezone = 60 minutes, so, you can figure out 480 / 60 = 8. Then the result is Los Angeles is in the west 8 timezone. AND 1时区= 60分钟,所以,你可以算出480/60 = 8.然后结果是洛杉矶在西8时区。 Then you can use pytz to get the local time. 然后你可以使用pytz来获取当地时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM