ko ko en en de ko en ca fa ko ko ko ko ko en en en de en en en fr it en ko en en en en it en en en ko en en en en en en en en en en en en en en en ko en en en en en en fr en fr en en en ja en sv en mk ko en en ko en en en en ko ko ko ko en ru en en ko ko en en en en ko ko ko en ko ko ko ko ko ko en en ko tr en en en en en en en ko ko ko en en en en en en ko ko ko en en it en en en en en en de ko en en en en ko ko ko ko ko ko ko ko en ko ko ko ko ko ko ko ko ko ko ko ko ko en ko ko en ko ko ko ko ko ko ko ko ko ko ko ko ko ko ko ko ko en ko ko ko ko ko ko ko ko en ko
Home About Algorithm Aws Book Deploy Django Docker Restful Git Html&Css IntroducingPython Other Project SQL UiPath
22 Mar 2018 • IntroducingPython
time: 시, 분, 초, 마이크로초
from datetime import date halloween = date(2015, 10, 31) halloween
iso는 국제 표준화 기구에서 재정한 날짜와 시간 표현에 대한 국제표준규걱인 ISO8601을 참고한다.
from datetime import date now = date.today() now
from datetime import timedelta one_day = timedelta(days=1) tomorrow = now + one_day tomorrow
yesterday = now - one_day yesterday
날짜의 범위는 date.min(year=1, month=1, day=1) 부터 date.max(year=9999, month=12, day=31)까지다.
from datetime import time noon = time(12, 0, 0) noon
from datetime import datetime some_day = datetime(2015, 1, 2, 3, 4, 5, 6)
datetime.datetime(2015, 1, 2, 3, 4, 5, 6)
datetime 객체에서 now() 메서드로 현재 날짜와 시간을 얻을수 있다.
from datetime import datetime now = datetime.now()
datetime.datetime(2018, 3, 6, 10, 13, 9, 87686)
combine으로 date객체와 time객체를 datetime객체로 병합할 수 있다.
from datetime import datetime, time, date noon = time(12) this_day = date.today() noon_today = datetime.combine(this_day, noon) noon_today
defatime 객체에서 date()와 time() 메서드를 사용하여 날짜와 시간을 얻을 수 있다.
유닉스 시간은 1970년 1월 1일 자정 이후 시간의 초를 사용한다.
이값을 에포치(epoch)라고 부르며 에포치는 시스테 ㅁ간에 날짜와 시간을 교환하는 아주 간단한방식이다.
time 모듈의 time() 함수는 현재시간을 에포치값으로 반환한다.
import time now = time.time() now
‘Tue Mar 6 10:24:11 2018’
localtime() 메서드는 시간을 시스템 표준 시간대로
gmtime() 메서드는 시간을 UTC로 제공한다.
time.struct_time(tm_year=2018, tm_mon=3, tm_mday=6, tm_hour=10, tm_min=24, tm_sec=11, tm_wday=1, tm_yday=65, tm_isdst=0)
time.struct_time(tm_year=2018, tm_mon=3, tm_mday=6, tm_hour=1, tm_min=24, tm_sec=11, tm_wday=1, tm_yday=65, tm_isdst=0)
한국의 현재시간 10:24는 그 전날의 UTC(그리니치 시간, 줄루 시간)로 1:24 다
인자를 생략하면 현재 시간이다.
mktime() 메서드는 struct_time 객체를 에포치 초 로 변환한다.
tm = time.localtime(now) time.mktime(tm)
이 값은 조금전에 본 now()의 에초피 값과 정확하게 일치 하지 않는다. struct_time 객체는 시간을 초 까지만 유지하기 때문.
시간에 대해서는 UTC, 문자열에 대해서는 UTF-8을 가까이 하라
10.4.3 날짜와 시간 읽고 쓰기
import time now = time.time() print(now) print(time.ctime(now)) print(time.mktime(time.localtime(now)))
1520299927.684161 Tue Mar 6 10:32:07 2018 1520299927.0
strftime() 은 문자열의 출력 포맷을 지정할 수 있다.
import time fmt = "It's %A, %B, %d, %Y, local time %I:%M:%Sp" t = time.localtime() t
time.struct_time(tm_year=2018, tm_mon=3, tm_mday=6, tm_hour=10, tm_min=32, tm_sec=55, tm_wday=1, tm_yday=65, tm_isdst=0)
“It’s Tuesday, March, 06, 2018, local time 10:32:55p”
from datetime import date some_day = date(2015, 12, 12) fmt = "It's %A, %B, %d, %Y, local time %I:%M:%Sp" some_day.strftime(fmt)
“It’s Saturday, December, 12, 2015, local time 12:00:00p”
from datetime import time some_time = time(10, 35) some_time.strftime(fmt)
“It’s Monday, January, 01, 1900, local time 10:35:00p”
문자열을 날짜나 시간으로 변환하기 위해 같은 포맷 문자열로 strptime()을 사용한다.
정규 표현식 패턴 매칭은 없다.
문자열의 비형식 부분(% 제외)이 정확히 일치해야 한다.
import time fmt = "%Y-%m-%d" time.strptime("2015 06 02", fmt)
ValueError Traceback (most recent call last)   in  () 1 import time 2 fmt = "%Y-%m-%d" ----> 3 time.strptime("2015 06 02", fmt) ~/.pyenv/versions/3.6.4/lib/python3.6/_strptime.py in _strptime_time(data_string, format) 557 """Return a time struct based on the input string and the 558 format string.""" --> 559 tt = _strptime(data_string, format)[0] 560 return time.struct_time(tt[:time._STRUCT_TM_ITEMS]) 561 ~/.pyenv/versions/3.6.4/lib/python3.6/_strptime.py in _strptime(data_string, format) 360 if not found: 361 raise ValueError("time data %r does not match format %r" % --> 362 (data_string, format)) 363 if len(data_string) != found.end(): 364 raise ValueError("unconverted data remains: %s" % ValueError: time data '2015 06 02' does not match format '%Y-%m-%d'
time.struct_time(tm_year=2015, tm_mon=6, tm_mday=2, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=153, tm_isdst=-1)
ValueError Traceback (most recent call last)   in  () ----> 1 time.strptime("2015-13-29", fmt) ~/.pyenv/versions/3.6.4/lib/python3.6/_strptime.py in _strptime_time(data_string, format) 557 """Return a time struct based on the input string and the 558 format string.""" --> 559 tt = _strptime(data_string, format)[0] 560 return time.struct_time(tt[:time._STRUCT_TM_ITEMS]) 561 ~/.pyenv/versions/3.6.4/lib/python3.6/_strptime.py in _strptime(data_string, format) 360 if not found: 361 raise ValueError("time data %r does not match format %r" % --> 362 (data_string, format)) 363 if len(data_string) != found.end(): 364 raise ValueError("unconverted data remains: %s" % ValueError: time data '2015-13-29' does not match format '%Y-%m-%d'
이름은 locale에 따라 다르다.
다른 월, 일의 이름을 출력하며녀 setlocale()을 사용하여 로케일을 바꿔야 한다.
setlocale()의 첫번째 인자는 날짜와 시간을 위한 locale.LC_TIME이고, 두 번째는 언어와 국가 약어가 결합된 문자열이다.
import locale from datetime import date halloween = date(2015, 10, 31) for lang_country in ['ko_kr', 'en_us', 'fr_fr', 'de_de', 'es_es', 'is_is']: locale.setlocale(locale.LC_TIME, lang_country) halloween.strftime('%A, %B, %d')
Error Traceback (most recent call last)   in  () 3 halloween = date(2015, 10, 31) 4 for lang_country in ['en_us']: ----> 5 locale.setlocale(locale.LC_TIME, lang_country) 6 halloween.strftime('%A, %B, %d') ~/.pyenv/versions/3.6.4/lib/python3.6/locale.py in setlocale(category, locale) 596 # convert to string 597 locale = normalize(_build_localename(locale)) --> 598 return _setlocale(category, locale) 599 600 def resetlocale(category=LC_ALL): Error: unsupported locale setting
import locale names = locale.locale_alias.keys()
dict_keys([‘a3’, ‘a3_az’, ‘a3_az.koic’, ‘aa_dj’, ‘aa_er’, ‘aa_et’, ‘af’, ‘af_za’, ‘am’, ‘am_et’, ‘american’, ‘an_es’, ‘ar’, ‘ar_aa’, ‘ar_ae’, ‘ar_bh’, ‘ar_dz’, ‘ar_eg’, ‘ar_in’, ‘ar_iq’, ‘ar_jo’, ‘ar_kw’, ‘ar_lb’, ‘ar_ly’, ‘ar_ma’, ‘ar_om’, ‘ar_qa’, ‘ar_sa’, ‘ar_sd’, ‘ar_sy’, ‘ar_tn’, ‘ar_ye’, ‘arabic’, ‘as’, ‘as_in’, ‘ast_es’, ‘ayc_pe’, ‘az’, ‘az_az’, ‘az_az.iso88599e’, ‘be’, ‘be@latin’, ‘be_bg.utf8’, ‘be_by’, ‘be_by@latin’, ‘bem_zm’, ‘ber_dz’, ‘ber_ma’, ‘bg’, ‘bg_bg’, ‘bho_in’, ‘bn_bd’, ‘bn_in’, ‘bo_cn’, ‘bo_in’, ‘bokmal’, ‘bokmål’, ‘br’, ‘br_fr’, ‘brx_in’, ‘bs’, ‘bs_ba’, ‘bulgarian’, ‘byn_er’, ‘c’, ‘c-french’, ‘c.ascii’, ‘c.en’, ‘c.iso88591’, ‘c.utf8’, ‘c_c’, ‘c_c.c’, ‘ca’, ‘ca_ad’, ‘ca_es’, ‘ca_es@valencia’, ‘ca_fr’, ‘ca_it’, ‘catalan’, ‘cextend’, ‘chinese-s’, ‘chinese-t’, ‘crh_ua’, ‘croatian’, ‘cs’, ‘cs_cs’, ‘cs_cz’, ‘csb_pl’, ‘cv_ru’, ‘cy’, ‘cy_gb’, ‘cz’, ‘cz_cz’, ‘czech’, ‘da’, ‘da_dk’, ‘danish’, ‘dansk’, ‘de’, ‘de_at’, ‘de_be’, ‘de_ch’, ‘de_de’, ‘de_li.utf8’, ‘de_lu’, ‘deutsch’, ‘doi_in’, ‘dutch’, ‘dutch.iso88591’, ‘dv_mv’, ‘dz_bt’, ‘ee’, ‘ee_ee’, ‘eesti’, ‘el’, ‘el_cy’, ‘el_gr’, ‘el_gr@euro’, ‘en’, ‘en_ag’, ‘en_au’, ‘en_be’, ‘en_bw’, ‘en_ca’, ‘en_dk’, ‘en_dl.utf8’, ‘en_gb’, ‘en_hk’, ‘en_ie’, ‘en_in’, ‘en_ng’, ‘en_nz’, ‘en_ph’, ‘en_sg’, ‘en_uk’, ‘en_us’, ‘en_us@euro@euro’, ‘en_za’, ‘en_zm’, ‘en_zw’, ‘en_zw.utf8’, ‘eng_gb’, ‘english’, ‘english_uk’, ‘english_united-states’, ‘english_united-states.437’, ‘english_us’, ‘eo’, ‘eo.utf8’, ‘eo_eo’, ‘eo_us.utf8’, ‘eo_xx’, ‘es’, ‘es_ar’, ‘es_bo’, ‘es_cl’, ‘es_co’, ‘es_cr’, ‘es_cu’, ‘es_do’, ‘es_ec’, ‘es_es’, ‘es_gt’, ‘es_hn’, ‘es_mx’, ‘es_ni’, ‘es_pa’, ‘es_pe’, ‘es_pr’, ‘es_py’, ‘es_sv’, ‘es_us’, ‘es_uy’, ‘es_ve’, ‘estonian’, ‘et’, ‘et_ee’, ‘eu’, ‘eu_es’, ‘eu_fr’, ‘fa’, ‘fa_ir’, ‘fa_ir.isiri3342’, ‘ff_sn’, ‘fi’, ‘fi_fi’, ‘fil_ph’, ‘finnish’, ‘fo’, ‘fo_fo’, ‘fr’, ‘fr_be’, ‘fr_ca’, ‘fr_ch’, ‘fr_fr’, ‘fr_lu’, ‘français’, ‘fre_fr’, ‘french’, ‘french.iso88591’, ‘french_france’, ‘fur_it’, ‘fy_de’, ‘fy_nl’, ‘ga’, ‘ga_ie’, ‘galego’, ‘galician’, ‘gd’, ‘gd_gb’, ‘ger_de’, ‘german’, ‘german.iso88591’, ‘german_germany’, ‘gez_er’, ‘gez_et’, ‘gl’, ‘gl_es’, ‘greek’, ‘gu_in’, ‘gv’, ‘gv_gb’, ‘ha_ng’, ‘he’, ‘he_il’, ‘hebrew’, ‘hi’, ‘hi_in’, ‘hi_in.isciidev’, ‘hne’, ‘hne_in’, ‘hr’, ‘hr_hr’, ‘hrvatski’, ‘hsb_de’, ‘ht_ht’, ‘hu’, ‘hu_hu’, ‘hungarian’, ‘hy_am’, ‘hy_am.armscii8’, ‘ia’, ‘ia_fr’, ‘icelandic’, ‘id’, ‘id_id’, ‘ig_ng’, ‘ik_ca’, ‘in’, ‘in_id’, ‘is’, ‘is_is’, ‘iso-8859-1’, ‘iso-8859-15’, ‘iso8859-1’, ‘iso8859-15’, ‘iso_8859_1’, ‘iso_8859_15’, ‘it’, ‘it_ch’, ‘it_it’, ‘italian’, ‘iu’, ‘iu_ca’, ‘iu_ca.nunacom8’, ‘iw’, ‘iw_il’, ‘iw_il.utf8’, ‘ja’, ‘ja_jp’, ‘ja_jp.euc’, ‘ja_jp.mscode’, ‘ja_jp.pck’, ‘japan’, ‘japanese’, ‘japanese-euc’, ‘japanese.euc’, ‘jp_jp’, ‘ka’, ‘ka_ge’, ‘ka_ge.georgianacademy’, ‘ka_ge.georgianps’, ‘ka_ge.georgianrs’, ‘kk_kz’, ‘kl’, ‘kl_gl’, ‘km_kh’, ‘kn’, ‘kn_in’, ‘ko’, ‘ko_kr’, ‘ko_kr.euc’, ‘kok_in’, ‘korean’, ‘korean.euc’, ‘ks’, ‘ks_in’, ‘ks_in@devanagari.utf8’, ‘ku_tr’, ‘kw’, ‘kw_gb’, ‘ky’, ‘ky_kg’, ‘lb_lu’, ‘lg_ug’, ‘li_be’, ‘li_nl’, ‘lij_it’, ‘lithuanian’, ‘lo’, ‘lo_la’, ‘lo_la.cp1133’, ‘lo_la.ibmcp1133’, ‘lo_la.mulelao1’, ‘lt’, ‘lt_lt’, ‘lv’, ‘lv_lv’, ‘mag_in’, ‘mai’, ‘mai_in’, ‘mg_mg’, ‘mhr_ru’, ‘mi’, ‘mi_nz’, ‘mk’, ‘mk_mk’, ‘ml’, ‘ml_in’, ‘mn_mn’, ‘mni_in’, ‘mr’, ‘mr_in’, ‘ms’, ‘ms_my’, ‘mt’, ‘mt_mt’, ‘my_mm’, ‘nan_tw@latin’, ‘nb’, ‘nb_no’, ‘nds_de’, ‘nds_nl’, ‘ne_np’, ‘nhn_mx’, ‘niu_nu’, ‘niu_nz’, ‘nl’, ‘nl_aw’, ‘nl_be’, ‘nl_nl’, ‘nn’, ‘nn_no’, ‘no’, ‘no@nynorsk’, ‘no_no’, ‘no_no.iso88591@bokmal’, ‘no_no.iso88591@nynorsk’, ‘norwegian’, ‘nr’, ‘nr_za’, ‘nso’, ‘nso_za’, ‘ny’, ‘ny_no’, ‘nynorsk’, ‘oc’, ‘oc_fr’, ‘om_et’, ‘om_ke’, ‘or’, ‘or_in’, ‘os_ru’, ‘pa’, ‘pa_in’, ‘pa_pk’, ‘pap_an’, ‘pd’, ‘pd_de’, ‘pd_us’, ‘ph’, ‘ph_ph’, ‘pl’, ‘pl_pl’, ‘polish’, ‘portuguese’, ‘portuguese_brazil’, ‘posix’, ‘posix-utf2’, ‘pp’, ‘pp_an’, ‘ps_af’, ‘pt’, ‘pt_br’, ‘pt_pt’, ‘ro’, ‘ro_ro’, ‘romanian’, ‘ru’, ‘ru_ru’, ‘ru_ua’, ‘rumanian’, ‘russian’, ‘rw’, ‘rw_rw’, ‘sa_in’, ‘sat_in’, ‘sc_it’, ‘sd’, ‘sd_in’, ‘sd_in@devanagari.utf8’, ‘sd_pk’, ‘se_no’, ‘serbocroatian’, ‘sh’, ‘sh_ba.iso88592@bosnia’, ‘sh_hr’, ‘sh_hr.iso88592’, ‘sh_sp’, ‘sh_yu’, ‘shs_ca’, ‘si’, ‘si_lk’, ‘sid_et’, ‘sinhala’, ‘sk’, ‘sk_sk’, ‘sl’, ‘sl_cs’, ‘sl_si’, ‘slovak’, ‘slovene’, ‘slovenian’, ‘so_dj’, ‘so_et’, ‘so_ke’, ‘so_so’, ‘sp’, ‘sp_yu’, ‘spanish’, ‘spanish_spain’, ‘sq’, ‘sq_al’, ‘sq_mk’, ‘sr’, ‘sr@cyrillic’, ‘sr@latn’, ‘sr_cs’, ‘sr_cs.iso88592@latn’, ‘sr_cs@latn’, ‘sr_me’, ‘sr_rs’, ‘sr_rs@latn’, ‘sr_sp’, ‘sr_yu’, ‘sr_yu.cp1251@cyrillic’, ‘sr_yu.iso88592’, ‘sr_yu.iso88595’, ‘sr_yu.iso88595@cyrillic’, ‘sr_yu.microsoftcp1251@cyrillic’, ‘sr_yu.utf8’, ‘sr_yu.utf8@cyrillic’, ‘sr_yu@cyrillic’, ‘ss’, ‘ss_za’, ‘st’, ‘st_za’, ‘sv’, ‘sv_fi’, ‘sv_se’, ‘sw_ke’, ‘sw_tz’, ‘swedish’, ‘szl_pl’, ‘ta’, ‘ta_in’, ‘ta_in.tscii’, ‘ta_in.tscii0’, ‘ta_lk’, ‘te’, ‘te_in’, ‘tg’, ‘tg_tj’, ‘th’, ‘th_th’, ‘th_th.tactis’, ‘th_th.tis620’, ‘thai’, ‘ti_er’, ‘ti_et’, ‘tig_er’, ‘tk_tm’, ‘tl’, ‘tl_ph’, ‘tn’, ‘tn_za’, ‘tr’, ‘tr_cy’, ‘tr_tr’, ‘ts’, ‘ts_za’, ‘tt’, ‘tt_ru’, ‘tt_ru.tatarcyr’, ‘tt_ru@iqtelif’, ‘turkish’, ‘ug_cn’, ‘uk’, ‘uk_ua’, ‘univ’, ‘universal’, ‘universal.utf8@ucs4’, ‘unm_us’, ‘ur’, ‘ur_in’, ‘ur_pk’, ‘uz’, ‘uz_uz’, ‘uz_uz@cyrillic’, ‘ve’, ‘ve_za’, ‘vi’, ‘vi_vn’, ‘vi_vn.tcvn’, ‘vi_vn.tcvn5712’, ‘vi_vn.viscii’, ‘vi_vn.viscii111’, ‘wa’, ‘wa_be’, ‘wae_ch’, ‘wal_et’, ‘wo_sn’, ‘xh’, ‘xh_za’, ‘yi’, ‘yi_us’, ‘yo_ng’, ‘yue_hk’, ‘zh’, ‘zh_cn’, ‘zh_cn.big5’, ‘zh_cn.euc’, ‘zh_hk’, ‘zh_hk.big5hk’, ‘zh_sg’, ‘zh_sg.gbk’, ‘zh_tw’, ‘zh_tw.euc’, ‘zh_tw.euctw’, ‘zu’, ‘zu_za’])
good_names = [name for name in names if len(name) == 5 and name[2] == '_']
[‘a3_az’, ‘aa_dj’, ‘aa_er’, ‘aa_et’, ‘af_za’]
de = [name for name in good_names if name.startswith('de')] de
[‘de_at’, ‘de_be’, ‘de_ch’, ‘de_de’, ‘de_lu’]
comments powered by Disqus.
11.2 네트워크 23 Mar 2018
11.1 병행성 23 Mar 2018
10.5 연습문제 22 Mar 2018
10.4 달력과 시간 22 Mar 2018
10.3 프로그램과 프로세스 22 Mar 2018
10.2 디렉터리 21 Mar 2018
10.1 파일 21 Mar 2018
9.3 연습 문제 19 Mar 2018
9.2 웹 서버 19 Mar 2018
9.1 웹 클라이언트 19 Mar 2018
8.7 연습 문제 15 Mar 2018
8.5 NoSql 데이터 스토어 15 Mar 2018
8.3 관계형 데이터베이스 14 Mar 2018
8.2 구조화된 텍스트 파일 14 Mar 2018
8.1 파일 입출력 14 Mar 2018
7.3 연습 문제 13 Mar 2018
7.2 이진 데이터 13 Mar 2018
7.1 텍스트 문자열 13 Mar 2018
7.1.3 정규표현식 12 Mar 2018
6.15 연습문제 08 Mar 2018
6.14.1 네임드 튜플 08 Mar 2018
6.14.2 클래스와 객체 그리고 모듈은 언제 사용 할까? 08 Mar 2018
6.13 컴포지션 08 Mar 2018
6.12 특수 메서드 08 Mar 2018
6.11 덕 타이핑 08 Mar 2018
6.9 private 네임 맹글링 07 Mar 2018
6.8 get, set 속성값과 프로퍼티 07 Mar 2018
6.10 메서드 타입 07 Mar 2018
6.7 자신 - self 06 Mar 2018
6.6 부모에게 도움 받기 - super 06 Mar 2018
6.5 메서드 추가 하기 06 Mar 2018
6.4 메서드 오버라이드 05 Mar 2018
6.3 상속 05 Mar 2018
6.2 클래스 선언하기 28 Feb 2018
5.7 연습문제 28 Feb 2018
5.5 파이썬 표준 라이브러리 28 Feb 2018
4.13 연습문제 27 Feb 2018
4.12 예외 만들기 26 Feb 2018
4.11 에러 처리하기 try-except 26 Feb 2018
4.9 데코레이터 23 Feb 2018
4.10 네임스페이스와 스코프 23 Feb 2018
4.8 제너레이터 21 Feb 2018
4.7 함수 21 Feb 2018
4.6 컴프리헨션 20 Feb 2018
4.5 순회 하기 20 Feb 2018
4.4 반복 하기 19 Feb 2018
4.3 비교하기 19 Feb 2018
4.2 라인 유지 하기 19 Feb 2018
4.1 주석 달기 19 Feb 2018
3-8 연습문제 13 Feb 2018
3.7 자료구조를 더 크게 13 Feb 2018
3.6 자료구조 비교하기 13 Feb 2018