日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python什么时候用框架_python时间模块的使用

發布時間:2025/4/5 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python什么时候用框架_python时间模块的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言:

在開發中經常會與時間打交道,如:獲取事件戳,時間戳的格式化等,這里簡要記錄一下python操作時間的方法。

python中常見的處理時間的模塊:

time:處理時間的模塊,如獲取時間戳,格式化日期等

datetime:date和time的結合體,處理日期和時間

calendar:日歷相關的模塊,如:處理年歷/月歷

time模塊介紹

說明:time模塊主要講解如下內容:

1.時間戳 –> 時間元組格式(time.struct_time) –> 日期字符串

2.日期字符串 –> 時間元組格式(time.struct_time) –> 時間戳

3.獲取當前時間的分鐘/秒

4.獲取整分鐘/整小時時間戳

1.時間戳 –> 時間元組格式(time.struct_time) –> 日期字符串

時間戳 –> 時間元組格式

time.localtime(timestamp) # 參數timestamp為秒級時間戳

例子:

import time

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

print time_tuple # time.struct_time(tm_year=2019, tm_mon=1, tm_mday=30, tm_hour=11, tm_min=29, tm_sec=33, tm_wday=2, tm_yday=30, tm_isdst=0)

時間元組 –> 日期字符串

time.strftime(format, p_tuple=None):format:格式化的日期樣式;p_tuple:時間元組

例子:

time_format = time.strftime("%Y-%m-%d %H:%M:%S", time_tuple)

print time_format # 2019-01-30 11:48:07

封裝成方法:

def timestamp_format(timestamp):

"""

:brief 時間戳格式化

:param timestamp: 時間戳

:return: 格式化后的日期

"""

return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp))

2.日期字符串 –> 時間元組格式(time.struct_time) –> 時間戳

日期字符串 –> 時間元組

time.strptime(string, format) # string:日期字符串,format:該日期字符串對應的格式化格式

例子:

import time

time_str_to_tuple = time.strptime("2019-01-30 11:48:07", "%Y-%m-%d %H:%M:%S")

print time_str_to_tuple # time.struct_time(tm_year=2019, tm_mon=1, tm_mday=30, tm_hour=11, tm_min=48, tm_sec=7, tm_wday=2, tm_yday=30, tm_isdst=-1)

時間元組 –> 時間戳

time.mktime(p_tuple):p_tuple:時間元組

例子:

time_tuple_to_timestamp = int(time.mktime(time_str_to_tuple))

print time_tuple_to_timestamp # 結果:1548820087

封裝成方法

def time_str_to_timestamp(date_str, format):

"""

:brief 將字符串日期轉換為時間戳

:param date_str: 日期字符串,如:2019-01-30 11:48:07

:param format: 日期字符串對應的格式化格式,如:%Y-%m-%d %H:%M:%S

:return: 時間戳

"""

return int(time.mktime(time.strptime(date_str, format)))

3.獲取當前時間的分鐘/秒

獲取當前時間戳

timestamp = int(time.time())

獲取當前時間的秒

seconds = timestamp % 60

print "seconds:{}".format(seconds)

獲取當前時間的分鐘

minute = (timestamp - seconds) % (60 * 60)

print "minute:{}".format(minute / 60)

4.獲取整分鐘/整小時時間戳

思路:

先除以對應的進制值取整,得到舍棄余數部分的整數,然后再乘以對應的進制值

one_minute = 60 # 一分鐘

one_hour = one_minute * 60 # 一小時

whole_minute = int(timestamp / one_minute) * one_minute

whole_hour = int(timestamp / one_hour) * one_hour

datetime模塊介紹

datetime模塊中常見的類:

datetime.date:處理日期

datetime.time:處理時間

datetime.datetime:處理日期和時間

datetime.timedelta:處理時間差

說明:datetime模塊主要講解如下內容

1.時間戳 –> datetime時間格式 –> 日期字符串

2.日期字符串 –> datetime時間格式 –> 時間元組格式(time.struct_time) –> 時間戳

3.時間差的使用,根據當前時間獲取前N天的時間

1.時間戳 –> datetime時間格式 –> 日期字符串

時間戳 –> datetime時間格式

datetime.datetime.fromtimestamp(timestamp) 參數timestamp:時間戳

例子:

import time, datetime

datetime_type = datetime.datetime.fromtimestamp(time.time())

print type(datetime_type) #

datetime時間格式 –> 日期字符串

datetime.datetime.strftime(format) format:日期字符串對應的格式化格式

例子:

datetime_format = datetime_type.strftime("%Y/%m/%d %H:%M:%S")

print datetime_format # 2019/01/30 16:44:01

2.日期字符串 –> datetime時間格式 –> 時間元組格式(time.struct_time) –> 時間戳

日期字符串 –> datetime時間格式

datetime.datetime.strptime(date_str, format) date_str:字符串日期 format:日期字符串對應的格式化格式

例子:

datetime_type = datetime.datetime.strptime('2019/01/30 16:44:01', '%Y/%m/%d %H:%M:%S')

print type(datetime_type) #

# print datetime_type.timestamp()

print time.mktime(datetime_type.timetuple())

datetime時間格式 –> 時間元組格式(time.struct_time) –> 時間戳

datetime.datetime.timetuple(): datetime轉換為時間元組

例子:

datetime_type_to_timestamp = int(time.mktime(datetime_type.timetuple()))

print datetime_type_to_timestamp

3.時間差的使用,根據當前時間獲取前N天的時間

datetime.timedelta(days, seconds, microseconds, milliseconds, minutes, hours, weeks)

參數說明:

1.days:天

2.seconds:秒

3.microseconds:毫秒 1秒 = 10^3 毫秒

4.milliseconds:微秒 1秒 = 10^6 微秒

5.minutes,分鐘

6.hours:小時

7.weeks:星期 1weeks = 7days

例子:

day_timedelta = datetime.timedelta(days=1) # 獲取1天的時間值

forward_datetime = datetime.datetime.today() - day_timedelta # 獲取前一天的datetime值

print forward_datetime

calendar模塊介紹

說明:

這里介紹一下使用month(year, month)方法打印出某年某月下的月歷時間

例子:

import calendar

cal = calendar.month(2019, 1) # 打印出2019年1月的月歷

print cal

喜歡點個贊!

總結

以上是生活随笔為你收集整理的python什么时候用框架_python时间模块的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。