Re: why not datetime.strptime() ?

Discussion in 'Python' started by Joshua Spoerri, Jan 11, 2005.

  1. Skip Montanaro <skip <at> pobox.com> writes:
    > josh> Shouldn't datetime have strptime?
    > If someone wants to get their feet wet with extension module
    > programming
    > this might be a good place to start. Mostly, I think nobody who has
    > needed/wanted it so far has the round tuits available to spend on the
    > task.


    OK, it was pretty straightforward. Thanks for the direction.

    To whom should I send the patch (attached)?
     
    Joshua Spoerri, Jan 11, 2005
    #1
    1. Advertising

  2. Joshua Spoerri <> writes:

    > Skip Montanaro <skip <at> pobox.com> writes:
    >> josh> Shouldn't datetime have strptime?
    >> If someone wants to get their feet wet with extension module
    >> programming
    >> this might be a good place to start. Mostly, I think nobody who has
    >> needed/wanted it so far has the round tuits available to spend on the
    >> task.

    >
    > OK, it was pretty straightforward. Thanks for the direction.
    >
    > To whom should I send the patch (attached)?


    Submit it to the patch tracker on sourceforge.

    But first, some constructive criticism:

    > --- Modules/datetimemodule.c.orig 2003-10-20 10:34:46.000000000 -0400
    > +++ Modules/datetimemodule.c 2005-01-10 20:58:38.884823296 -0500
    > @@ -3774,6 +3774,32 @@
    > return result;
    > }
    >
    > +/* Return new datetime from time.strptime(). */
    > +static PyObject *
    > +datetime_strptime(PyObject *cls, PyObject *args)
    > +{
    > + PyObject *result = NULL, *obj, *module;
    > + const char *string, *format;
    > +
    > + if (!PyArg_ParseTuple(args, "ss:strptime", &string, &format))
    > + return NULL;
    > + if ((module = PyImport_ImportModule("time")) == NULL)
    > + return NULL;
    > + obj = PyObject_CallMethod(module, "strptime", "ss", string, format);
    > + Py_DECREF(module);


    You don't check for errors: an exception being thrown by
    PyObject_CallMethod will return obj == NULL.

    If there's a module in sys.path called time that overrides the stdlib
    time, things will fail, and you should be able to catch that.

    > + result = PyObject_CallFunction(cls, "iiiiiii",
    > + PyInt_AsLong(PySequence_GetItem(obj, 0)),
    > + PyInt_AsLong(PySequence_GetItem(obj, 1)),
    > + PyInt_AsLong(PySequence_GetItem(obj, 2)),
    > + PyInt_AsLong(PySequence_GetItem(obj, 3)),
    > + PyInt_AsLong(PySequence_GetItem(obj, 4)),
    > + PyInt_AsLong(PySequence_GetItem(obj, 5)),
    > + PyInt_AsLong(PySequence_GetItem(obj, 6)));


    Are you positive those PySequence_GetItem calls will succeed? That
    they will return Python integers?

    > + Py_DECREF(obj);
    > + return result;
    > +}
    > +
    > /* Return new datetime from date/datetime and time arguments. */
    > static PyObject *
    > datetime_combine(PyObject *cls, PyObject *args, PyObject *kw)
    > @@ -4385,6 +4411,11 @@
    > PyDoc_STR("timestamp -> UTC datetime from a POSIX timestamp "
    > "(like time.time()).")},
    >
    > + {"strptime", (PyCFunction)datetime_strptime,
    > + METH_VARARGS | METH_CLASS,
    > + PyDoc_STR("strptime -> new datetime parsed from a string"
    > + "(like time.strptime()).")},
    > +
    > {"combine", (PyCFunction)datetime_combine,
    > METH_VARARGS | METH_KEYWORDS | METH_CLASS,
    > PyDoc_STR("date, time -> datetime with same date and time fields")},


    It probably would help to add some documentation to add to the
    datetime module documentation.

    --
    |>|\/|<
    /--------------------------------------------------------------------------\
    |David M. Cooke
    |cookedm(at)physics(dot)mcmaster(dot)ca
     
    David M. Cooke, Jan 11, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. josh

    why not datetime.strptime() ?

    josh, Jan 7, 2005, in forum: Python
    Replies:
    0
    Views:
    306
  2. Skip Montanaro

    Re: why not datetime.strptime() ?

    Skip Montanaro, Jan 7, 2005, in forum: Python
    Replies:
    1
    Views:
    408
    Lee Harr
    Jan 8, 2005
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,226
    Smokey Grindel
    Dec 2, 2006
  4. kongkolvyu
    Replies:
    4
    Views:
    495
    Old Wolf
    Jan 4, 2006
  5. Tuomas Vesterinen

    Problems with datetime.datetime.strptime

    Tuomas Vesterinen, May 9, 2009, in forum: Python
    Replies:
    0
    Views:
    912
    Tuomas Vesterinen
    May 9, 2009
Loading...

Share This Page