python3之路 基礎 時間

2021-09-10 02:11:17 字數 1589 閱讀 8355

在敲**過程中,我們難免會涉及到時間的轉換,那麼python3中的時間是如何轉換的呢?

# author gpf

# 功能

import time

# 獲取當前時間

# 返回float,單位為秒

defgetnowtime()

:return time.time(

)# 獲取當前時間

# 返回tuple

defgettuplelocaltime

(seconds=getnowtime())

:return time.localtime(seconds)

# 獲取當前時間

# 返回tuple

defgettupletime

(seconds=getnowtime())

:return time.gmtime(seconds)

# float轉字串

# 返回時間字串

defstimetostr

(seconds=getnowtime())

:return time.strftime(

"%y-%m-%d %h:%m:%s"

, gettuplelocaltime(seconds)

)# 字串轉float

# 返回tuple

defstrtostime

(str

=stimetostr())

:return time.mktime(time.strptime(

str,

"%y-%m-%d %h:%m:%s"))

# 獲取當前時間

# 返回字串

defgetstrtime

(tupletime=gettuplelocaltime())

:return time.asctime(tupletime)

# 獲取當前時間

# 返回字串

defgetstrtime

(seconds=getnowtime())

:return time.ctime(seconds)

print

(strtostime())

print

(stimetostr(

))

這裡邊因為北京在東8區,所以要獲取北京時間要使用localtime();

struct_time中的元素含義:

字段屬性

值4位年數

tm_year

2017

月tm_mon

1到12

日tm_mday

1到31

小時tm_hour

0到23

分鐘tm_min

0到59

秒tm_sec

0到61(60或61是潤秒)

一周的第幾日

tm_wday

0到6(0是周一)

一年的第幾日

tm_yday

1到366,一年中的第幾天

夏令時tm_isdst

是否為夏令時,值為1時是夏令時,值為0時不是夏令時,預設為-1

python中時間日期格式化符號:

Python全棧之路基礎

2.x的預設編碼是assic,預設不支援中文。而3.x的預設編碼是unicode,預設支援中文 3.x不相容2.x,新特性只在3.x上有 3.x的核心語法更加簡單易學 變數的命名規則 要具有描述性 變數名只能 數字,字母組成,不可以是空格或特殊字元 不能以中文為變數名 不能以數字開頭 不能被使用保留...

python自學之路 基礎篇(一)

1 info abc info 2 d 結果是什麼,為什麼會報錯呢?typeerror str object does not support item assignment 字串不支援字元賦值。2 如果要把上面的字串info裡面的c替換成d,要怎麼操作呢?info abc b info.repla...

python自學之路 基礎篇(三)

一 下面列表 a 1,2,3,4,5,333,11,44 輸出下面的結果 4,5,333 print a 3 6 二 有下面2個列表 a 1,2,3 b 4,5,6 用2個方法輸出下面結果 1,2,3,4,5,6 a 1 2,3 b 4 5,6 print a b a.extend b print ...