Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Marko Rauhamaa Newsgroups: comp.lang.python Subject: Re: how to change the time string into number? Date: Thu, 14 Aug 2014 08:19:28 +0300 Organization: A noiseless patient Spider Lines: 47 Message-ID: <874mxfd4wv.fsf@elektro.pacujo.net> References: <53EC14EC.2090503@gmail.com> <20140813210111.197005cd@bigbox.christie.dr> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx05.eternal-september.org; posting-host="ff5cf27ef3d5b31f034d3b72bdc27a41"; logging-data="27946"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+kdiUROYbLlGy2KhqRmYFU" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux) Cancel-Lock: sha1:Y0vbAkXdvn37BuEsmXck8xIUfIA= sha1:yiPFNeXPVFOLFtDiv9lAZ2fywy0= Xref: csiph.com comp.lang.python:76263 Tim Chase : > Or, if you want a more convoluted way: > > >>> import calendar as c > >>> [i for i, m in enumerate(c.month_abbr) if m == "Aug"].pop() > 8 Let's not forget the much simpler solutions: >>> def eight(x): return 8 ... >>> eight("Aug") 8 and: >>> 8 8 BTW, is this a bug: >>> import locale >>> locale.getlocale() ('de_DE', 'UTF-8') >>> import time >>> time.strptime("Dez", "%b").tm_mon Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.2/_strptime.py", line 482, in _strptime_time tt = _strptime(data_string, format)[0] File "/usr/lib/python3.2/_strptime.py", line 337, in _strptime (data_string, format)) ValueError: time data 'Dez' does not match format '%b' >>> time.strftime("%b", time.localtime(time.time() + 120 * 86400)) 'Dec' >>> time.strftime("%x") '08/14/14' After all, "%b" is documented as "Locale’s abbreviated month name." Anyway, "%b" *should* depend on the locale, so str[pf]time may not be suitable to deal with email dates, for example. Marko