Python processing time-related methods

Source: Internet
Author: User
Tags days in month local time string format

A module that records Python processing time: A timing module, a datetime module, and a calendar module.

Python version: 2.7

50564504

Before introducing the module, let's say the following points:

1. Time is usually expressed in these ways:

A. Timestamp: The offset is usually calculated in seconds from 00:00:00 January 1, 1970, such as: 1488520142.802

B. Formatted time string: Output the time as a string as specified by the formatted character, as specified by the format characters: '%y-%m-%d%h:%m:%s ', the resulting time string is: 2017-03-03 13:51:34. The formatted character is shown in Figure 1.

C. In the form of a tuple, a total of 9 elements. The specific elements are shown in Figure 2.

2. Universal Coordination time UTC (Universal time coordinated), like Greenwich Mean Time GMT (Greenwich Mean time), is the same as in London, UK, and is world standard. China for Utc+8

One, Time module

The main functions include:

Time ()--Returns the current timestamp, the form of a floating-point number, and does not accept parameters.

Gmtime ()--Converts the timestamp to UTC time, tuple form, accepts a float timestamp parameter, and the default value is the current timestamp.

LocalTime ()--Converts the timestamp to local time, tuple form, accepts a float timestamp parameter, and the default value is the current timestamp.

CTime ()--Converts the timestamp to the specified string form, accepts a float timestamp parameter, and the default value is the current timestamp.

Actime ()--Converts the time tuple format to the specified string form, accepts a tuple parameter, and the default value is the LocalTime () return value.

Mktime ()--Converts a local time tuple to a timestamp and accepts a tuple parameter, required.

Strftime ()--Converts a time tuple to a string in the specified format, accepts a formatted string, a time tuple, and optionally a time tuple parameter, which defaults to LocalTime ().

Strptime ()--resolves a time string of the specified format to a time tuple, accepting a format string, a string of time, and two parameters are required.

Sleep ()--delay the specified time, accept integer, float, in seconds.

Clock ()-on UNIX systems, returns the process time, timestamp in seconds, on the Windows system, the first call returns the actual time the process is running, and the second subsequent call is the run time from the first call to the present.

Tzset ()--Change the local time zone. (the function was tested for error: The module does not contain this method.) The specific reason has not been scrutiny. )

(This picture is from the network, the original author has nowhere to be found)

Commonly used for:

Gets the current time of the formatted string:

To convert a formatted string to a timestamp:

Second, the DateTime module

In addition to getting the time, the module is very convenient for time comparison operations. directly on the code.

Datetime.date.today ()

Datetime.datetime.now ()

Third, calendar module

Calendar is a class of calendars that defines the date and day of the week for one months or years, and also shows the format output of the text calendar and the HTML calendar. [Python]View PlainCopy
  1. Import Calendar
  2. c = Calendar. Textcalendar (Calendar. SUNDAY)
  3. C.prmonth ( 1)
  4. January
  5. Su Mo Tu We Th Fr Sa
  6. 1 2
  7. 3 4 5 6 7 8 9
  8. Ten to One
  9. ( + )
  10. From $
  11. 31
function and Description 1. Calendar.calendar (year, w=2, L=1, c=6, m=3)

Returns the year calendar of a multi-line string format.

2. Calendar.firstweekday ()

Returns the settings for the current weekly start date. Default returns 0, which is Monday.

3. Calendar.isleap (year)

A leap year returns true, otherwise false.

4. Calendar.leapdays (y1, y2)

Returns the total number of leap years between Y1 and Y2, including Y1, which does not contain y2.

5. Calendar.month (year, month, w=2, l=1)

Returns a multi-line string format for year month calendar.

6. Calendar.monthcalendar (Year,month)

Returns a single-level nested list of integers. Each sub-list is loaded for one weeks. The date outside that month is 0, and the date within that month is set to the date of the day, starting from 1.

7. Calendar.monthrange (year, month)

Returns a tuple of two integers, the first of which is the day of the week, and the second is the number of days of the month. (Calendar.monthrange (year, month):
Returns weekday of the month and number of days in month, for the specified year and Month.--python document)
PS: Calculate the day of the week here according to Monday for 0.

8. Calendar.prcal (year, w=2, L=1, c=6)

equals print Calendar.calendar (year)

9. Calendar.prmonth (year, month)

Ditto.

Calendar.setfirstweekday (Weekday)

Set the weekly start date code. 0 (Monday) to 6 (Sunday)

CALENDAR.TIMEGM (Tupletime)

In contrast to Time.gmtime, receives a time tuple that returns the timestamp of that moment (the number of seconds after the computer's previous date)

Calendar.weekday (year, month, day)

Returns the weekday code for the given day, 0 (Monday) through 6 (Sunday).

Python processing time-related methods

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.