본문 바로가기
코딩 연습/코딩배우기

[python] 파이썬 시간 관련 함수, time() datetime() timezone()

by good4me 2020. 9. 8.

goodthings4me.tistory.com

 

파이썬 웹 사이트 개발 연습 중, UI에 표시할 날짜와 시간을 어떻게 표시할 지 고민했는데, 파이썬 시간 관련 함수인 time(), datetime(), timezone()을 사용하면 얼마든지 만들어 사용할 수 있었다.

pytz 라이브러리는 pip로 설치하고 사용해야한다.

# pip install pytz
from pytz import common_timezones, timezone
from datetime import datetime
from time import time, localtime, gmtime, ctime


print(datetime.today())  # 2020-09-08 11:27:41.561416
print(datetime.now())  # 2020-09-08 11:27:41.561416

print('한국시간 :', datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
# 한국시간 : 2020-09-08 11:27:41
print(datetime.now().strftime('%Y-%m-%d'))
# 2020-09-08

print('한국시간 :', datetime.today().strftime('%Y-%m-%d %H:%M:%S'))
# 한국시간 : 2020-09-08 11:27:41
print(datetime.today().strftime('%Y-%m-%d'))
# 2020-09-08

KST = datetime.now(timezone('Asia/Seoul'))  # Korea Standard Time
print('한국시간 :', KST.strftime('%Y-%m-%d %H:%M:%S'))
# 한국시간 : 2020-09-08 11:27:41
print(KST.strftime('%Y-%m-%d'))
# 2020-09-08


print(time())
# 1599532061.5614164
print(localtime(time()))  # ==gmtime(time())
# time.struct_time(tm_year=2020, tm_mon=9, tm_mday=8, tm_hour=11, tm_min=27, 
# tm_sec=41, tm_wday=1, tm_yday=252, tm_isdst=0)
# time_struct 클래스
# tm_wday는 요일(월요일~일요일, 0~6), tm_yday는 1월 1일부터 경과한 일수, tm_isdst는 서머타임 여부


# 날짜/시간 형식 만들기
t = localtime(time())
wk_list = ['월', '화', '수', '목', '금', '토', '일']
wk = None
print(type(t.tm_wday))  # <class 'int'>

for n in range(len(wk_list)):
    if n == t.tm_wday:
        wk = wk_list[n]
        
print('{}.{}.{}({}) {}:{}:{}'.format(t.tm_year, t.tm_mon, t.tm_mday,\
        wk, t.tm_hour, t.tm_min, t.tm_sec))
# 2020.9.8(화) 11:27:41


print(ctime(time()))
# Tue Sep  8 11:27:41 2020


tz_list = ['Asia/Seoul', 'Africa/Casablanca', 'America/Chicago', 'America/Costa_Rica',\
             'America/Mexico_City', 'America/New_York',\
             'America/Chicago', 'America/Costa_Rica', 'America/Mexico_City',\
             'America/New_York', 'America/Santiago', 'Asia/Bangkok',\
             'Asia/Hong_Kong', 'Asia/Pyongyang', 'Asia/Singapore',\
             'Asia/Taipei', 'Asia/Tokyo', 'Australia/Melbourne',\
             'Australia/Sydney', 'Canada/Atlantic', 'Canada/Mountain',\
             'Europe/Berlin', 'Europe/Helsinki', 'Europe/London',\
             'Europe/Luxembourg', 'Europe/Paris', 'Europe/Rome', 'Europe/Vienna',\
             'Pacific/Fiji', 'Pacific/Honolulu', 'Pacific/Saipan', 'US/Alaska',\
             'US/Hawaii']
 
for tz in tz_list:
    wtime = datetime.now(timezone(tz))
    print('{}: {}'.format(tz, wtime.strftime('%Y-%m-%d %H:%M:%S')))

    
# ==================전 세계 국가도시========================
# for tz in common_timezones:
#     world_time = datetime.now(timezone(tz))
#     print('{}: {}'.format(tz, world_time.strftime(fmt)))
# =========================================================

good4me.co.kr

-------------------------------------------------------------------
for tz in tz_list:
    wtime = datetime.now(timezone(tz))
    print('{}: {}'.format(tz, wtime.strftime('%Y-%m-%d %H:%M:%S')))
-------------------------------------------------------------------

위 for문 출력 결과

Asia/Seoul: 2020-09-08 11:32:40
Africa/Casablanca: 2020-09-08 03:32:40
America/Chicago: 2020-09-07 21:32:40
America/Costa_Rica: 2020-09-07 20:32:40
America/Mexico_City: 2020-09-07 21:32:40
America/New_York: 2020-09-07 22:32:40
America/Chicago: 2020-09-07 21:32:40
America/Costa_Rica: 2020-09-07 20:32:40
America/Mexico_City: 2020-09-07 21:32:40
America/New_York: 2020-09-07 22:32:40
America/Santiago: 2020-09-07 23:32:40
Asia/Bangkok: 2020-09-08 09:32:40
Asia/Hong_Kong: 2020-09-08 10:32:40
Asia/Pyongyang: 2020-09-08 11:32:40
Asia/Singapore: 2020-09-08 10:32:40
Asia/Taipei: 2020-09-08 10:32:40
Asia/Tokyo: 2020-09-08 11:32:40
Australia/Melbourne: 2020-09-08 12:32:40
Australia/Sydney: 2020-09-08 12:32:40
Canada/Atlantic: 2020-09-07 23:32:40
Canada/Mountain: 2020-09-07 20:32:40
Europe/Berlin: 2020-09-08 04:32:40
Europe/Helsinki: 2020-09-08 05:32:40
Europe/London: 2020-09-08 03:32:40
Europe/Luxembourg: 2020-09-08 04:32:40
Europe/Paris: 2020-09-08 04:32:40
Europe/Rome: 2020-09-08 04:32:40
Europe/Vienna: 2020-09-08 04:32:40
Pacific/Fiji: 2020-09-08 14:32:40
Pacific/Honolulu: 2020-09-07 16:32:40
Pacific/Saipan: 2020-09-08 12:32:40
US/Alaska: 2020-09-07 18:32:40
US/Hawaii: 2020-09-07 16:32:40

 

댓글