python1.3-----Time Module/datetime Module/calendar module

Source: Internet
Author: User
Tags local time month name time and date

Time module:

UTC (World Standard Time): Greenwich Astronomical Time. China time and UTC relationship: China time =utc+8
DST (Daylight saving time): It is a kind of energy saving and it is considered as specified, and it is 1 hours faster in summer.

representation of Time:
1. Time Stamp:
a time interval in seconds for a shape or float to represent the time. The base value for this interval is calculated starting from 0:0 on January 1, 1970.
2. Tuples
A Python data structure that has 9 integer content:
Year : ~
month:~
Day : ~
hours:~
minutes:~
seconds:~
Weekday: Day of the Week (0-6)
Julia Day: One of the Year (1-366)
DST Flag (1: Daylight Saving Time-1: Judging by date self 0: normal format)
3. Format the string:
%Y Four-digit year representation (000-9999)
%m Month (01-12)
One day in%d months (0-31)
%H 24-hour system hours (0-23)
%I 12-hour hours (01-12)
%M minutes (00=59)
%s seconds (00-59)
%f MS (000000-999999)
%a Local Simplified week name
%A Local Full week name
%b a locally simplified month name
%B Local Full month name
%y Two-digit year representation (00-99)
%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

Import time

#三种时间格式的转换:
c=time.time () #以浮点数的形式返回当前时间的时间戳, no parameters are required and the timestamp is assigned to C.
T=time.gmtime (c) #以时间戳为参数, converted to UTC time tuple, not much at Greenwich.
B=time.localtime (c) #以时间戳为参数, converted to local time tuples, with few tuples.
M=time.mktime (b) #将本地时间转为时间戳.
S=time.asctime (b) #将时间元组转为字符串.
P=time.ctime (c) #将时间戳转为字符串.
q=time.strftime ('%y-%m-%d%h:%m:%s ', b) #将b的时间转为格式化字符串形式.
w=time.strptime (q, '%y-%m-%d%h:%m:%s ') #将格式化的时间转化为元组.

Conversion of three time formats:

Other methods of the time module:
Time.sleep (value) #延时value秒, value can be a decimal and an integer.
Time.clock () #返回当前程序的cpu执行时间, Windows starts from the second time, always with the first call to this function's timestamp as the cardinality. Unix always returns the full run time.

Example: Test your computer's computational performance
Import time
Sum=0
Time.clock ()
For I in Range (100000000):
Sum+=i
Print (Time.clock ())


#corei5->16.5 seconds

Datatime module:
The Datatime module is encapsulated based on the time module and provides a more practical function, and the interface of the DateTime module is more intuitive and easier to invoke.

Classes in the module:
DateTime: With both time and date
Timedalta: Main calculation time difference
Tzinfo: Time Zone related
Time: Focus on times only
Date: Focus on dates only

Methods for datetime:
D1=datetime.datetime.now () #获取当前的时间.
d2=datetime.datetime (1999,10,1,10,28,25,123456) #指定一个时间: 1999-10-1 10:28:25.123456
d3=d1.strftime ('%y-%m-%d%x ') #将时间转化为 '%y-%m-%d%x ' format string
D4=datetime.datetime.strptime (D3, '%y-%m-%d%x ') #将格式化字符串转换为datetime对象. The format of the conversion is consistent with the string.
d5=datetime.datetime (1999,10,1,10,28,25,123456)
D6=datetime.datetime.now ()
d7=d6-d5 #时间的减法.
type (D7)-------->datetime.timedalta.
d7.days #间隔的天数
d7.seconds #除天数外间隔的秒数.

Calendar module:
Import Calendar
Calendar.month (Year,month) #返回指定某年某月的日历
Calendar.calendar (year) #返回某年的日历
Calendar.isleap (year) #闰年返回True, otherwise returns false
Calendar.monthrange (Year,month) #返回一个元组 (the first day of the year month of weekday, the total number of days this month)
Calendar.monthcalendar (Year,month) #返回year年第month月每一周为元素的列表

python1.3-----Time Module/datetime Module/calendar 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.