Post

(python) time, date, datetime

timeout

https://pypi.python.org/pypi/timeout-decorator

time

1
2
3
4
5
6
time.time()    #UTC 기준 초단위 실수형 리턴
time.localtime()    #초단위 실수형 리턴값을 연,월,시 등의 time.struct\_time(투플)로 리턴
time.asctime()    #time.struct\_time을 보기 좋은 형태로 리턴
time.ctime() == time.asctime(time.localtime(time.time()))
time.strftime('형식 포맷 코드', time.localtime(time.time()))
time.sleep()

date

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2017, 11, 12, 8, 12, 50, 236423)
>>> datetime.datetime.now().strftime('%y%m%d-%H%M')
'171112'
>>> (datetime.now() - timedelta(minutes=20)).isoformat(timespec='seconds')
'2018-07-25T00:32:17'
 
>>> datetime.date.today()
datetime.date(2017, 11, 12)
>>> datetime.date.today().strftime('%y%m%d')
'171112'
 
>>> str_date = '2004/03/30'
>>> date = datetime.strptime(str_date, "%Y/%m/%d")
1
2
3
4
def get_utc():
    hours = int(time.localtime().tm_gmtoff / 3600)
    minutes = time.localtime().tm_gmtoff % 60
    return "+{0:0>2}:{1:0>2}".format(hours, minutes)

https://github.com/umbum/Python-snippet/blob/master/extract_days_by_month.py

This post is licensed under CC BY 4.0 by the author.