Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #76263

Re: how to change the time string into number?

From Marko Rauhamaa <marko@pacujo.net>
Newsgroups comp.lang.python
Subject Re: how to change the time string into number?
Date 2014-08-14 08:19 +0300
Organization A noiseless patient Spider
Message-ID <874mxfd4wv.fsf@elektro.pacujo.net> (permalink)
References <53EC14EC.2090503@gmail.com> <20140813210111.197005cd@bigbox.christie.dr> <mailman.12958.1407982655.18130.python-list@python.org>

Show all headers | View raw


Tim Chase <python.list@tim.thechases.com>:

> 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 "<stdin>", line 1, in <module>
     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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: how to change the time string into number? Tim Chase <python.list@tim.thechases.com> - 2014-08-13 21:16 -0500
  Re: how to change the time string into number? YBM <ybmess@nooos.fr.invalid> - 2014-08-14 04:51 +0200
    Re: how to change the time string into number? Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-13 21:07 -0600
    Re: how to change the time string into number? Roy Smith <roy@panix.com> - 2014-08-13 23:14 -0400
  Re: how to change the time string into number? Marko Rauhamaa <marko@pacujo.net> - 2014-08-14 08:19 +0300

csiph-web