Example usage of time module and datetime module in Python

Time module method:
time.time (): get the timestamp of the current time, time.localtime (): accept a timestamp and convert it into a tuple of the current time. If no parameter is given, time.time () will be passed in as a parameter by default.

time.localtime():
index Attributes meaning
0 tm_year year
1 tm_mon month
2 tm_mday day
3 tm_hour Time
4 tm_min Minute
5 tm_sec second
6 tm_wday Day of the week
7 tm_yday Day of the year
8 tm_isdst summer time
  • time.mktime (): Contrary to time.localtime (), it converts a time tuple into a timestamp (this must give a parameter)
  • time.asctime (): Represent a time tuple as: "Sun Jul 28 03:35:26 2013" This format, if no parameters are given, time.localtime () will be passed in as a parameter by default
  • time.ctime (): convert a timestamp to the expression format of time.asctime (), if no parameter is given, time.time () will be passed in as a parameter by default
  • time.gmtime (): Convert a timestamp to a time tuple of UTC + 0 time zone (China should be +8 time zone, a difference of 8 hours). If no parameter is given, time.time () will be passed by default Enter
  • time.strftime (format, time.localtime ()): convert a time tuple to a formatted time character, if no time tuple parameter is given, time.localtime () will be passed in as a parameter by default

For example, the time format in the web log is time.strftime ('% d /% b /% Y:% X').

Return results:

Sun Jul 28 04:37:38 2013

format:

 

Attributes format meaning Value range (format)
years %and Year without century 00-99
%AND Full year  
%j Day of the year 001-366
month %m month January 12
%b Local simplified month name Abbreviated English month
%B The name of the local full month Full English month
date %d Day of the month January 31
hour %H The hour of the day (24-hour clock) 00-23
%l The first few hours (12-hour system) “01-12”
minute %M Minutes 00-59
second %S second 00-59
week % U Week of the year (counting from Sunday) 00-53
%W Week of the year (starting on Monday)  
%w Day of the week 0-6
Time zone %FROM China: It should be GMT + 8 (China Standard Time) Pray for literacy
other %x Local corresponding date Day / month / year
%X Local printing time Minutes and seconds
%c Detailed date and time Day / Month / Year Hour: Minute: Second
%% '%'character '%'character
%p Correspondence of local am or pm AM    or    PM

time.strptime (stringtime, format): Convert the time string to the time in the form of an array according to the specified formatter,
for example:

 

time.strptime('28/Jul/2013:04:33:29', '%d/%b/%Y:%X')

Return results:

time.struct_time(tm_year=2013, tm_mon=7, tm_mday=28, tm_hour=4, tm_min=33, tm_sec=29, tm_wday=6, tm_yday=209, tm_isdst=-1)

 

time.clock (): returns the processor clock time, generally used for performance testing and benchmark testing, etc., because they reflect the actual time used by the program, which is not commonly used.

time.clock (): returns the processor clock time, generally used for performance testing and benchmark testing, etc., because they reflect the actual time used by the program, which is not commonly used.

time.sleep (): postpone the specified time to run, in seconds

import time
print time.time() #打印时间戳
print time.localtime()#打印本地时间元组
print time.gmtime()#答应UTC+0时区的时间元组
print time.ctime()#打印asctime格式化时间
print time.mktime(time.localtime())#将时间元组转换为时间戳
print time.asctime()#打印格式化时间
print time.strftime('%d/%b/%Y:%X')#打印指定格式的时间格式
#把时间字符串和它的格式翻译成时间元组
print time.strptime('28/Jul/2013:04:33:29', '%d/%b/%Y:%X')
print '%0.5f'%time.clock() #打印处理器时间
for i in range(100000): 
 pass
print '%0.5f'%time.clock()#打印处理器时间

Output:
----------------------------------------------------------------------------
1364028568.55
time.struct_time(tm_year=2013, tm_mon=3, tm_mday=23, tm_hour=4, tm_min=49, tm_sec=28, tm_wday=5, tm_yday=82, tm_isdst=1)
time.struct_time(tm_year=2013, tm_mon=3, tm_mday=23, tm_hour=8, tm_min=49, tm_sec=28, tm_wday=5, tm_yday=82, tm_isdst=0)
Sat Mar 23 04:49:28 2013
1364028568.0
Sat Mar 23 04:49:28 2013
23/Mar/2013:04:49:28
time.struct_time(tm_year=2013, tm_mon=7, tm_mday=28, tm_hour=4, tm_min=33, tm_sec=29, tm_wday=6, tm_yday=209, tm_isdst=-1)
0.02000
0.03000
----------------------------------------------------------------------------

datetime module
datetime.time (): Generate a time object. This time can be set by us, the default is 0 (this class is only for time)

#coding:utf-8
import datetime
print datetime.time()
t = datetime.time(1, 3, 5, 25)
print t
print t.hour #时
print t.minute #分
print t.second #秒
print t.microsecond #毫秒
print datetime.time.max #一天的结束时间
print datetime.time.min #一天的开始时间

Output:
------------------
00:00:00
01:03:05.000025

23:59:59.999999
00:00:00
------------------

datetime.date (): Generate a date object. This date should be set by us, (this class is only for dates)

#coding:utf-8
import datetime
#设置日期
t = datetime.date(2013, 2, 3)
#打印设置日期的和元组
print t.timetuple()#日期元组
print t
print t.year #年
print t.month #月
print t.day #日
#获取今天的日期
today = datetime.date.today()
print today
print datetime.datetime.now()#这个打印到毫秒级别
#获取今天日期的元组
t1 = today.timetuple()
print t1
#打印成ctime格式(time.ctime()格式)
#'%a %b %d %H:%M:%S %Y'
print t.ctime()
print today.ctime()

Output:
------------------------------------------------------------------------------------------------------------------------
time.struct_time(tm_year=2013, tm_mon=2, tm_mday=3, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=34, tm_isdst=-1)
2013-02-03
2013
2
3
2013-07-28
2013-07-28 20:13:25.942000
time.struct_time(tm_year=2013, tm_mon=7, tm_mday=28, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=209, tm_isdst=-1)
Sun Feb 3 00:00:00 2013
Sun Jul 28 00:00:00 2013
------------------------------------------------------------------------------------------------------------------------

datetime.timedelta (): This class is used to do arithmetic operations on time
datetime.datetime.combine (date, time): This is used to combine date and time

#coding:utf-8
import datetime
#打印:从毫秒到周的表示格式 = 转换成秒 (total_seconds()) 
for i in [datetime.timedelta(milliseconds=1), #1毫秒
  datetime.timedelta(seconds=1), #1秒
  datetime.timedelta(minutes=1), #1分钟
  datetime.timedelta(hours=1), #1小时
  datetime.timedelta(days=1), #1天
  datetime.timedelta(weeks=1)]:#11周
 #print i + ':' + i.total_seconds()
 print '%s = %s seconds'%(i,i.total_seconds())
print
print '~' * 20 + '我是分割线' + '~' * 20
print '计算时间的加减。。。。。。。。。'
a = datetime.datetime.now()
print '现在时间是:'
print a
print '加5小时之后变成:'
b = a + datetime.timedelta(hours=5)
print b
print '加一周之后变成:'
c = a + datetime.timedelta(weeks=1)
print c
print '减去一周后变成:'
d = a - datetime.timedelta(weeks=1)
print d
print '计算2个时间相差多久'
print '%s减去%s'%(b, a)
print '等于:%s'%(b - a)
print '%s减去%s'%(a, d)
print '等于:%s'%(a - d)
print
print '~' * 20 + '我是分割线' + '~' * 20
print '比较2个时间:'
print '比较当天和一周前的'
print a > d
print '如果比较d > a 的话就返回False'
print
print '~' * 20 + '我是分割线' + '~' * 20
print '上面的列子都是把日期和时间分开的,现在我们来把他们自由结合'
print '假设我们想要的时间是:2014-01-05 13:14:25'
t = datetime.time(13, 14, 25)
d = datetime.date(2014, 01, 05)
print datetime.datetime.combine(d, t)


Output:
-----------------------------------------------------------------------------------------
打印为:
0:00:00.001000 = 0.001 seconds
0:00:01 = 1.0 seconds
0:01:00 = 60.0 seconds
1:00:00 = 3600.0 seconds
1 day, 0:00:00 = 86400.0 seconds
7 days, 0:00:00 = 604800.0 seconds

计算时间的加减。。。。。。。。。
现在时间是:
2013-07-28 21:34:33.531000
加5小时之后变成:
2013-07-29 02:34:33.531000
加一周之后变成:
2013-08-04 21:34:33.531000
减去一周后变成:
2013-07-21 21:34:33.531000
计算2个时间相差多久
2013-07-29 02:34:33.531000减去2013-07-28 21:34:33.531000
等于:5:00:00
2013-07-28 21:34:33.531000减去2013-07-21 21:34:33.531000
等于:7 days, 0:00:00

比较2个时间:
比较当天和一周前的
True
如果比较d > a 的话就返回False

上面的列子都是把日期和时间分开的,现在我们来把他们自由结合
假设我们想要的时间是:2014-01-05 13:14:25
2014-01-05 13:14:25
-----------------------------------------------------------------------------------------

 

 

 

 

 

 

 

 

 

Published 943 original articles · Like 136 · Visit 330,000+

Guess you like

Origin blog.csdn.net/weixin_36670529/article/details/105001502