简体   繁体   English

'...' in time(...) 在python的timedate类中是什么意思?

[英]What does '…' in time(…) means in timedate class in python?

I am new to python programming.I am learning datetime module.我是 python 编程的新手。我正在学习datetime模块。 But,going further,I saw:但是,更进一步,我看到:

from datetime import datetime
t = datetime.time()
print ("The current time is ", t)

But,I got error as:但是,我得到的错误是:

TypeError                                 Traceback (most recent call last)
<ipython-input-109-91a2f569ccbd> in <module>
      8 #print(type(datetime.time()))
      9 # Get the current time
---> 10 t = datetime.time()
     11 
     12 print ("The current time is ", t)

TypeError: descriptor 'time' of 'datetime.datetime' object needs an argument

so I tried to see the help for datetime in my juypter notebook:所以我试图在我的 juypter 笔记本中查看日期时间的帮助:

class datetime(date)
 |  datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])
 |  
 |  The year, month and day arguments are required. tzinfo may be None, or an
 |  instance of a tzinfo subclass. The remaining arguments may be ints.
 |  
 |  Method resolution order:
 |      datetime
 |      date
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  
 |  time(...)
 |      Return time object with same time but with tzinfo=None.
 |  
 |  
 |  now(tz=None) from builtins.type
 |      Returns new datetime object representing current time local to tz.
 |      
 |        tz
 |          Timezone object.
 |      
 |      If no tz is specified, uses local timezone.
 | 

I saw the class datetime has function time.我看到类datetime具有功能时间。 As given by juypter notebook:正如 juypter notebook 给出的:

time(...)
 |      Return time object with same time but with tzinfo=None.

(I have not posted all functions above).I get confuse what the function parameter is expecting? (我没有发布上面的所有函数)。我对函数参数期望什么感到困惑? I am unclear about these three dots.What does this three dots means?这三个点我不清楚。这三个点是什么意思?

So,I moved to python docs and https://docs.python.org/3/library/datetime.html#datetime.datetime.year .所以,我转到了 python 文档和https://docs.python.org/3/library/datetime.html#datetime.datetime.year

And I saw,而我看到,

datetime.time()
Return time object with same hour, minute, second, microsecond and fold. tzinfo is None. See also method timetz().

Changed in version 3.6: The fold value is copied to the returned time object.  

So,in this docs, i am seeing time() function has no any argument it is expecting.I again get confuse at this point.Since,error says that it expects at least a argument ,but in docs it is showing me the function is not expecting any argument .所以,在这个文档中,我看到time()函数没有它所期望的任何参数。此时我再次感到困惑。因为,错误说它至少需要一个参数,但在文档中它向我展示了该函数不期待任何争论 Can some one clarify me how can I see from juypter notebook what the function is expecting?有人可以澄清我如何从 juypter notebook 看到该功能的期望吗?

In your code you are trying to get the time from the time object directly, so you need to do:在您的代码中,您试图直接从时间对象中获取时间,因此您需要执行以下操作:

from datetime import datetime
t = datetime.now().time()
print ("The current time is ", t)

# The current time is  18:11:56.271907

The class method datetime.now() is described here类方法datetime.now() 在这里描述

Return the current local date and time.返回当前本地日期和时间。 If optional argument tz is None or not specified, this is like today(), but, if possible, supplies more precision than can be gotten from going through a time.time() timestamp (for example, this may be possible on platforms supplying the C gettimeofday() function).如果可选参数 tz 为 None 或未指定,则类似于 today(),但是,如果可能,提供比通过 time.time() 时间戳获得的精度更高的精度(例如,这可能在提供C gettimeofday() 函数)。

If you want to use the function time() directly you need to change your import:如果你想直接使用函数time()你需要改变你的导入:

import datetime
print(datetime.time())

# datetime.time(0, 0)

The import from datetime import datetime means that you are importing the object datetime from the module datetime, into this object you have the nested object time <method 'time' of 'datetime.datetime' objects> . import from datetime import datetime意味着您从模块 datetime 导入对象datetime ,到这个对象中,您有嵌套对象time <method 'time' of 'datetime.datetime' objects>

The datetime module is probably particularly confusing because the module also includes a datetime class, so datetime.datetime refers to the datetime class within the datetime module. datetime 模块可能特别容易混淆,因为该模块还包含一个 datetime 类,因此datetime.datetime指的是 datetime 模块中的 datetime 类。 If you do from datetime import datetime , you're only importing the datetime class, so when you refer to datetime in your code, it's referring to the datetime class, not the whole datetime module.如果您执行from datetime import datetime ,则您只导入了 datetime 类,因此当您在代码中引用 datetime 时,它​​指的是 datetime 类,而不是整个 datetime 模块。

Using the import import datetime you are importing the module directly, where is located the function time() that use the class <class 'datetime.time'>使用 import import datetime直接导入模块,其中使用<class 'datetime.time'>的函数time()所在的位置

Reference to the time class is here时间类的参考在这里

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).一个理想化的时间,独立于任何特定的一天,假设每天正好有 24 60 60 秒(这里没有“闰秒”的概念)。 Attributes: hour, minute, second, microsecond, and tzinfo.属性:小时、分钟、秒、微秒和 tzinfo。

Reference to the datetime class nested into the datetime module is here对嵌套在datetime 模块中datetime 类的引用是here

A combination of a date and a time.日期和时间的组合。 Attributes: year, month, day, hour, minute, second, microsecond, and tzinfo.属性:年、月、日、小时、分钟、秒、微秒和 tzinfo。

Inside this reference you can find the datetime.time() instance method在此参考中,您可以找到datetime.time() 实例方法

Return time object with same hour, minute, second, microsecond and fold.返回具有相同时、分、秒、微秒和折叠的时间对象。 tzinfo is None. tzinfo 是无。 See also method timetz().另见方法 timetz()。

I get confuse what the function parameter is expecting?我对函数参数的期望感到困惑? I am unclear about these three dots.What does this three dots means?这三个点我不清楚。这三个点是什么意思?

It means that the help function was unable to determine the actual arguments.这意味着help函数无法确定实际参数。 The documentation is here ;文档在这里 the method does not expect any arguments.该方法不需要任何参数。 (Yes, it's hard to search the page because there are not enough page anchors and way too many times that the words "date" and "time" appear; sorry about that.) (是的,搜索页面很困难,因为没有足够的页面锚点,而且出现“日期”和“时间”这两个词的次数太多了;对此很抱歉。)

The reason datetime.time() in your code does not work is that you are incorrectly trying to call an instance method as a class method.代码中的datetime.time()不起作用的原因是您错误地尝试将实例方法作为类方法调用。 This does not make sense;这根本不符合逻辑; you need to have an actual thing that represents a date and time, in order to pull the time out of it.您需要有一个代表日期和时间的实际事物,以便从中提取时间。 If you want to know what time it is now, you create the date-and-time-representing-thing that represents "now", and get the time from that .如果你想知道现在是什么时候,在创建日期和时间表示,事情表示“现在”,并从那个时候。 Thus: datetime.now().time() .因此: datetime.now().time() Here, now is a class method that asks the datetime class to create an instance representing "now".这里, now是一个方法,它要求datetime类创建一个表示“现在”的实例。

If you only want to get the actual time then you can simply use:如果您只想获得实际时间,那么您可以简单地使用:

from datetime import datetime

def test_time():

    date = datetime.now()
    time = datetime.time(date)

    print("This is the actual time", time)

datetime.now() gives you the actual date and time combined. datetime.now() 为您提供组合的实际日期和时间。 At this point an instance of the datetime class was created.此时创建了 datetime 类的一个实例。 Then you can pass this date-Object to the time function, which simply returns the actual time.然后您可以将此日期对象传递给时间函数,该函数仅返回实际时间。

Also the signature of the datetime-class shows you, that you have to pass parameters, when you want to instantiate the class.日期时间类的签名还显示,当您想要实例化类时,您必须传递参数。 So if you want to create your own date, you can simply do it in the following way:因此,如果您想创建自己的日期,只需通过以下方式进行:

from datetime import datetime

def test_time():

    date = datetime(year=2020, month=8, day=31, hour=0, minute=0, second=0, microsecond=0)

    print("This is the actual date and time", date)

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

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