{"id":5970,"date":"2014-04-11T06:54:07","date_gmt":"2014-04-11T06:54:07","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/11\/work-around-for-time-strptimetoday-strftimex-x-failing-in-some-locales-collection-of-common-programming-errors\/"},"modified":"2014-04-11T06:54:07","modified_gmt":"2014-04-11T06:54:07","slug":"work-around-for-time-strptimetoday-strftimex-x-failing-in-some-locales-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/11\/work-around-for-time-strptimetoday-strftimex-x-failing-in-some-locales-collection-of-common-programming-errors\/","title":{"rendered":"work around for time.strptime(today.strftime(&ldquo;%x&rdquo;), &ldquo;%x&rdquo;) failing in some locales-Collection of common programming errors"},"content":{"rendered":"<p>Trying to display a localized date string in a text gui widget. The approach I am taking at the moment is to display the localized string with datetime.datetime.strftime(&#8220;%x&#8221;) into a text widget, so that a user can edit it. Then I try to parse the string with time.strptime(&#8220;x&#8221;).<\/p>\n<p>The current problem is that for some locales (or_IN, ja_JP.UTF-8, ko_KO.UTF-8) time.strptime(&#8220;x&#8221;) can&#8217;t parse the format generated by datetime.datetime.strftime(&#8220;%x&#8221;). It always throws a ValueError trying to parse this.<\/p>\n<p>I suspect this is a problem with strptime and date strings that use doublebyte seperators in the date format. It could also be an issue with differences in what %x means for strftime and strptime (if I understand correctly, one is basically just the glibc implementation, while the other is python specific).<\/p>\n<p>I&#8217;m looking for a better approach to handling localized date strings, so that they can be edited in the ui.<\/p>\n<pre><code>#!\/usr\/bin\/python\n\n\n# We are trying to present a localized date in a text widget, so\n# that a user could potentially edit it, and then we try to\n# parse it with datetime.strptime\n\n# however, even if the user doesn't edit the date produced\n# by today.strftime(\"%x\"), time.strptime(DATE, \"%x\") fails\n# to parse it\n\n# Not sure what's going on there before. I've seen this once\n# before with in_OR, and that turned out to be a locale bug\n# in glibc. This could also be the case, so what I'm looking\n# for is a more robust approach to this problem that will\n# work on all locales\n\n# platform: fedora 14, x86_64\n# Python 2.7 (r27:82500, Sep 16 2010, 18:02:00) \n# [GCC 4.5.1 20100907 (Red Hat 4.5.1-3)] on linux2\n\nimport datetime\nimport time\nimport locale\n\ntoday = datetime.date.today()\n\n# works for \"C\"\nlocale.setlocale(locale.LC_ALL, 'C')\nprint time.strptime(today.strftime(\"%x\"), \"%x\")\n\n# works for en_us.utf8\nlocale.setlocale(locale.LC_ALL, 'en_US.utf8')\nprint time.strptime(today.strftime(\"%x\"), \"%x\")\n\n\n# fails for 'ja_JP.UTF-8'\nloc = 'ja_JP.UTF-8'\nlocale.setlocale(locale.LC_ALL,loc)\ntry:\n    print today.strftime(\"%x\")\n    print time.strptime(today.strftime(\"%x\"), \"%x\")\nexcept ValueError:\n    print \"failed for %s\" % loc\n\n\nloc = 'ko_KR.UTF-8'\nlocale.setlocale(locale.LC_ALL,loc)\ntry:\n    print today.strftime(\"%x\")\n    print time.strptime(today.strftime(\"%x\"), \"%x\")\nexcept ValueError:\n    print \"failed for %s\" % loc\n<\/code><\/pre>\n<p>And a sample output of this test program on my system (fedora 14, x86_64):<\/p>\n<pre><code>time.struct_time(tm_year=2011, tm_mon=10, tm_mday=10, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=283, tm_isdst=-1)\ntime.struct_time(tm_year=2011, tm_mon=10, tm_mday=10, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=283, tm_isdst=-1)\n2011?10?10?\nfailed for ja_JP.UTF-8\n2011? 10? 10?\nfailed for ko_KR.UTF-8\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Trying to display a localized date string in a text gui widget. The approach I am taking at the moment is to display the localized string with datetime.datetime.strftime(&#8220;%x&#8221;) into a text widget, so that a user can edit it. Then I try to parse the string with time.strptime(&#8220;x&#8221;). The current problem is that for some [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5970","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5970"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5970\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}