python 的 time 模塊

大笑轉載自:http://www.jb51.net/article/87721.htm  因個人需要,稍有改編


time模塊
time模塊是包含各方面對時間操作的函數. 儘管這些常常有效但不是所有方法在任意平臺中有效. time用struct_time表示時間

?

函數

  • time.time(): 返回一個時間戳
  • time.asctime([t]): 轉換gmtime()和localtime()返回的元組或struct_time爲string.
  • time.clock(): 在第一次調用的時候, 返回程序運行的時間. 第二次之後返回與之前的間隔.
  • time.ctime([secs]): 將時間戳轉換爲時間字符串, 如沒有提供則返回當前的時間字符串,並與asctime(localtime())一樣.
  • time.gmtime([secs]): 將時間戳轉化爲, UTC 時區的struct_time.
  • time.localtime([secs]): 類似gmtime()但會把他轉換成本地時區.
  • time.mktime(t): struct_time 轉化爲時間戳.
  • time.sleep(secs): 線程推遲指定時間, 以秒爲單位.
  • time.strftime(format[,t]): 根據參數轉換一個sturc_time或元組爲字符串.
  • time.strptime(string[, format]): 與strftime相反,返回一個struct_time.
自己的python3.6的運行結果
>>> import time
>>> print (time.asctime(time.gmtime()))
Fri Aug 18 01:51:17 2017
>>> print(time.clock())
1.9555558038801022e-06
>>> print(time.clock())
26.231219819837438
>>> print(time.clock())
40.59816275532226
>>> print(time.ctime())
Fri Aug 18 09:53:14 2017
>>> print(time.mktime(time.localtime()))
1503021235.0

以下的time用法暫時還沒看||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

time模塊中常用的格式化字符串

  • %y 兩位數的年份 00 ~ 99.
  • %Y 四位數的年份 0000 ~ 9999
  • %m 月份 01 ~ 12.
  • %d day 01 ~ 31.
  • %H 時 00 ~ 23.
  • %I 時 01 ~ 12.
  • %M 分 00 ~ 59.
  • %S 秒 00 ~ 61.

datetime模塊
datetime模塊提供對於日期和時間進行簡單或複雜的操作. datetime 模塊提供了一下的可用類型(Available Types).

datetime.MINYEAR 和 datetime.MAXYEAR 模塊常量表示datetime接受的範圍

  • class datetime.date: 一個理想化的日期, 提供year, month, day屬性
  • class datetime.time: 一個理想化的時間, 提供hour, minute, second, microsecond, tzinfo.
  • class datetime.datetime: 日期和時間的組合.提供year, month, day, hour, minute, second, microsecond, tzinfo.
  • class datetime.timedelta: 表達兩個date,time和datetime持續時間內的微妙差異.
  • class datetime.tzinfo: 時間對象的抽象基類.
?
1
2
3
4
5
6
7
8
9
from datetime import timedelta, datetime
 
a = datetime.now()
b = timedelta(days=7)
 
# 7 days, 0:00:00
# 2015-04-14 16:02:39.189000
print b
print a - b

下面說具體說一下類和類的方法

date類

一個date對象代表理想化的日期.

?
1
2
3
4
5
6
class datetime.date(year, month, day)
  # All arguments are required. Arguments may be ints or longs.
  # 所有參數都是必須的. 參數可能是 int 或 long.
  MINYEAR <= year <= MAXYEAR
  1<= month <= 12
  1<= day <= number of days in the given month and year.(隨着月份和年份)

如果參數脫離給的範圍會拋出, valueError.

1.類方法 >`date.today()`:返回當前的本地日期, 這等價於 `date.fromtimestamp(time.time())`.
Return the current local date. This is equvalent to `date.fromtimestamp(time.time())`.

?
1
2
3
4
from datetime import date
 
# print 2015-04-21
print date.today()

2.date.fromtimestamp(timestamp):根據提供的時間戳返回local date. 時間戳常用於對時間類型的存儲.

?
1
2
3
4
5
6
7
import time
from datetime import date
 
# 1429587111.21
# 2015-04-21
print time.time()
print date.fromtimestamp(time.time())

3.類方法date.fromordinal(ordinal):根據提供的Gregorian日曆返回date.(不做描述)

類屬性

  • date.min: 返回 date(MINYEAR, 1, 1).
  • date.max: 返回 date(MAXYEAR, 12, 31).
  • date.year: 返回 年, MINYEAR和MAXYEAR之間
  • date.month: 返回 月, 1到12月之間
  • date.day: 返回 1到 n 之間.
?
1
2
3
d = date(2014, 4, 21)
# 2014 4 21
print d.year, d.month, d.day

實例方法

  • date.replace(year, month, day):返回一個相同值的data對象, 除了這些參數給關鍵字指定新的值.
  • date.timetuple(): 返回一個time.struct_time對象.
  • date.toordinal(): 返回一個Gregoian Calendar對象.
  • date.weekday(): 返回day of the week. 星期一爲0,星期日爲6.
  • date.isoweekday(): 返回day of the week. 星期一爲1,星期日爲7.
  • date.isocalendar(): 返回一個三元組, (ISO year, ISO week number, ISO weekday).
  • date.isoformat(): 返回 一個'YYYY-MM-DD'的字符串格式.
  • date.ctime(): 返回一個字符串日期, d.ctime() 等同於 time.ctime(time.mktime(d.timetuple())).
  • date.strftime(format): 返回一個字符串日期, 格式自定義.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
d = date(2015, 4, 21)
 
# 2015-04-21
# 2015-04-21
# 2015-04-22
print d
print d.replace()
print d.replace(day=22)
 
# time.struct_time(tm_year=2015, tm_mon=4, tm_mday=21, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=111, tm_isdst=-1)
print d.timetuple()
 
# print 1
# print 2
print d.weekday()
print d.isoweekday()
 
# print 2015-04-21
print d.isoformat()
 
# print 21/04/2015
print d.strftime('%d/%m/%y')

datetime 類

datetime 對象是一個單一的對象, 包含所有date和time對象的信息.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
class datetime.datetime(year, month, day[, hour
                    [, minute
                    [, second
                    [, microsecond
                    [, tzinfo]]]]])
  # The year, month and day arguments are required.
  MINYEAR <= year <= MAXYEAR
  1 <= month <= 12
  1 <= day <= n
  0 <= hour < 24
  0 <= minute < 60
  0 <= second < 60
  0 <= microsecond < 10**6

類方法

  • datetime.today(): 返回當前本地datetime.隨着 tzinfo None. 這個等同於datetime.fromtimestamp(time.time()).
  • datetime.now([tz]): 返回當前本地日期和時間, 如果可選參數tz爲None或沒有詳細說明,這個方法會像today().
  • datetime.utcnow(): 返回當前的UTC日期和時間, 如果tzinfo None ,那麼與now()類似.
  • datetime.fromtimestamp(timestamp[, tz]): 根據時間戳返回本地的日期和時間.tz指定時區.
  • datetime.utcfromtimestamp(timestamp): 根據時間戳返回 UTC datetime.
  • datetime.fromordinal(ordinal): 根據Gregorian ordinal 返回datetime.
  • datetime.combine(date, time): 根據date和time返回一個新的datetime.
  • datetime.strptime(date_string, format): 根據date_string和format返回一個datetime.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from datetime import datetime
 
# 2015-04-21 14:07:39.262000
print datetime.today()
 
# 2015-04-21 14:08:20.362000
print datetime.now()
 
# 1429596607.06
# 2015-04-21 14:10:07.061000
t = time.time()
print t
print datetime.fromtimestamp(t)
 
from datetime import datetime, date, time
 
a = date(2015, 4, 21)
b = time(14, 13, 34)
 
# 2015-04-21 14:13:34
print datetime.combine(a, b)

實例方法

  • datetime.date(): 返回相同年月日的date對象.
  • datetime.time(): 返回相同時分秒微秒的time對象.
  • datetime.replace(kw): kw in [year, month, day, hour, minute, second, microsecond, tzinfo], 與date類似.

其他方法可查看官方文檔…

?
1
2
3
4
5
6
7
from datetime import datetime, date, time
 
td = date(2015, 4, 21)
n = time(14, 28, 30)
 
# 2099-04-21 14:30:42.103000
print datetime.now(0.replace(year=2099)

類屬性

  • datetime.min: datetime(MINYEAR, 1, 1).
  • datetime.max: datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999).

實例屬性(read-only)

  • datetime.year: 1 至 9999
  • datetime.month: 1 至 12
  • datetime.day: 1 至 n
  • datetime.hour: In range(24). 0 至 23
  • datetime.minute: In range(60).
  • datetime.second: In range(60).
  • datetime.microsecond: In range(1000000).

time類

time 代表本地(一天內)時間.

?
1
2
3
4
5
6
7
8
9
10
11
class datetime.time([hour
          [, minute
          [, second
          [, microsecond
          [, tzinfo]]]]])
  # All arguments are optional.
  # 所有參數都是可選的.
  0 <= hour < 24
  0 <= minute < 60
  0 <= second < 60
  0 <= microsesond < 10**6

time類就是對時間的一些操作,其功能類似與datetime.其實date和time就是對datetime中日期和時間的操作.




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章