Python 使用 time 和 calendar 模塊處理日期和時間。
* `time.time()` 獲取當前時間戳, 自1970年1月1日午夜(歷元)開始
```
import time # 導入time模塊
ticks = time.time()
print("當前時間戳為:", ticks)
```
## 時間元組
Python函數用一個元組裝起來的9組數字處理時間
| 序號 | 屬性 | 描述 | 示例 |
| --- | --- |--- |--- |
| 0 | tm_year | 4位年數 | |
| 1 | tm_mon | 月, 1-12 | |
| 2 | tm_mday | 日 ,1-31 | |
| 3 | tm_hour | 小時 | 0-23 |
| 4 | tm_min | 分鐘 | 0-59 |
| 5 | tm_sec | 秒 | 0-61 , 60,61是閏秒|
| 6 | tm_wday | 周幾 | 0-6 |
| 7 | tm_yday | 一年的第幾天 | 1-366 |
| 8 | tm_isdst | 夏令時 | -1, 0, 1 |
```
# 時間元組
localtime = time.localtime(time.time())
print(localtime)
```
## 格式化時間
1. asctime()
```
localtime = time.asctime( time.localtime(time.time()) ) # Fri Oct 13 16:55:42 2023
print ("本地時間為 :", localtime)
```
2. time 模塊的 strftime 方法來格式化日期
```
print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) # 2023-10-13 16:58:44
print (time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())) # Fri Oct 13 16:58:44 2023
a = "Sat Mar 28 22:24:24 2023"
print (time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y")))
```
## 格式化符號
* `%y` 兩位數年份
* `%Y` 四位數年份
* `%m` 月份 01-12
* `%d` 月中的一天 0-31
* `%H` 24小時小時數 0-23
* `%l` 12小時小時數 01-12
* `%M` 分鐘數 00-59
* `%S` 秒 00-59
* `%a` 本地簡化星期名稱
* `%A` 本地完整星期名稱
* `%b` 本地簡化月份名稱
* `%B` 本地完成月份名稱
* `%c` 本地相應的日期表示
* `%j` 年內的一天 001-366
* `%p` 本地A.M. 或P.M. 的等價符
* `%U` 一年中的星期數(00-53)星期天為星期的開始
* `%w` 星期(0-6),星期天為星期的開始
* `%W` 一年中的星期數(00-53)星期一為星期的開始
* `%x` 本地相應的日期表示
* `%X` 本地相應的時間表示
* `%Z` 當前時區的名稱
* `%%` 百分號本身
## 日歷
Calendar模塊有很廣泛的方法用來處理年歷和月歷,
例如: 打印月的日歷
```
import calendar
cal = calendar.month(2023, 10)
print (cal)
```
## Time模塊
1 time.altzone
返回格林威治西部的夏令時地區的偏移秒數。如果該地區在格林威治東部會返回負值(如西歐,包括英國)。對夏令時啟用地區才能使用。
2 time.asctime([tupletime])
接受時間元組并返回一個可讀的形式為"Tue Dec 11 18:07:14 2008"(2008年12月11日 周二18時07分14秒)的24個字符的字符串。
3 time.clock( )
用以浮點數計算的秒數返回當前的CPU時間。用來衡量不同程序的耗時,比time.time()更有用。
4 time.ctime([secs])
作用相當于asctime(localtime(secs)),未給參數相當于asctime()
5 time.gmtime([secs])
接收時間戳(1970紀元后經過的浮點秒數)并返回格林威治天文時間下的時間元組t。注:t.tm_isdst始終為0
6 time.localtime([secs])
接收時間戳(1970紀元后經過的浮點秒數)并返回當地時間下的時間元組t(t.tm_isdst可取0或1,取決于當地當時是不是夏令時)。
7 time.mktime(tupletime)
接受時間元組并返回時間戳(1970紀元后經過的浮點秒數)。
8 time.sleep(secs)
推遲調用線程的運行,secs指秒數。
9 time.strftime(fmt[,tupletime])
接收以時間元組,并返回以可讀字符串表示的當地時間,格式由fmt決定。
10 time.strptime(str,fmt='%a %b %d %H:%M:%S %Y')
根據fmt的格式把一個時間字符串解析為時間元組。
11 time.time( )
返回當前時間的時間戳(1970紀元后經過的浮點秒數)。
12 time.tzset()
根據環境變量TZ重新初始化時間相關設置。
1 time.timezone
屬性 time.timezone 是當地時區(未啟動夏令時)距離格林威治的偏移秒數(>0,美洲<=0大部分歐洲,亞洲,非洲)。
2 time.tzname
屬性time.tzname包含一對根據情況的不同而不同的字符串,分別是帶夏令時的本地時區名稱,和不帶的。
## 日歷模塊
1 calendar.calendar(year,w=2,l=1,c=6)
返回一個多行字符串格式的year年年歷,3個月一行,間隔距離為c。 每日寬度間隔為w字符。每行長度為21* W+18+2* C。l是每星期行數。
2 calendar.firstweekday( )
返回當前每周起始日期的設置。默認情況下,首次載入 calendar 模塊時返回 0,即星期一。
3 calendar.isleap(year)
是閏年返回 True,否則為 False。
>>> import calendar
>>> print(calendar.isleap(2000))
True
>>> print(calendar.isleap(1900))
False
4 calendar.leapdays(y1,y2)
返回在Y1,Y2兩年之間的閏年總數。
5 calendar.month(year,month,w=2,l=1)
返回一個多行字符串格式的year年month月日歷,兩行標題,一周一行。每日寬度間隔為w字符。每行的長度為7* w+6。l是每星期的行數。
6 calendar.monthcalendar(year,month)
返回一個整數的單層嵌套列表。每個子列表裝載代表一個星期的整數。Year年month月外的日期都設為0;范圍內的日子都由該月第幾日表示,從1開始。
7 calendar.monthrange(year,month)
返回兩個整數。第一個是該月的星期幾的日期碼,第二個是該月的日期碼。日從0(星期一)到6(星期日);月從1到12。
8 calendar.prcal(year,w=2,l=1,c=6)
相當于 print calendar.calendar(year,w=2,l=1,c=6)。
9 calendar.prmonth(year,month,w=2,l=1)
相當于 print calendar.month(year,month,w=2,l=1) 。
10 calendar.setfirstweekday(weekday)
設置每周的起始日期碼。0(星期一)到6(星期日)。
11 calendar.timegm(tupletime)
和time.gmtime相反:接受一個時間元組形式,返回該時刻的時間戳(1970紀元后經過的浮點秒數)。
12 calendar.weekday(year,month,day)
返回給定日期的日期碼。0(星期一)到6(星期日)。月份為 1(一月) 到 12(12月)。
- 前言
- 1.入門篇
- Python介紹
- 安裝與使用
- Python開發利器之VS Code
- 模塊安裝
- 命令行
- 一次Python無法安裝模塊的問題探索與解決之旅
- 命令運行
- Conda
- 下載地址
- 2.基礎篇
- 基礎語法
- 輸入與輸出
- with as的用法
- 注釋
- Python命令行參數
- 編碼
- 變量類型
- 列表遍歷
- 運算符
- 表達式語句
- 條件
- 循環
- 日期和時間
- 函數
- 高級語法
- @符號-裝飾器
- 模塊和包
- name
- init.py
- 錯誤和異常
- 面向對象
- 3.專題篇
- 常用功能
- Python 字符串連接
- python web
- Python CGI編程
- Python OAuth2
- 認證 Flask-HTTPAuth
- 常用命令
- 內置函數
- dir()
- print(f)
- 標準模塊
- sys
- pickle-數據序列化
- os
- IO(輸入輸出)
- 鍵盤輸入
- 文件讀寫
- 測試
- Python測試框架之pytest快速入門
- pytest-bdd快速示例和問題解決
- 基于pytest-bdd的項目目錄結構和命名規范
- python BDD 的相關概念
- Behave介紹和快速示例
- Python BDD之Behave測試報告
- Python BDD 框架比較之 pytest-bdd vs behave
- pytest進階
- Flask + pytest測試
- 參考網址
- pytest-bdd進階
- hehave進階
- 測試路徑
- python + selunium
- HTML 根據多層CSS 查找元素
- 等待執行
- 使用text 查找 span
- pytest如何在控制臺輸出
- 4.問題篇
- pip pip3 及區別
- TypeError: can only concatenate str (not "NoneType") to str
- 5.實戰篇
- matplotlib-繪圖包
- 導入類
- 命名規范
- 模塊查找
- 6.進階篇
- Flask
- Flask介紹
- Flask擴展模塊
- Flask-Login
- 問題
- Jinja2
- Flask-RESTful
- Flask-JWT-Extended
- WSGI
- Flask-SQLAlchemy
- 部署
- Flask VS Django
- Flask Web
- Flask + Vue
- Flask實戰
- Flask 標準目錄結構
- Blueprints
- 參考
- FastAPI 測試
- https 證書 Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate