time模組中時間表現的格式主要有三種:
a、timestamp時間戳,時間戳表示的是從2023年1月1日00:00
:00開始按秒計算的偏移量
b、struct_time時間元組,共有九個元素組。
c、format time 格式化時間,已格式化的結構使時間更具可讀性。包括自定義格式和固定格式。
2、主要time生成方法和time格式轉換方法例項:
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "tkq"
import time
# 生成timestamp
time.time(
)# 1477471508.05
#struct_time to timestamp
time.mktime(time.localtime())
#生成struct_time
# timestamp to struct_time 本地時間
time.localtime(
)time.localtime(time.time())
# time.struct_time(tm_year=2016, tm_mon=10, tm_mday=26, tm_hour=16, tm_min=45, tm_sec=8, tm_wday=2, tm_yday=300, tm_isdst=0)
# timestamp to struct_time 格林威治時間
time.gmtime(
)time.gmtime(time.time())
# time.struct_time(tm_year=2016, tm_mon=10, tm_mday=26, tm_hour=8, tm_min=45, tm_sec=8, tm_wday=2, tm_yday=300, tm_isdst=0)
#format_time to struct_time
time.strptime(
'2011-05-05 16:37:06'
,'%y-%m-%d %x'
)# time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6, tm_wday=3, tm_yday=125, tm_isdst=-1)
#生成format_time
#struct_time to format_time
time.strftime(
"%y-%m-%d %x"
)time.strftime(
"%y-%m-%d %x"
,time.localtime())
# 2016-10-26 16:48:41
#生成固定格式的時間表示格式
python常用模組 time
在python中,與時間處理有關的模組包括 time datetime以及calendar。這篇主要講解time。utc時間協調時即格林威治天文時間,世界標準時間。中國為utc 8。dst即夏令時。1.1 時間戳 通常來說,時間戳表示的是從1970年1月1日00 00 00開始按秒計算的偏移量,返回...
Python常用模組之time模組
python中的time和datetime模組是時間方面的模組 time模組中時間表現的格式主要有三種 1 timestamp 時間戳,時間戳表示的是從1970年1月1日00 00 00開始按秒計算的偏移量 2 struct time 時間元組,共有九個元素組。3 format time 格式化時間...
常用模組 time
import time 時間戳 一串數字 從unix元年,每過一秒 1 格式化好的時間 2020 08 29 17 24 38 20200829172438 print 20200829 28 print int time.time 當前的時間戳 print time.strftime y m d ...