[IronPython] Bug in time.strptime?

Snaury snaury at gmail.com
Mon Jul 17 20:18:22 CEST 2006


Apparently, after I tried to create a patch for this, I suddenly found
that not only quoting of user text was the problem. Format for %d also
was another problem, it seems it should be %x (otherwise ParseExact is
looking for two numbers and fails).

--- time.cs.orig	Fri Jul  7 09:02:28 2006
+++ time.cs	Mon Jul 17 22:15:20 2006
@@ -170,7 +170,10 @@
             } else {
                 string[] formats = new string[formatInfo.Count];
                 for (int i = 0; i < formatInfo.Count; i++) {
-                    formats[i] = formatInfo[i].Text;
+                    if(formatInfo[i].Type == FormatInfoType.UserText) {
+                        formats[i] = "'" + formatInfo[i].Text + "'";
+                    } else
+                        formats[i] = formatInfo[i].Text;
                 }

                 if (!DateTime.TryParseExact(@string, String.Join("",
formats), null, DateTimeStyles.AllowWhiteSpaces, out res)) {

>>> import System
>>> System.Threading.Thread.CurrentThread.CurrentCulture =
System.Globalization.CultureInfo("en-US")
>>> import time, datetime
>>> d = time.strptime("July 3, 2006 At 0724 GMT", "%B %x, %Y At %H%M GMT")
>>> print d
7/3/2006 7:24:00 AM

On 7/17/06, Snaury <snaury at gmail.com> wrote:
> On 7/17/06, Snaury <snaury at gmail.com> wrote:
> > Hi Dino,
> >
> > It seems that you just need to use DateTime.ParseExact and wrap those
> > random characters in single quotes and then it parses successfully. At
> > least I did it in the clr way and it worked:
> >
> > import System
> > System.DateTime.ParseExact("July 3, 2006 At 0724 GMT", "MMMM d, y 'At'
> > HHmm 'GMT'", System.Globalization.CultureInfo("en-US"))
>
> Oh, that should read:
>
> import System
> System.DateTime.ParseExact("July 3, 2006 At 0724 GMT", "MMMM d, yyyy
> 'At' HHmm 'GMT'", System.Globalization.CultureInfo("en-US"))



More information about the Ironpython-users mailing list