Python base = = Time Processing module

Source: Internet
Author: User

Time Module

There are many modules in Python that allow us to process time information.

  • Time Module
  • datetime Module
  • Pytz Module
  • Dateutil Module

Here we highlight the first two types of

Time Module

Time.time ()
Returns the number of seconds elapsed from the current time in Unix time (1970.1.1 00:00:00)
The return value, also known as a timestamp, is a floating-point type

Time.localtime (seconds)
Converts a timestamp number of seconds to a time tuple representing local time
Returns the time tuple of the current local time directly if no parameters are passed in

Time tuples:(Tm_year, Tm_mon, Tm_mday, Tm_hour, Tm_min, Tm_sec, Tm_wday , Tm_yday, TM_ISDST)

  • tm_wday: Starting from 0, indicating the day of the week
  • tm_yday: Day of the week
  • TM_ISDST: The decision flag for daylight saving time

Time.sleep (seconds)
Delay The running of the program, the number of seconds delayed

Time.clock ()
Generally used to measure the time-consuming process

  • win:
    • First call: Returns the current CPU run time as a floating-point number
    • Second call: The floating-point number returns the distance from the last time the function was called to the current interval
  • Linux:
    • Floating-point number returns the current program execution time

Time.asctime (Tupletime)
Returns a time tuple as a readable form string

>>> Time.asctime (Time.localtime ())
' Fri Feb 2 22:26:36 2018 '

Time.strftime (format [, tuple])
Returns a time tuple as a readable string According to the specified format

Time.strptime (string, format)
To return a readable string to a time tuple based on format

format :

  • % Y : Year
  • %m : Month
  • %d : Days
  • % H : Hours
  • % M : minutes
  • % S : Seconds
  • %x : Day/month/year
  • % X : Current time: minute: Seconds
  • % A : Week (full name)
  • % a : Weekday (abbreviation)
>>> time.strftime ('%Y%m%d ', Time.localtime ())
' 2018 02 02 '
>>> time.strptime (' 2018 ','%Y%m%d ')
Time.struct_time (tm_year=2018, tm_mon=2, tm_mday=2, tm_hour=0, tm_min=0, tm_sec=0, Tm_ wday=4, tm_yday=, tm_isdst=-1)
DateTime module

Sub-module description : In the datetime module contains the following three modules for time processing

  • datetime.datetime: processing date, time and seconds
  • datetime.date: processing date
  • datetime.time: minutes and seconds of processing
datetime.datetime.now ()
Datetime.datetime.today ()
Gets the current time , including month , hour, minute , microsecond
return type is datetime.datetime

Datetime.date.today ()
Returns the current time , containing only the date of the month

Datetime.datetime.delta (days=999999999, hours=23, minutes=59, seconds=59, microseconds=999999)
Indicates a time difference

 >>> now = Datetime.datetime.now () 
>>> now
datetime.datetime ( 2018, 2, 10, 17, 12, 18, 220858)
> >> de = Datetime.timedelta (days=30)
>> > de + now
datetime.datetime (2018, 3, 12, 17, 12, 18, 220858)

Some of the functions in the time instance:

  • res = Datetime.datetime.today ()
  • res = Datetime.date.today ()
  • res = Datetime.time (10,20,10)

Some common instance functions are listed below:

  • res.year : Year
  • res.month: Month
  • res.day: Sun
  • res.hour: When
  • res.minute: Min
  • Res.second: sec

Res.timestamp ()
Gets the current time stamp

Res.timetuple ()
Gets the current time tuple

Res.ctime ()
Returns a string date

Res.replace (year, month, day, hour, minute, second)
Returns a new date data after the specified value is replaced

Res.timetuple ()
Return a time tuple

Res.weekday ()
starting from 0 returns the current time is the day of the week
Monday is 0, Sunday is 6

Res.isoweekday ()
Starting from 1 in ISO time standard format Returns the current time is the day of the week
Monday is 1, Sunday is 7

Res.isocalendar ()
ISO represents the format of a time tuple
(year, month, day)

Res.isoformat ()
Returns a string format of ' Yyyy-mm-dd '

To construct your own time

Datetime.datetime (2018, 2, 2, 23, 11, 2, 9999)
The parameter positions are : dd/μs
return value : datetime.datetime Type

Python base = = Time Processing module

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.