简体   繁体   English

Django日期过滤器“天真”是什么意思?

[英]What does Django Date filter mean by 'Naive'?

I want to use the 'c' date format with the Django date filter. 我想在Django日期过滤器中使用'c'日期格式。 That format references 'naive' dates. 该格式引用“原始”日期。 I don't want to have timezones in my template (that's held elsewhere in my xml). 我不想在模板中放置时区(在xml的其他地方保存)。

I'm not sure what that is, the documentation for django doesn't mention it, nor does the PHP site it references. 我不确定那是什么,django的文档没有提及它,它所引用的PHP站点也没有提及。

What is it, and how do I get rid of it? 这是什么,我如何摆脱它?

The documentation refers to python dates, of these available types . 这些文档引用了这些可用类型的 python date。

An object of type time or datetime may be naive or aware. 类型为time或datetime的对象可能是幼稚的或感知的。 A datetime object d is aware if d.tzinfo is not None and d.tzinfo.utcoffset(d) does not return None . datetime对象d知道d.tzinfo是否不为None,并且d.tzinfo.utcoffset(d)不返回None If d.tzinfo is None , or if d.tzinfo is not None but d.tzinfo.utcoffset(d) returns None , d is naive. 如果d.tzinfoNone ,或者d.tzinfo不为Noned.tzinfo.utcoffset(d)返回None ,则d天真。 A time object t is aware if t.tzinfo is not None and t.tzinfo.utcoffset(None) does not return None . time对象t知道t.tzinfo不为Nonet.tzinfo.utcoffset(None)不返回None Otherwise, t is naive. 否则, t天真。

So naive just means it does not have any time zone information. 因此,天真意味着它没有任何时区信息。

To make something 'aware' follow this method by unutbu : 要使某些东西“知道”,请通过unutbu遵循此方法

In general, to make a naive datetime timezone-aware, use the localize method : 通常,要使原始的datetime时区感知,请使用localize方法

 import datetime import pytz unaware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0) aware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0, pytz.UTC) now_aware = pytz.utc.localize(unaware) assert aware == now_aware 

For the UTC timezone, it is not really necessary to use localize since there is no daylight savings time calculation to handle: 对于UTC时区,实际上没有必要使用localize因为没有夏时制可以计算:

 now_aware = unaware.replace(tzinfo=pytz.UTC) 

works. 作品。 ( .replace returns a new datetime; it does not modify unaware .) .replace返回一个新的datetime;它不修改unaware 。)

To make it unaware set the timezone to None . 要使其不知道,请将时区设置为None

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

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