python3 time and date processing function

time module processing time

Python provides the time module for processing time and formatting time. Unlike Java, Python's timestamp is a floating point number in seconds (distance 1970-1-1 00:00:00)

Get current timestamp
You can pass time.time()Get current timestamp
import  time
ticks = time.time()
print(ticks)
# output: 1516811586.4532115

Get current time tuple
You can use time.localtime() to get the current time tuple. The tuple is used to load the current time information. This time tuple contains the following attributes:
  • tm_year: year (4 digits), such as: 2018;
  • tm_mon: month, range 1 - 12;
  • tm_mday: day of the month, range 1-31;
  • tm_hour: hour, range 0 - 23
  • tm_min: minutes, range 0 - 59
  • tm_sec: seconds , range 0 - 61 (60 and 61 are leap seconds)
  • tm_wday: Day of the week, range 0 - 6 (0 is Monday)
  • tm_yday: Day of the year, range 1-366
  • tm_isdst: Whether it is daylight saving time, the value is: 1 (daylight saving time), 0 (non-daylight saving time), -1 (unknown, default)
import  time
like = team . localtime ()
print ( like )
print("cur year:", curtime.tm_year)
print("cur month:", curtime.tm_mon)
print("cur date:", curtime.tm_mday)
print("cur hour:", curtime.tm_hour)
print("cur min:", curtime.tm_min)
print ( "why sec" , curtime . tm_sec )
# output:
# time.struct_time(tm_year=2018, tm_mon=1, tm_mday=25, tm_hour=0, tm_min=42, tm_sec=40, tm_wday=3, tm_yday=25, tm_isdst=0)
# cur year: 2018
# cur month: 1
# cur date: 25
# cur hour: 0
# cur min: 42
# cur sec: 40

format time
For the formatting of time tuples, you can use the time.asctime() or time.strptime() function to obtain the formatted output of the time tuple, and use time.mktime() to create a time tuple object from the formatted time string;
The time formatting notation in python is as follows:
%AND  Four-digit year representation (000-9999) %and Two-digit year representation (00-99)
%m Month (01-12)
%d day of the month (0-31)
%H 24-hour clock hours (0-23)
%I 12-hour clock hours (01-12)
%M
minutes (00-59)
%S
 Seconds (00-59)
%p
Localized AM, PM
%w
Day of the week (0-6, 0 is Sunday)
%a
Local simplified week name, eg: Mon
%A
Local full week name, eg: Monday
%b
Local simplified month name, eg: Jan
%B
Local full month name, eg: January
%j
day of the year (001-366)
% U
Week of the year (00-53), starting on Sunday
%c
Local corresponding date representation and time representation;
equivalent to: %a %b %d %H:%M%S %Y

such as: Thu Jan 25 01:22:04 2018
%WITH
The name of the current time zone
%x
Local corresponding date representation;
equivalent to: %d/%m/%y
such as: 01/25/18
%X
本地相应的时间表示;
等同于:%H:%M%S
如:01:32:46
以下为示例使用:
# 快速格式化时间输出
curtime_str = time.asctime(time.localtime())
print(curtime_str)
# 输出:Thu Jan 25 00:42:40 2018
# 时间对象转化为格式化字符串
curtime_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(curtime_str)
# 输出:2018-01-25 00:55:12
# 格式化字符串转化为时间对象
curtime = time.mktime(time.strptime("2017-12-12", "%Y-%m-%d"))
print(curtime)
# 输出:1513008000.0

计算某段程序的运行时间
import time, random
start_tricks = time.clock()  # 记录开始CPU时间戳
# 测试代码
mylist = list()
for i in range(0, 10000000):
    mylist.append(random.randrange(0, 1000, 10))
del mylist
end_tricks = time.clock()              # 记录结束CPU时间戳
run_time = end_tricks - start_tricks   # 计算运行时间
print("code running time:", run_time, "seconds")
# 输出: code running time: 12.014192567369587 seconds


time 模块常用方法列表
线程相关
time.sleep(secs)
线程挂起一段时间,secs指秒数;
时间操作相关
time.time( )
返回当前时间的时间戳(1970纪元后经过的浮点秒数)。
time.clock()
用以浮点数计算的秒数返回当前的CPU时间。用来衡量不同程序的耗时,比time.time()更有用
time.altzone()
返回格林威治西部的夏令时地区的偏移秒数
time.gmtime([secs])
接收时间辍(1970纪元后经过的浮点秒数)并返回格林威治天文时间下的时间元组
time.localtime([secs]
接收时间辍(1970纪元后经过的浮点秒数)并返回当地时间下的时间元组
time.asctime([tupletime])
接受时间元组并返回一个可读的形式为"Tue Dec 11 18:07:14 2008" 的字符串。
time.ctime([secs])
相当于asctime(localtime(secs))
time.mktime(tupletime)
接受时间元组并返回时间辍(1970纪元后经过的浮点秒数)。
time.strftime(fmt[,tupletime])
接收以时间元组,并返回以可读字符串表示的当地时间,格式由fmt决定。
time.strptime(str,fmt='%a %b %d %H:%M:%S %Y')
根据fmt的格式把一个时间字符串解析为时间元组。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324521030&siteId=291194637