Python內建模組 time calendar

2022-05-02 07:45:10 字數 3303 閱讀 7229

#

時間模組

import

time

#日曆模組

import

calendar

#時間戳

ticks =time.time()

#本地時間[元組]

localtime =time.localtime(time.time())

#格式化日期

formattime = time.strftime("

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

",time.localtime())#日曆

cal = calendar.month(2020,6)

print(cal)

#

!/usr/bin/python

#-*- coding: utf-8 -*-

#時間模組

import

time

#格林威治西部的夏令時地區的偏移(小時)

timezone = time.altzone/3600

#距離格林威治的偏移秒數(小時)

zone = time.timezone/3600#亂碼

tzname =time.tzname

#時間戳

time_m =time.time()

#返回cpu時間

cpu_time =time.clock()

#元組型別的時間

localtime =time.localtime()

gmtime =time.gmtime()

#將元組時間轉換為可讀格式

time_asc =time.asctime(localtime)

#作用相當於asctime(localtime(secs)),未給引數相當於asctime()

ctime =time.ctime()

#時間轉換為時間戳

mktime =time.mktime(gmtime)#睡眠

time.sleep(1)

#時間元組格式化

time_strf = time.strftime("

%y-%m

",gmtime)

#時間字串轉換為元組

time_strp = time.strptime(time_asc,"

%a %b %d %h:%m:%s %y")

#根據環境變數tz重新初始化時間相關設定。

#time.tzset()

print(time_asc)

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

import

calendar

#設定每週的起始日期碼

calendar.setfirstweekday(6)

#單個月日曆

cal = calendar.month(2016,1,w=2,l=1)

#全年的日曆

all_year = calendar.calendar(2019,w=2,l=1,c=6)

#一周的第一天是星期幾

firstweek =calendar.firstweekday()

#判斷閏年還是平年

leap_year = calendar.isleap(2020)

#返回兩個年份之間的閏年總數

leap_number = calendar.leapdays(2019,2020)

#返回乙個多行字串格式的year年month月日曆,兩行標題,一周一行

split_month = calendar.monthcalendar(2019,1)

#返回月的範圍值(元組)

month_range = calendar.monthrange(2019,4)

#列印全年的日曆

print_all_year = calendar.prcal(2020)

#列印月份

print_cal = calendar.prmonth(2019,12)

#和time.gmtime相反:接受乙個時間元組形式,返回該時刻的時間戳(1970紀元後經過的浮點秒數)。

#calendar.timegm(tupletime)

#返回給定日期的星期數

weekday = calendar.weekday(2020,6,14)

#查詢系統

import time

import calendar

flag =true

ticks =time.time()

localtime =time.localtime(ticks)

formattime = time.strftime("

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

",localtime)

alert = """

請輸入對應的查詢專案:

【1】查詢今天的日期和時間

【2】查詢某年的屬性和星期數

【3】列印某年的日曆

【4】停止

"""while

flag:

print(alert)

value = eval(input("

請輸入:"))

if value==2 or value==3

: year = eval(input("

請輸入年份:"))

month = eval(input("

請輸入月份:"))

day = eval(input("

請輸入具體的天:"))

if value==1

: print(formattime)

elif value==2

:

weekday = calendar.weekday(year,month,day)+1

leap_year =calendar.isleap(year)

print(

"***************=星期

"+str(weekday))

ifleap_year:

print(

"**********===閏年")

else

: print(

"**********===平年")

elif value==3

: print(calendar.month(year,month))

elif value==4

: flag =false

else

: print(

"輸入錯誤請重新輸入!")

python內建模組 Python 內建模組

內建模組 python有一套很有用的標準庫 standard library 標準庫會隨著python直譯器,一起安裝在你的電腦中的。它是python的 乙個組成部分。這些標準庫是python為你準備好的利器,可以讓程式設計事半功倍。常用標準庫 標準庫 說明 builtins 內建函式預設載入 os...

Python內建模組

os.remove 刪除檔案 os.unlink 刪除檔案 os.rename 重新命名檔案 os.listdir 列出指定目錄下所有檔案 os.curdir 返回當前目錄 os.pardir 獲取當前目錄的父目錄字串名 os.chdir 改變當前工作目錄 os.getcwd 獲取當前檔案路徑 os...

python內建模組

time模組 時間戳time.time 列印出的是秒數,從1970年開始算 time.localtime 顯示的是結構化時間,當地時間 time.gmtime 顯示的也是結構化時間,不過是世界標準時間 將結構化時間轉換成時間戳,time.mktime time.localtime 將結構化時間轉換成...