Create datetime instance using a tuple.

Discussion in 'Python' started by Negroup, Jul 6, 2005.

  1. Negroup

    Negroup Guest

    Hi, all.
    I would like to know if it is possible to create a datetime instance
    using a tuple instead of single values.

    I mean:
    >>> from datetime import datetime
    >>> t = (1, 2, 3)
    >>> dt = datetime(t)

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: function takes at least 3 arguments (1 given)

    (class datetime(year, month, day[, hour[, minute[, second[,
    microsecond[, tzinfo]]]]])

    Thanks.
    Negroup, Jul 6, 2005
    #1
    1. Advertising

  2. On 6 Jul 2005 02:01:55 -0700, Negroup <> wrote:
    > Hi, all.
    > I would like to know if it is possible to create a datetime instance
    > using a tuple instead of single values.
    >
    > I mean:
    > >>> from datetime import datetime
    > >>> t = (1, 2, 3)
    > >>> dt = datetime(t)

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > TypeError: function takes at least 3 arguments (1 given)
    >
    > (class datetime(year, month, day[, hour[, minute[, second[,
    > microsecond[, tzinfo]]]]])


    Use:
    dt = datetime(*t)

    --
    Qiangning Hong
    Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&amp;id=67907&amp;t=1>
    Qiangning Hong, Jul 6, 2005
    #2
    1. Advertising

  3. Negroup

    Dan Bishop Guest

    Qiangning Hong wrote:
    > On 6 Jul 2005 02:01:55 -0700, Negroup <> wrote:
    > > Hi, all.
    > > I would like to know if it is possible to create a datetime instance
    > > using a tuple instead of single values.
    > >
    > > I mean:
    > > >>> from datetime import datetime
    > > >>> t = (1, 2, 3)
    > > >>> dt = datetime(t)

    > > Traceback (most recent call last):
    > > File "<stdin>", line 1, in ?
    > > TypeError: function takes at least 3 arguments (1 given)
    > >
    > > (class datetime(year, month, day[, hour[, minute[, second[,
    > > microsecond[, tzinfo]]]]])

    >
    > Use:
    > dt = datetime(*t)


    It's better to write:

    dt = datetime(*t[:6])

    This gives you compatibility with the (year, month, day, hour, minute,
    second, weekday, julian_day, dst) tuples returned by time.gmtime,
    time.localtime, and time.strptime.
    Dan Bishop, Jul 6, 2005
    #3
  4. Negroup

    Negroup Guest

    >
    > Use:
    > dt =3D datetime(*t)
    >


    Thanks for the quick reply.
    I can't find any doc about '*' used in this context. Have you some url
    or suggestion for which terms search in Google?
    Negroup, Jul 7, 2005
    #4
  5. Negroup

    John Machin Guest

    Negroup wrote:
    >>Use:
    >>dt =3D datetime(*t)
    >>

    >
    >
    > Thanks for the quick reply.
    > I can't find any doc about '*' used in this context. Have you some url
    > or suggestion for which terms search in Google?


    http://www.python.org/doc/2.4.1/tut/tut.html

    Then read this section

    4.7.4 Unpacking Argument Lists

    Then read all the other sections :)
    John Machin, Jul 7, 2005
    #5
    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. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    702
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    529
    Tim Peters
    Sep 9, 2003
  3. mp
    Replies:
    1
    Views:
    385
    John Machin
    Jul 28, 2006
  4. Martin
    Replies:
    0
    Views:
    333
    Martin
    Dec 27, 2008
  5. Replies:
    2
    Views:
    752
    M.-A. Lemburg
    Jan 6, 2009
Loading...

Share This Page