Python's use of time-and datetime-related modules

Source: Internet
Author: User
Tags local time parse string

First, preface

Learn about Python processing time-related modules, times, datetime

Second, Time module

First look at the time module

Take a look at help (time) to see what functions are available under the time module:

    time() -- return current time in seconds since the Epoch as a float    clock() -- return CPU time since process start as a float    sleep() -- delay for a number of seconds given as a float    gmtime() -- convert seconds since Epoch to UTC tuple    localtime() -- convert seconds since Epoch to local time tuple    asctime() -- convert time tuple to string    ctime() -- convert time in seconds to string    mktime() -- convert local time tuple to seconds since Epoch    strftime() -- convert time tuple to string according to format specification    strptime() -- parse string to time tuple according to format specification    tzset() -- change the local timezone

There are three ways to show time:

1. Timestamp (timestamp): The timestamp represents the offset that is calculated in seconds from 00:00:00 on January 1, 1970. The float type is returned, and the function that returns the timestamp has time (), clock ()

2, tuple (struct_time) mode: Struct_time tuple has 9 elements, the function that returns Struct_time mainly has Gmtime (), localtime (), Strptime (). Several elements in this way tuple are listed below:

3. Format the string (format time) way: Formatting times, formatted structures make time more readable. Includes custom formats and fixed formats. Like "2018-5-11."

Let's talk about some of the commonly used functions:

1, Time.sleep ()

Time.sleep (secs): Thread delays the specified time run. Unit is seconds.

2, Time.time ()

Gets the current time stamp

>>>time.time()      1525954582.579378
3, Time.clock ()

Calculate the time taken by the CPU calculation

>>>time.sleep(3)print(time.clock())0.061649
4, Time.gmtime ()
>>>a=time.gmtimeprint(a)time.struct_time(tm_year=2018, tm_mon=5, tm_mday=10, tm_hour=6, tm_min=53, tm_sec=30, tm_wday=3, tm_yday=130, tm_isdst=0)

The Gmtime () method is a struct_time that converts a timestamp to the UTC time zone (0 o'clock Zone). , a total of 9 parameters, meaning as shown in the chart above

5, Time.localtime ()
>>>a=time.localtime()print(a)time.struct_time(tm_year=2018, tm_mon=5, tm_mday=11, tm_hour=9, tm_min=22, tm_sec=14, tm_wday=4, tm_yday=131, tm_isdst=0)

Converts a timestamp to the struct_time of the current time zone. The secs parameter is not provided, whichever is the current time. , a total of 9 parameters, meaning as shown in the chart above

6, Time.asctime ([t]):

A tuple or struct_time representing time is represented in this form: ' Sun June 20 23:21:05 1993 '. If there are no parameters, Time.localtime () will be passed in as a parameter.

>>>time.asctime()‘Thu May 5 14:55:43 2011‘

Converts a timestamp to the struct_time of the current time zone. The secs parameter is not provided, whichever is the current time. , a total of 9 parameters, meaning as shown in the chart above

7, Time.ctime ()
>>>time.ctime(3600)print(a)Thu Jan  1 09:00:00 1970

Convert the timestamp to this form of ' Sun June 20 23:21:05 1993 ' Format time. If there are no parameters, Time.localtime () will be passed in as a parameter.

8, Time.strftime (format[, T])
>>>local_time = time.localtime()print(time.strftime(‘%Y-%m-%d  %H:%M:%S‘, local_time))2018-05-11  09:38:46

Converts a tuple or struct_time that represents time, such as returned by Time.localtime () and Time.gmtime (), to a formatted time string. If T is not specified, the Time.localtime () is passed in. If any of the elements in the tuple are out of bounds, the ValueError error will be thrown.

9, Time.strptime (string[, format])
>>>a=time.localtime()print(a)time.struct_time(tm_year=2018, tm_mon=5, tm_mday=11, tm_hour=9, tm_min=22, tm_sec=14, tm_wday=4, tm_yday=131, tm_isdst=0)

Converts a formatted time string to Struct_time. In fact it is inverse operation with strftime (). In this function, format defaults to: "%a%b%d%h:%m:%s%Y".

They convert relationships such as:

Time formatting:

%y 两位数的年份表示(00-99)%Y 四位数的年份表示(000-9999)%m 月份(01-12)%d 月内中的一天(0-31)%H 24小时制小时数(0-23)%I 12小时制小时数(01-12) %M 分钟数(00=59)%S 秒(00-59)%a 本地简化星期名称%A 本地完整星期名称%b 本地简化的月份名称%B 本地完整的月份名称%c 本地相应的日期表示和时间表示%j 年内的一天(001-366)%p 本地A.M.或P.M.的等价符%U 一年中的星期数(00-53)星期天为星期的开始%w 星期(0-6),星期天为星期的开始%W 一年中的星期数(00-53)星期一为星期的开始%x 本地相应的日期表示%X 本地相应的时间表示%Z 当前时区的名称%% %号本身
Third, datetime module

The datetime built-in object relationships are as follows:

Divided into Date/time/timedelta/tzinfo four classes, of which date, tzinfo and respectively have their own subclasses Datetim/timezone

1. Datetime.datetime.now () Gets the current time string
>>>datetime.datetime.now()2018-05-11 10:05:29.397237
2, Datetime.datetime.now (). Timestamp () Gets the current time timestamp
>>>datetime.datetime.now().timestamp()1526004460.797517
3, Datetime.datetime.today () Get current time month day
>>>datetime.datetime.today()2018-05-11
4. Some other methods
In [42]: #设置一个时间对象In [43]: d=datetime(2016,7,21,22,23,15)In [44]: #自定义格式显示In [45]: d.strftime(‘%x %X‘)Out[45]: ‘07/21/16 22:23:15‘In [46]: #显示英文格式In [47]: d.ctime()Out[47]: ‘Thu Jul 21 22:23:15 2016‘In [48]: #显示日历(年,年中第几周,周几)In [49]: d.isocalendar()Out[49]: (2016, 29, 4)

DateTime sub-module unit interval: datetime.resolution=1 microseconds.

The time interval for the date submodule is 1 days date.resolution=1 days

Time interval multiplied by one number, indicating a few days apart

>>>from datetime import date#现在时间是date.today()datetime.date(2018, 5, 11)#100天以前的日期是result=date.today()-date.resolution*100print(result)输出结果:2018-01-31

Python's use of time-and datetime-related modules

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.