How to get the current time in Python

前端 未结 30 1376
滥情空心
滥情空心 2020-11-22 06:47

What is the module/method used to get the current time?

30条回答
  •  有刺的猬
    2020-11-22 07:29

    This question doesn't need a new answer just for the sake of it ... a shiny new-ish toy/module, however, is enough justification. That being the Pendulum library, which appears to do the sort of things which arrow attempted, except without the inherent flaws and bugs which beset arrow.

    For instance, the answer to the original question:

    >>> import pendulum
    >>> print(pendulum.now())
    2018-08-14T05:29:28.315802+10:00
    >>> print(pendulum.now('utc'))
    2018-08-13T19:29:35.051023+00:00
    

    There's a lot of standards which need addressing, including multiple RFCs and ISOs, to worry about. Ever get them mixed up; not to worry, take a little look into dir(pendulum.constants) There's a bit more than RFC and ISO formats there, though.

    When we say local, though what do we mean? Well I mean:

    >>> print(pendulum.now().timezone_name)
    Australia/Melbourne
    >>>
    

    Presumably most of the rest of you mean somewhere else.

    And on it goes. Long story short: Pendulum attempts to do for date and time what requests did for HTTP. It's worth consideration, particularly for both its ease of use and extensive documentation.

提交回复
热议问题