91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

time和datetime模塊

發布時間:2020-08-28 18:49:43 來源:網絡 閱讀:441 作者:DevOperater 欄目:編程語言

1.在Python中表示時間的方式

1.1時間戳

時間戳(timestamp)方式:通常來說,時間戳表示從1970年1月1日00:00:00開始按秒計算的偏移量。運行type(time.time())返回float類型
UTC (Coordinated Universal Time,世界協調時)即格林威治天文時間,世界標準時間。在中國為UTC+8。DST(Daylight Saving Time)即夏令時

>>> import time
>>> type(time.time())
<class 'float'>
>>> print(time.time())
1557046718.6016247

1.2.格式化的時間字符串

"2019-05-05"

1.3.元組

元組(struct_time)方式共九個元素。返回struct_time的函數主要有gmtime(),localtime(),strptime()。下面列出這種方式元組中的幾個元素

索引(Index)    屬性(Attribute)    值(Values)
0     tm_year(年)                             比如2011 
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
6     tm_wday(weekday)                  0 - 6(0表示周日)
7     tm_yday(一年中的第幾天)        1 - 366
8     tm_isdst(是否是夏令時)            默認為-1

2.time模塊的方法

  • time.localtime([secs]):將一個時間戳轉換為當前時區的struct_time。secs參數未提供,則以當前時間為準。
>>> time.localtime()
time.struct_time(tm_year=2019, tm_mon=5, tm_mday=5, tm_hour=17, tm_min=15, tm_sec=43, tm_wday=6, tm
_yday=125, tm_isdst=0)
>>> time.localtime()
time.struct_time(tm_year=2019, tm_mon=4, tm_mday=25, tm_hour=23, tm_min=57, tm_sec=32, tm_wday=3, tm_yday=115, tm_isdst=0)
>>> a.tm_year
2019
>>> '%s-%s-%s'%(a.tm_year,a.tm_mon,a.tm_mday)
'2019-4-26'
  • time.gmtime([secs]):和localtime()方法類似,gmtime()方法是將一個時間戳轉換為UTC時區(0時區)的struct_time。0時區的時間+8小時是utc+8的時間
>>> time.gmtime()
time.struct_time(tm_year=2019, tm_mon=5, tm_mday=5, tm_hour=9, tm_min=20, tm_sec=41, tm_wday=6, tm_
yday=125, tm_isdst=0)
  • time.time():返回當前時間的時間戳。
>>> time.time()
1557048269.2991927
  • time.mktime(t):將一個struct_time轉化為時間戳。
>>> time.mktime(time.localtime())
1557048434.0
  • time.sleep(secs):線程推遲指定的時間運行。單位為秒。
time.sleep(2)
  • time.asctime([t]):把一個表示時間的元組或者struct_time表示為這種形式:'Sun Oct 1 12:04:38 2017'。如果沒有參數,將會將time.localtime()作為參數傳入。
>>> time.asctime(time.localtime())
'Sun May  5 17:30:00 2019'
  • time.ctime([secs]):把一個時間戳(按秒計算的浮點數)轉化為time.asctime()的形式。如果參數未給或者為None的時候,將會默認time.time()為參數。它的作用相當于time.asctime(time.localtime(secs))。
>>> time.ctime(time.time())
'Sun May  5 17:34:26 2019'
  • time.strftime(format[, t]):把一個代表時間的元組或者struct_time(如由time.localtime()和time.gmtime()返回)轉化為格式化的時間字符串。如果t未指定,將傳入time.localtime()。
>>> time.strftime("%Y-%m-%d %X", time.localtime())
'2019-05-05 17:37:35'
  • time.strptime(string[, format]):把一個格式化時間字符串轉化為struct_time。實際上它和strftime()是逆操作。
>>> time.strptime('2019-05-05 17:37:35', "%Y-%m-%d %X")
time.struct_time(tm_year=2019, tm_mon=5, tm_mday=5, tm_hour=17, tm_min=37, tm_sec=35, tm_wday=6, tm
_yday=125, tm_isdst=-1)

time和datetime模塊

字符串轉時間格式對應表

    Meaning Notes
%a  Locale’s abbreviated weekday name.  
%A  Locale’s full weekday name. 
%b  Locale’s abbreviated month name.    
%B  Locale’s full month name.   
%c  Locale’s appropriate date and time representation.  
%d  Day of the month as a decimal number [01,31].   
%H  Hour (24-hour clock) as a decimal number [00,23].   
%I  Hour (12-hour clock) as a decimal number [01,12].   
%j  Day of the year as a decimal number [001,366].  
%m  Month as a decimal number [01,12].  
%M  Minute as a decimal number [00,59]. 
%p  Locale’s equivalent of either AM or PM. (1)
%S  Second as a decimal number [00,61]. (2)
%U  Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.    (3)
%w  Weekday as a decimal number [0(Sunday),6].  
%W  Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.    (3)
%x  Locale’s appropriate date representation.   
%X  Locale’s appropriate time representation.   
%y  Year without century as a decimal number [00,99].   
%Y  Year with century as a decimal number.  
%z  Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59]. 
%Z  Time zone name (no characters if no time zone exists).  
%%  A literal '%' character.
>>> time.strftime("%a",time.localtime())
'Sun'
>>> time.strftime("%A",time.localtime())
'Sunday'
>>> time.strftime("%b",time.localtime())
'May'
>>> time.strftime("%B",time.localtime())
'May'
>>> time.strftime("%c",time.localtime())
'Sun May  5 17:54:45 2019'
>>> time.strftime("%C",time.localtime())
'20'
>>> time.strftime("%d",time.localtime())
'05'
>>> time.strftime("%D",time.localtime())
'05/05/19'
>>> time.strftime("%H",time.localtime())
'17'
>>> time.strftime("%h",time.localtime())
'May'
>>> time.strftime("%I",time.localtime())
'05'
>>> time.strftime("%i",time.localtime())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Invalid format string
>>> time.strftime("%j",time.localtime())
'125'
>>> time.strftime("%m",time.localtime())
'05'
>>> time.strftime("%M",time.localtime())
'57'
>>> time.strftime("%p",time.localtime())
'PM'
>>> time.strftime("%S",time.localtime())
'22'
>>> time.strftime("%U",time.localtime())
'18'
>>> time.strftime("%W",time.localtime())
'17'
>>> time.strftime("%w",time.localtime())
'0'
>>> time.strftime("%x",time.localtime())
'05/05/19'
>>> time.strftime("%X",time.localtime())
'17:58:54'
>>> time.strftime("%y",time.localtime())
'19'
>>> time.strftime("%Y",time.localtime())
'2019'
>>> time.strftime("%Z",time.localtime())
'?D1¨2?à¨o?á?¨o?à??'
>>> time.strftime("%%",time.localtime())
'%'
>>>

3.datetime模塊

datetime.date:表示日期的類。常用的屬性有year, month, day;
datetime.time:表示時間的類。常用的屬性有hour, minute, second, microsecond;
datetime.datetime:表示日期時間。
datetime.timedelta:表示時間間隔,即兩個時間點之間的長度。
datetime.tzinfo:與時區有關的相關信息。(這里不詳細充分討論該類,可以參考python手冊)
"需要記住的"
1.d=datetime.datetime.now() 返回當前的datetime日期類型
d.timestamp(),d.today(), d.year,d.timetuple()等方法可以調用

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import datetime
d = datetime.datetime.now()
print(d)
print("d.timestamp()",d.timestamp())
print("d.today()",d.today())
print("d.year",d.year)
print("d.timetuple()",d.timetuple())

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
2019-05-05 18:11:52.005561
d.timestamp() 1557051112.005561
d.today() 2019-05-05 18:11:52.005561
d.year 2019
d.timetuple() time.struct_time(tm_year=2019, tm_mon=5, tm_mday=5, tm_hour=18, tm_min=11, tm_sec=52, tm_wday=6, tm_yday=125, tm_isdst=-1)

Process finished with exit code 0
2.datetime.date.fromtimestamp(時間戳)把時間戳轉換為datetime日期類型
>>> datetime.date.fromtimestamp(1557051112.005561)
datetime.date(2019, 5, 5)
3.時間運算
>>> datetime.datetime.now()

datetime.datetime(2017, 10, 1, 12, 53, 11, 821218)

>>> datetime.datetime.now() + datetime.timedelta(4) #當前時間 +4天

datetime.datetime(2017, 10, 5, 12, 53, 35, 276589)

>>> datetime.datetime.now() + datetime.timedelta(hours=4) #當前時間+4小時

datetime.datetime(2017, 10, 1, 16, 53, 42, 876275)
4.時間替換
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import datetime
d = datetime.datetime.now()

print(d.replace(year=2020,month=11,day=30))

print("d.timetuple()",d.timetuple())

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
2020-11-30 18:16:59.517815
d.timetuple() time.struct_time(tm_year=2019, tm_mon=5, tm_mday=5, tm_hour=18, tm_min=16, tm_sec=59, tm_wday=6, tm_yday=125, tm_isdst=-1)

Process finished with exit code 0
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

双柏县| 雷山县| 文水县| 邢台市| 巧家县| 乐都县| 上饶县| 北海市| 新龙县| 阳春市| 卢湾区| 左贡县| 鸡东县| 双流县| 保德县| 巴中市| 子长县| 夏津县| 桃园县| 密山市| 田阳县| 措勤县| 房产| 五莲县| 夏津县| 浦北县| 若羌县| 鹿邑县| 遂川县| 海安县| 昌江| 四子王旗| 镇坪县| 辉县市| 运城市| 兴海县| 应城市| 昌江| 闽清县| 营山县| 武穴市|