Python-基础-日期和时间

Python程序可以通过多种方式来处理日期和时间。

## 什么是刻度？

### 示例

import time;  # This is required to include time module.

ticks = time.time()
print ("Number of ticks since 12:00am, January 1, 1970:", ticks)

python

Number of ticks since 12:00am, January 1, 1970: 1568018748.2947338

## 什么是TimeTuple？

Python时间的函数处理时间9个数字的元组，如下图所示

0 4-digit year 2016
1 Month 1 to 12
2 Day 1 to 31
3 Hour 0 to 23
4 Minute 0 to 59
5 Second 0 to 61 (60 or 61 are leap-seconds)
6 Day of Week 0 to 6 (0 is Monday)
7 Day of year 1 to 366 (Julian day)
8 Daylight savings -1, 0, 1, -1 means library determines DST

### 示例

print (time.localtime())

time.struct_time(tm_year=2019, tm_mon=9, tm_mday=9, tm_hour=16, tm_min=51, tm_sec=9, tm_wday=0, tm_yday=252, tm_isdst=0)

0 tm_year 2016
1 tm_mon 1 to 12
2 tm_mday 1 to 31
3 tm_hour 1 to 23
4 tm_min 1 to 59
5 tm_sec 0 to 61 (60 or 61 are leap-seconds)
6 tm_wday 0 to 6 (0 is Monday)
7 tm_yday 1 to 366 (Julian day)
8 tm_isdst -1, 0, 1, -1 means library determines DST

## 获取当前时间

import time

localtime = time.localtime(time.time())
print ("Local current time :", localtime)

Local current time : time.struct_time(tm_year=2019, tm_mon=9, tm_mday=9, tm_hour=16, tm_min=51, tm_sec=9, tm_wday=0, tm_yday=252, tm_isdst=0)