问题描述
说我在模型中有一个类 class Post(models.Model):
time_posted = models。 DateTimeField(auto_now_add = True,blank = True)
def get_time_diff(self):
timediff = timediff = datetime.datetime.now() - self.time_posted
print timediff#这行从来没有执行
return timediff
我定义了一个get_time_diff来获取时间差当Post发布到现在的时候,根据文档,DateTimeField应该能够自动转换为datetime,是正确的吗?为什么print语句永远不会运行?您如何提取时差?
除此之外,如果您有时差,是否有简单的方法将时差转换为整数,如
你的代码已经在工作了; 返回。
要获得秒数的总数,您需要调用生成的 timedelta
:
from django.utils.timezone import utc
def get_time_diff(self):
如果self.time_posted:
now = datetime.datetime.utcnow()。replace(tzinfo = utc)
timediff = now - self.time_posted
return timediff.total_seconds()
.total_seconds()
返回一个 float
值,包括微秒。
请注意,您需要使用时区知道 datetime
对象,因为Django DateTimeField
处理时区知道 datetime
对象也是。请参阅。
演示 .total_seconds()
(使用naive datetime
对象,但原则是一样的) :
>>> import datetime
>>>> time_posted = datetime.datetime(2013,3,31,12,55,10)
>>> timediff = datetime.datetime.now() - time_posted
>>> timediff.total_seconds()
1304529.299168
因为这两个对象都是时区感知(有一个 .tzinfo
属性不是无
),他们之间的计算照顾时区,从另一个减去一个将做正确事情当考虑到任何一个对象的时区。
Say I have a class in model
class Post(models.Model):
time_posted = models.DateTimeField(auto_now_add=True, blank=True)
def get_time_diff(self):
timediff = timediff = datetime.datetime.now() - self.time_posted
print timediff # this line is never executed
return timediff
I defined a get_time_diff to get the time difference from the time when the Post is posted up to now, according to the document, the DateTimeField should be able to be converted to datetime automatically, is that correct? Why the print statement is never being run? How can you extract the time difference?
Beside, if you get a time difference, is there an easy way to convert the time difference to an integer, like the number of seconds of the total time.
Your code is already working; a datetime.timedelta
object is returned.
To get the total number of seconds instead, you need to call the .total_seconds()
method on the resulting timedelta
:
from django.utils.timezone import utc
def get_time_diff(self):
if self.time_posted:
now = datetime.datetime.utcnow().replace(tzinfo=utc)
timediff = now - self.time_posted
return timediff.total_seconds()
.total_seconds()
returns a float
value, including microseconds.
Note that you need to use a timezone aware datetime
object, since the Django DateTimeField
handles timezone aware datetime
objects as well. See Django Timezones documentation.
Demonstration of .total_seconds()
(with naive datetime
objects, but the principles are the same):
>>> import datetime
>>> time_posted = datetime.datetime(2013, 3, 31, 12, 55, 10)
>>> timediff = datetime.datetime.now() - time_posted
>>> timediff.total_seconds()
1304529.299168
Because both objects are timezone aware (have a .tzinfo
attribute that is not None
), calculations between them take care of timezones and subtracting one from the other will do the right thing when it comes to taking into account the timezones of either object.
这篇关于Django:如何从时间上获得时差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!