Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Create datetime instance using a tuple.

Reply
Thread Tools

Create datetime instance using a tuple.

 
 
Negroup
Guest
Posts: n/a
 
      07-06-2005
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.
 
Reply With Quote
 
 
 
 
Qiangning Hong
Guest
Posts: n/a
 
      07-06-2005
On 6 Jul 2005 02:01:55 -0700, Negroup <(E-Mail Removed)> 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>
 
Reply With Quote
 
 
 
 
Dan Bishop
Guest
Posts: n/a
 
      07-06-2005
Qiangning Hong wrote:
> On 6 Jul 2005 02:01:55 -0700, Negroup <(E-Mail Removed)> 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.

 
Reply With Quote
 
Negroup
Guest
Posts: n/a
 
      07-07-2005
>
> 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?
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      07-07-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: [2.4.4] creating a datetime.datetime from an XML xs:dateTime skip@pobox.com Python 2 01-06-2009 01:31 PM
[2.4.4] creating a datetime.datetime from an XML xs:dateTime Martin Python 0 12-27-2008 08:08 PM
mx.DateTime to datetime.datetime mp Python 1 07-28-2006 10:57 PM
datetime: .datetime-.datetime = .timedelta, .time-.time=TypeError ? Christos TZOTZIOY Georgiou Python 3 09-13-2003 10:44 AM
RE: datetime: .datetime-.datetime = .timedelta, .time-.time=TypeError ? Tim Peters Python 0 09-09-2003 12:57 AM



Advertisments