python 耗時操作 python 時間操作

2021-10-13 09:30:54 字數 3007 閱讀 8849

import datetime

import time

#獲取當前時間

datetime.datetime.now()

#獲取當前日期

datetime.date.today()

#字串轉換為時間格式

>>> t = time.strptime("2009-08-08", "%y-%m-%d")

>>> y,m,d = t[0:3]

>>> datetime.date(y,m,d)

datetime.date(2009, 8, 8)

>>> datetime.datetime(y,m,d)

datetime.datetime(2009, 8, 8, 0, 0)

#返回前一天的日期

>>> import datetime

>>> today = datetime.date.today()

>>> days = datetime.timedelta(days=1)

>>> print today

2010-04-15

>>> print days

1 day, 0:00:00

>>> print today-days

2010-04-14

#datetime 物件獲取當天日期

>>> now = datetime.datetime.now()

>>> today = now.date().today()

>>> print today

2010-04-15

>>> print now

2010-04-15 14:41:21.421000

獲取時間戳

>>> time.time()

1446885023.80355

時間戳轉換時間

>>> time.strftime('%y-%m-%d %h:%m:%s', time.localtime(time.time()))

'2015-11-07 16:32:45'

時間轉換時間戳

>>> time.mktime(time.strptime('2015-11-06 16:30:00','%y-%m-%d %h:%m:%s'))

1446798600.0

獲取年與日時分秒

>>> time.strftime("%y-%m-%d %h:%m:%s", time.localtime())

'2015-11-07 16:45:46'

>>> print (time.strftime("%y-%m-%d %x", time.localtime()))

2015-11-07 16:52:31

獲取微妙(百萬分之一秒)

>>> datetime.datetime.now().microsecond

獲取時間+微秒數

>>> now = datetime.datetime.now()

>>> "%s.%s" % (now.strftime('%y%m%d%h%m%s'), now.microsecond)

'20151107172412.390765'

python格式化日期時間的函式為datetime.datetime.strftime();由字串轉為日期型的函式為:datetime.datetime.strptime(),兩個函式都涉及日期時間的格式化字串,列舉如下:

%a abbreviated weekday name

%a full weekday name

%b abbreviated month name

%b full month name

%d day of month as decimal number (01 - 31)

%h hour in 24-hour format (00 - 23)

%i hour in 12-hour format (01 - 12)

%j day of year as decimal number (001 - 366)

%m month as decimal number (01 - 12)

%m minute as decimal number (00 - 59)

%p current locale's a.m./p.m. indicator for 12-hour clock

%s second as decimal number (00 - 59)

%u week of year as decimal number, with sunday as first day of week (00 - 51)

%w weekday as decimal number (0 - 6; sunday is 0)

%w week of year as decimal number, with monday as first day of week (00 - 51)

%x date representation for current locale

%x time representation for current locale

%y year without century, as decimal number (00 - 99)

%y year with century, as decimal number

%z, %z time-zone name or abbreviation; no characters if time zone is unknown

%% percent sign

例子:字串轉換成datetime

>>> c = datetime.datetime.strptime('sep-21-09 16:34','%b-%d-%y %h:%m');

>>> c

datetime.datetime(2009, 9, 21, 16, 34)

datetime轉換成字串

>>> datetime.datetime.now().strftime('%b-%d-%y %h:%m:%s');

'sep-22-09 16:48:08'

多執行緒 耗時操作

viewcontroller.m 01 耗時操作 created by gzxzmac on 16 1 28.import viewcontroller.h inte ce viewcontroller end implementation viewcontroller void viewdidlo...

IOS 分析耗時操作

最近在工作中發現有寫頁面的tableview存在滑動卡頓的現象,雖然該頁面的布局確實很複雜,但是卡頓的程度有的過分,學習到了instrument 另乙個小玩意可以來分析我到底是 出了問題,在分析之前,tableview 的卡頓原因一般如下 instrument 最開始接觸是分析記憶體洩漏的時候,那個...

Oracle 分開執行耗時操作

start 首先,我們看乙個故事,聯合利華引進了一條香皂包裝生產線,結果發現這條生產線有個缺陷 常常會有盒子裡沒裝入香皂。總不能把空盒子賣給顧客啊,他們只得請了乙個學自動化的博士後設計乙個方案來分揀空的香皂盒。博士後拉起了乙個十幾人的科研攻關小組,綜合採用了機械 微電子 自動化 x射線探測等技術,花...