最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Python中调用time.time()出错:AttributeError: type object ‘datetime.time’ has no attribute ‘time’

Python crifan 13178浏览 0评论

Python代码:

from datetime import datetime, date, time, timedelta
    curTimestamp = time.time()
    gLog.debug(“type(curTimestamp)=%s, curTimestamp=%s”, type(curTimestamp), curTimestamp)

出错:

  File “/root/html/SIPEvents/sipevents/views.py”, line 687, in create_event_qrcode
    curTimestamp = time.time()
AttributeError: type object ‘datetime.time’ has no attribute ‘time’

但是诡异的是:

之前这个代码还是可以正常运行的啊。。。

AttributeError: type object ‘datetime.time’ has no attribute ‘time’

Error python datetime/time imports… – Stack Overflow

python – type object ‘datetime.datetime’ has no attribute ‘datetime’ – Stack Overflow

python datetime time

8.1. datetime — Basic date and time types — Python 2.7.12 documentation

class datetime.date

An idealized naive date, assuming the current Gregorian calendar always was, and always will be, in effect. Attributes: yearmonth, and day.

class datetime.time

An idealized time, independent of any particular day, assuming that every day has exactly 24*60*60 seconds (there is no notion of “leap seconds” here). Attributes: hourminutesecondmicrosecond, and tzinfo.

class datetime.datetime

A combination of a date and a time. Attributes: yearmonthdayhourminutesecondmicrosecond, and tzinfo.

class datetime.timedelta

A duration expressing the difference between two datetime, or datetime instances to microsecond resolution.

class datetime.tzinfo

An abstract base class for time zone information objects. These are used by the datetime and time classes to provide a customizable notion of time adjustment (for example, to account for time zone and/or daylight saving time).

15.3. time — Time access and conversions — Python 2.7.12 documentation

“This module provides various time-related functions. For related functionality, see also the datetime and calendar modules”

->

看起来是:

datetime的内部也有个class叫做time

但是本身和datetime并列的,就有个time

而:

datetime中的time这个类,没有time()

->所以其官网例子中:

>>> import time
>>> from datetime import date
>>> today = date.today()
>>> today
datetime.date(2007, 12, 5)
>>> today == date.fromtimestamp(time.time())

都是用的time的函数:time.time()

time类本身,是有time()函数的:

time.time()

Return the time in seconds since the epoch as a floating point number. Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second. While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls.

所以:

此处应该是直接导入time,而不是导入datetime中的time

即把:

from datetime import datetime, date, time, timedelta

换成:

from datetime import datetime, date, timedelta
import time

即可正常使用time类中的time()函数了:

    curTimestamp = time.time()
    gLog.debug(“type(curTimestamp)=%s, curTimestamp=%s”, type(curTimestamp), curTimestamp)

输出:

type(curTimestamp)=<type ‘float’>, curTimestamp=1472636683.04

转载请注明:在路上 » [已解决]Python中调用time.time()出错:AttributeError: type object ‘datetime.time’ has no attribute ‘time’

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.174 seconds, using 22.12MB memory