time.strptime() for different languages

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam Monsen

    #1

    time.strptime() for different languages

    Anyone know of something that works like time.strptime() , but for
    other languages? Specifically, Dutch (ex: "31 augustus 2005, 17:26")
    and German?

    Thinking out loud... since "31 augustus 2005, 17:26" is only different
    by month name, I suppose I could just substitute the month name using
    a translation table for English to Dutch month names.

    --
    Adam Monsen
    Adam Monsen's personal home page


    (crossposted to Seattle Python Users List)

  • Benjamin Niemann

    #2
    Re: time.strptime() for different languages

    Adam Monsen wrote:
    [color=blue]
    > Anyone know of something that works like time.strptime() , but for
    > other languages? Specifically, Dutch (ex: "31 augustus 2005, 17:26")
    > and German?
    >
    > Thinking out loud... since "31 augustus 2005, 17:26" is only different
    > by month name, I suppose I could just substitute the month name using
    > a translation table for English to Dutch month names.[/color]

    Have you tested it with the proper locale setting and strptime(dateSt ring,
    "%c")? I have not ;)

    --
    Benjamin Niemann
    Email: pink at odahoda dot de
    WWW: http://www.odahoda.de/

    Comment

    • Adam Monsen

      #3
      Re: time.strptime() for different languages

      No, this doesn't seem to work, and I can't find anything in the
      documentation indicating that it should.
      [color=blue][color=green][color=darkred]
      >>> import os
      >>> os.getenv('LANG ')[/color][/color][/color]
      'nl_NL'[color=blue][color=green][color=darkred]
      >>> import time
      >>> time.strptime(" 10 augustus 2005 om 17:26", "%d %B %Y om %H:%M")[/color][/color][/color]
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      File "/usr/lib/python2.4/_strptime.py", line 292, in strptime
      raise ValueError("tim e data did not match format: data=%s fmt=%s"
      %
      ValueError: time data did not match format: data=10 augustus 2005 om
      17:26 fmt=%d %B %Y om %H:%M

      --
      Adam Monsen
      Adam Monsen's personal home page


      Comment

      • Fredrik Lundh

        #4
        Re: time.strptime() for different languages

        Adam Monsen wrote:
        [color=blue]
        > No, this doesn't seem to work, and I can't find anything in the
        > documentation indicating that it should.
        >[color=green][color=darkred]
        > >>> import os
        > >>> os.getenv('LANG ')[/color][/color]
        > 'nl_NL'[color=green][color=darkred]
        > >>> import time
        > >>> time.strptime(" 10 augustus 2005 om 17:26", "%d %B %Y om %H:%M")[/color][/color]
        > Traceback (most recent call last):
        > File "<stdin>", line 1, in ?
        > File "/usr/lib/python2.4/_strptime.py", line 292, in strptime
        > raise ValueError("tim e data did not match format: data=%s fmt=%s"
        > %
        > ValueError: time data did not match format: data=10 augustus 2005 om
        > 17:26 fmt=%d %B %Y om %H:%M[/color]
        [color=blue][color=green][color=darkred]
        >>> import locale, time
        >>> time.strftime(" %B")[/color][/color][/color]
        'August'[color=blue][color=green][color=darkred]
        >>> locale.getlocal e()[/color][/color][/color]
        (None, None)[color=blue][color=green][color=darkred]
        >>> locale.setlocal e(locale.LC_ALL , "")[/color][/color][/color]
        'nl_NL'[color=blue][color=green][color=darkred]
        >>> locale.getlocal e()[/color][/color][/color]
        ('nl_NL', 'ISO8859-1')[color=blue][color=green][color=darkred]
        >>> time.strftime(" %B")[/color][/color][/color]
        'augustus'[color=blue][color=green][color=darkred]
        >>> time.strptime(" 10 augustus 2005 om 17:26", "%d %B %Y om %H:%M")[/color][/color][/color]
        (2005, 8, 10, 17, 26, 0, 2, 222, -1)

        (see http://docs.python.org/lib/module-locale.html for more on this)

        </F>



        Comment

        • Adam Monsen

          #5
          Re: time.strptime() for different languages

          Excellent! Thank you, Fredrik!

          --
          Adam Monsen
          Adam Monsen's personal home page


          Comment

          • Adam Monsen

            #6
            Re: time.strptime() for different languages

            Strange, but I can't figure out how to switch back to the default
            locale.
            [color=blue][color=green][color=darkred]
            >>> import locale, datetime, time
            >>> locale.setlocal e(locale.LC_ALL , 'nl_NL')[/color][/color][/color]
            'nl_NL'[color=blue][color=green][color=darkred]
            >>> date = '10 augustus 2005 om 17:26'
            >>> time.strptime(d ate, "%d %B %Y om %H:%M")[/color][/color][/color]
            (2005, 8, 10, 17, 26, 0, 2, 222, -1)[color=blue][color=green][color=darkred]
            >>> locale.setlocal e(locale.LC_ALL , '')[/color][/color][/color]
            'en_US.UTF-8'[color=blue][color=green][color=darkred]
            >>> date = '10 August 2005 at 17:26'
            >>> time.strptime(d ate, "%d %B %Y at %H:%M")[/color][/color][/color]
            Traceback (most recent call last):
            File "<stdin>", line 1, in ?
            File "/usr/lib/python2.4/_strptime.py", line 292, in strptime
            raise ValueError("tim e data did not match format: data=%s fmt=%s"
            %
            ValueError: time data did not match format: data=10 August 2005 at
            17:26 fmt=%d %B %Y at %H:%M


            Also, locale.resetloc ale() throws locale.Error (I saw some open bugs on
            this).

            Ugh! Is this stuff broken or is it just me?

            --
            Adam Monsen
            Adam Monsen's personal home page


            Comment

            • Adam Monsen

              #7
              Re: time.strptime() for different languages

              Figured this out. I thought I'd post my results in case it is helpful
              to someone else.

              ----------------------------------8<----------------------------------
              import locale, time
              # save old locale
              old_loc = locale.getlocal e(locale.LC_TIM E)
              locale.setlocal e(locale.LC_TIM E, 'nl_NL')
              # seems to be the only way to avoid a ValueError from _strptime...
              # now that's a badly behaved module!
              import _strptime; reload(_strptim e)
              # parse local date
              date = '10 augustus 2005 om 17:26'
              format = '%d %B %Y om %H:%M'
              dateTuple = time.strptime(d ate, format)
              # switch back to previous locale
              locale.setlocal e(locale.LC_TIM E, old_loc)
              ---------------------------------->8----------------------------------

              If I try to do further date parsing in the same scope (with a different
              locale), it fails. Let me know if you have any ideas about why.

              --
              Adam Monsen
              Adam Monsen's personal home page


              Comment

              • Adam Monsen

                #8
                Re: time.strptime() for different languages

                One way I'm able to do further date parsing in other locales is to
                switch the locale for LC_TIME, bust the _strptime regular expressions
                manually, then call strptime() again. Here's a function to bust the
                cache. This works for me, but your mileage may vary.

                def bust_strptime_c ache():
                import _strptime
                _strptime._cach e_lock.acquire( )
                _strptime._Time RE_cache = _strptime.TimeR E()
                _strptime._rege x_cache = {}
                _strptime._cach e_lock.release( )

                This has been filed as Python bug #1290505. (
                http://sf.net/support/tracker.php?aid=1290505 ) A full test case is
                attached to that bug.

                --
                Adam Monsen
                Adam Monsen's personal home page


                Comment

                • Adam Monsen

                  #9
                  Re: time.strptime() for different languages

                  Brett Cannon fixed this bug last week. Thanks, Brett!

                  --
                  Adam Monsen
                  Adam Monsen's personal home page


                  Comment

                  Working...