Python Time,calendar Module Basic instructions

Source: Internet
Author: User
Tags local time month name readable string format

1 time stamp : GMT January 01, 1970 00 minutes 00 seconds (Beijing time January 01, 1970 08:00 00 seconds) up to now the total number of seconds.

The usual way to get time in Python is to get a timestamp before converting it to the desired time format.

2 tuples struct_time: Date, Time is a number of variables, so a tuple is defined in Python struct_time all these variables together, including: 4-digit years, months, days, hours, minutes, seconds, and so on.

All variables and requirements are as follows:

Corresponding, the attributes of the Struct_time tuple are as follows:

Many Python functions use a single element to assemble 9 sets of digital processing time:

import time;  # Introducing the time module  = time.time ()print (" current timestamp is:"1459996086.7115328
Get current time

Converts from the time of the return floating-point number to the time tuple, as long as the floating-point number is passed to a function such as localtime.

Import= time.localtime (time.time ())print (" local time:" , localtime) output local time: Time.struct_time (tm_year=2016, tm_mon=4, tm_mday=7, tm_hour=10, tm_min=28, tm_sec=49, Tm_wday=3, tm_yday=98, tm_isdst=0)

Get the formatted time

You can choose a variety of formats according to your needs, but the simplest function to get a readable time pattern is asctime ():

Import= time.asctime (time.localtime (Time.time ()))print (" local time:  ", localtime) local time: Thu Apr  7 10:29:13 2016
Formatted date

We can use the Strftime method of the time module to format the date:

Import Time#formatted as 2016-03-20 11:45:39 in the form ofPrint(Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ()))#formatted as Sat 28 22:24:24 2016 FormPrint(Time.strftime ("%a%b%d%h:%m:%s%Y", Time.localtime ())) #convert a format string to a timestampA ="Sat Mar 22:24:24"Print(Time.mktime (Time.strptime (A,"%a%b%d%h:%m:%s%Y")), the result is2016-04-07 10:29:46Thu APR07 10:29:46 20161459175064.0

Python format symbols in time Date:

    • %y Two-digit year representation (00-99)
    • %Y Four-digit year representation (000-9999)
    • %m Month (01-12)
    • One day in%d months (0-31)
    • %H 24-hour hours (0-23)
    • %I 12-hour hours (01-12)
    • %M minutes (00=59)
    • %s seconds (00-59)
    • %a Local Simplified Week name
    • %A Local Full week name
    • %b a locally simplified month name
    • %B Local Full month name
    • %c Local corresponding date representation and time representation
    • %j Day of the Year (001-366)
    • %p the equivalent of a local a.m. or p.m.
    • %u weeks of the year (00-53) Sunday is the beginning of the week
    • %w Week (0-6), Sunday for the beginning of the week
    • %W Week of the Year (00-53) Monday is the beginning of the week
    • %x Local corresponding date representation
    • %x Local corresponding time representation
    • %Z the name of the current time zone
    • Percent% of the number itself
Get a calendar for a month

The Calendar module has a wide range of methods for working with calendar and monthly calendars, such as printing a month's monthly calendar:

Import= calendar.month (1)print ("The following output January 2016 calendar:"  )print  (CAL) output for the following output January 2016 calendar:    Mo Tu We Th Fr Sa Su             1  2  3 4  5  6  7  8  9 1011 12 13 14 15 16 1718 19 20 21 22 23 2425 26 27 28 29 30 31
Time Module

The time module contains the following built-in functions, both temporal and conversion time formats:

Not commonly used

Time.altzone the number of offset seconds to return to the daylight saving time region in West Greenwich. If the region returns negative values in eastern Greenwich (such as Western Europe, including the UK). Available for daylight saving time enabled regions. >>>Import Time>>>Print("Time.altzone%d"%time.altzone) Time.altzone-28800time.asctime ([Tupletime]) accepts a time tuple and returns a readable form of"Tue Dec 18:07:14"(December 11, 2008 Tuesday 18:07 14 seconds) The 24-character string. >>>Import Time>>> T =time.localtime ()>>>Print("Time.asctime (t):%s"%time.asctime (t)) Time.asctime (t): Thu APR7 10:36:20 2016Time.clock () returns the current CPU time for the number of seconds that are calculated as floating-point numbers. It is more useful than time.time () to measure the time spent on different programs. Import Timedefprocedure (): Time.sleep (2.5)#Time.clockT0 =Time.clock () procedure ( )Print(Time.clock ()-t0)#Time.timeT0 =time.time () procedure ( )Print(Time.time ()-t0) results are5.000000000000143e-052.5020556449890137time.gmtime ([secs]) receives the time suffix (the number of floating-point seconds elapsed after the 1970 era) and returns the time-tuple T in Greenwich Astronomical time. Note: T.TM_ISDST is always 0>>>Import Time>>>Print("gmtime:", Time.gmtime (1455508609.34375)) Gmtime:time.struct_time (Tm_year=2016, tm_mon=2, tm_mday=15, tm_hour=3, tm_min=56, tm_sec=49, tm_wday=0, tm_yday=46, tm_isdst=0) Time.localtime ([secs] Receive time (the number of floating-point seconds elapsed after the 1970 era) and return the time tuple T under local time (T.TM_ISDST desirable 0 or 1, depending on whether the local daylight saving time). >>Import Time>>>Print("localtime ():", Time.localtime (1455508609.34375)) localtime (): Time.struct_time (Tm_year=2016, tm_mon=2, tm_mday=15, tm_hour=11, tm_min=56, tm_sec=49, tm_wday=0, tm_yday=46, tm_isdst=0) Time.mktime (tupletime) accepts the time tuple and returns the time suffix (the number of floating-point seconds elapsed after the 1970 era). ImportTimet= (2016, 2, 17, 17, 3, 38, 1, 48, 0) secs=time.mktime (t)Print("Time.mktime (t):%f"%secs)Print("asctime (localtime (secs)):%s"%Time.asctime (Time.localtime (secs))) results for Time.mktime (t):1455699818.000000asctime (localtime (secs)): Wed Feb17 17:03:38 2016time.sleep (secs) postpones the call thread's run, secs refers to the number of seconds. Import TimePrint("Start:%s"%time.ctime ()) Time.sleep (5 )Print("End:%s"%time.ctime ()) Time.strftime (Fmt[,tupletime]) receives a time tuple and returns the local time as a readable string, in the format determined by FMT. >>>Import Time>>>Print(Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ()))2016-04-07 11:18:05Time.strptime (str,fmt='%a%b%d%h:%m:%s%Y'resolves a time string to a time tuple according to the format of the FMT. >>>Import Time>>> struct_time = Time.strptime ("From the Nov","%d%b%y")>>>Print("returns a tuple:", Struct_time) returns the tuple: Time.struct_time (tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, Tm_isdst=-1) Time.time () returns the timestamp of the current time (the number of floating-point seconds elapsed after the 1970 era). >>>Import Time>>>Print(Time.time ())1459999336.1963577Time.tzset () re-initializes time-related settings according to the environment variable TZ. Import TimeImportOS os.environ['TZ'] ='est+05edt,m4.1.0,m10.5.0'Time.tzset ()Print(Time.strftime ('%x%x%Z')) os.environ['TZ'] ='aest-10aedt-11,m10.5.0,m3.5.0'Time.tzset ()Print(Time.strftime ('%x%x%Z'))23:25:45 04/06/16EDT13:25:45 04/07/16 AEST
View CodeCalendar Module

The functions of this module are calendar-related, such as printing a month's character calendar.

Monday is the default first day of the week, and Sunday is the default last day. You need to call the Calendar.setfirstweekday () function to change the settings. The module contains the following built-in functions:

1 Calendar.calendar (year,w=2,l=1,c=6returns a multi-line string format for the year calendar, a 3-month line with a distance of C. The daily width interval is w characters. Each line length is* w+18+2*C. L is the number of rows per week. 2Calendar.firstweekday () returns the current weekly start date setting. By default, 0 is returned when the Caendar module is first loaded, which is Monday. 3Calendar.isleap (year) is a leap year that returns true, otherwise false. 4calendar.leapdays (y1,y2) returns the total number of leap years between y1,y2 two. 5 Calendar.month (year,month,w=2,l=1returns a multi-line string format for year month calendar, two row headings, and one week row. The daily width interval is w characters. The length of each line is 7* W+6. L is the number of rows per week. 6Calendar.monthcalendar (year,month) returns a single-level nested list of integers. Each sub-list is loaded with integers representing one weeks. The date of year month is set to 0, and the day of the month is indicated by the day of the week, starting from 1. 7Calendar.monthrange (year,month) returns two integers. The first is the day of the week of the month, and the second is the date code for that month. Day from 0 (Monday) to 6 (Sunday); The month is from 1 to 12. 8 Calendar.prcal (year,w=2,l=1,c=6) is equivalent toPrintCalendar.calendar (year,w,l,c).9 Calendar.prmonth (year,month,w=2,l=1) is equivalent toPrintCalendar.calendar (year,w,l,c). 10Calendar.setfirstweekday (Weekday) sets the weekly start date code. 0 (Monday) to 6 (Sunday). 11Calendar.timegm (tupletime) and time.gmtime instead: accept a time tuple form, returning the time of the moment (the number of floating-point seconds elapsed after the 1970 era). 12calendar.weekday (Year,month,day) returns the date code for the given date. 0 (Monday) to 6 (Sunday). Month is1 (January) to 12 (December).
View Code

Python Time,calendar Module Basic instructions

Related Article

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.