Python's time module time and datetime module

A, time module

1, several types of time indicated

1.Timestamp: Represents a time stamp 1970 00:00:00 January 1 according to the present offset in seconds, is the float type. The main function returns the timestamp time (), clock () and so on.
Here Insert Picture Description
2.Time string: Formatting time, formatted structure makes time more readable. Comprising a fixed format and custom format.
Here Insert Picture Description

3.Tuple type of time : Struct_time tuple total of nine elements, the function returns struct_time main gmtime (), localtime () and strptime ().Both can get the value of the index tuple corresponding item directly in a tuple you can also call the members of the symbol to obtain the corresponding value
Here Insert Picture Description
Here Insert Picture Description

2, the commonly used type conversion time

2.1 yuan set of time stamp is converted to

  • time.mktime (tuple Time): convert the time stamp tuple

Here Insert Picture Description

2.2 yuan set time into a string time

  • The time.strftime (symbol string format, time tuple): Returns a string representation of the local time. To a tuple representing time or struct_time (as returned by time.localtime () and time.gmtime ()) is converted to time format string in the format determined by the parameter format. If not specified, the incoming time.localtime ().

  • time.strptime (string [, format]): The format string is converted to the function struct_time The time.strftime () function is the inverse operation. time strptime () function in accordance with the specified format to parse a time string tuple of time. Therefore, the function returns struct_time object.

  • Parameters (symbol string format)
    Here Insert Picture Description
    Here Insert Picture Description

2.3 converting the time stamp to a string type Time

Here Insert Picture Description

2.4 is converted to a time stamp tuple

Here Insert Picture Description

3. Other usage time module

  • sleep (secs): thread to sleep a specified time, in seconds
  • clock() :这个函数, 函数以浮点数计算的秒数返回当前的CPU时间。用来衡量不同程序的耗时,比time.time()更有用。在不同的系统上含义不同。在NUix系统上,它返回的是“进程时间”(返回的是CPU时间),返回时间戳。而在Windows中,第一次调用,返回的是进程运行时实际时间。而第二次之后的调用是自第一次调用以后到现在的运行时间。

二、datetime模块

  • datatime模块重新封装了time模块,提供更多接口,提供的类有:date,time,datetime,timedelta,tzinfo。

  • datetime.date(year, month, day)

  • date.max、date.min:date对象所能表示的最大、最小日期;

  • date.resolution:date对象表示日期的最小单位。这里是天。

  • date.today():返回一个表示当前本地日期的date对象;

  • date.fromtimestamp(timestamp):根据给定的时间戮,返回一个date对象

Here Insert Picture Description

d1 = date (2011,06,03) #date objects
d1.year, date.month, date.day: year, month, day;
d1.replace (year, month The, Day): generates a new date object with parameter specifies the year, month, day instead of the original object's properties. (Original target remains unchanged)
d1.timetuple (): Returns the time.struct_time objects corresponding to the date;
d1.weekday (): Returns the weekday, if it is Monday, returns 0; if it is 2 weeks, 1 return to such push;
d1.isoweekday (): returns WEEKDAY, if Monday, return 1; if 2 weeks, return to 2, and so on;
d1.isocalendar (): returns the format such as (year, month, day) of the element group;
d1.isoformat (): returns the format as 'YYYY-MM-DD' string;
d1.strftime (FMT): time module and the same format.

Here Insert Picture Description

Published 102 original articles · won praise 21 · views 5346

Guess you like

Origin blog.csdn.net/ranrancc_/article/details/101268428