Windows上virtualenv的区域设置

时间:2017-12-11 10:21:31

标签: django python-3.x datetime virtualenv locale

我有两个虚拟环境,我在Windows上使用DJANGO 1.10.1。我在一个格式为'%c'的datetime.strptime对象上出现错误 - 我正在使用此文件进行测试 - test-py.py

import re, datetime
from django.utils.timezone import utc

def GetDate(date_str=None):
    date_str = "created on Thu Oct 28 08:27:43 2017 - by user bla@bla.com"
    pattern = re.compile('[a-z-A-Z]{3}\s[a-z-A-Z]{3}\s*\d* [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}')
    created_date = datetime.datetime.strptime(re.findall(pattern, date_str)[0], '%c').replace(tzinfo=utc)
    return created_date

此代码适用于一个virtualenv但不适用于另一个 - 并返回错误。起初我以为它是python语言环境,所以我在python中检查了locale的虚拟ENV 1(工具):

(tools) C:\xeb\django_tools>python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.getdefaultlocale()
('da_DK', 'cp1252')

Tools env上的test-py.py脚本的返回是

(tools) C:\xeb\django_tools>python test-py.py
2017-10-28 08:27:43+00:00

虚拟ENV 2 - (工具2)

(tools2) C:\xeb\django_tools>python
Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.getdefaultlocale()
('da_DK', 'cp1252')

Tools2 env上返回test-py.py脚本

(tools2) C:\xeb\django_tools>python test-py.py
Traceback (most recent call last):
File "test-py.py", line 14, in <module>
print(GetInfo())
File "test-py.py", line 8, in GetInfo
created_date = datetime.datetime.strptime(re.findall(pattern, test_str)[0], '%c').replace(tzinfo=utc)
File "C:\Python34\Lib\_strptime.py", line 500, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "C:\Python34\Lib\_strptime.py", line 337, in _strptime
(data_string, format))
ValueError: time data 'Thu Oct 28 08:27:43 2017' does not match format '%c'

编辑:

在DJANGO 1.10.1中 - TIME_ZONE设置设置为UTC。阅读下面的评论后,我检查了python shell中的LC_TIME变量以及加载django设置的交互式shell - 在每种情况下和两个环境中

locale.getlocale(locale.LC_TIME)

返回

(None, None)

将TIME_ZONE设置更改为其他任何设置仍会返回None。

那么为什么同一代码只在一个环境中为'%c'返回匹配格式错误。

0 个答案:

没有答案
相关问题