Time and date operation

Chris Rebert clp2 at rebertia.com
Tue Nov 23 13:50:19 EST 2010


> -----Original Message-----
> From: clp2 at rebertia.com [mailto: clp2 at rebertia.com]
> Sent: 2010年11月23日 19:12
> To: huisky
> Cc: python-list at python.org
> Subject: Re: Time and date operation
>
> On Tue, Nov 23, 2010 at 9:47 AM, huisky <huisky at gmail.com> wrote:
>> Hi everyone,
>>
>> Could you anybody shed lights to me? Say I have two dics.
>>>>> cstart
>> defaultdict(<type 'int'>, {15424: ['Dec', '6', '18:57:40'], 552:
>> ['Dec', '7', '09:31:00'], 15500: ['Dec', '6', '20:17:02'], 18863:
>> ['Dec', '7', '13:14:47'], 18291: ['Dec', '6', '21:01:17'], 18969:
>> ['Dec', '7', '14:28:42'], 18937: ['Dec', '7', '14:21:34']})
>>>>> ccompl
>> defaultdict(<type 'int'>, {15424: ['Dec', '6', '19:42:55'], 18291:
>> ['Dec', '6', '21:01:28'], 15500: ['Dec', '6', '20:26:03'], 18863:
>> ['Dec', '7', '13:24:07']})
>>
>> and I need to calculate the difference time if the key value is the
>> same in both dics.
>
> Create datetime.datetime objects. Subtract one from another and you'll
> get a datetime.timedelta object representing the difference between
> them.
> The fine manual: http://docs.python.org/library/datetime.html#datetime-objects
>
On Tue, Nov 23, 2010 at 10:42 AM, Huisky <huisky at gmail.com> wrote:
> Hi,
>
> Thanks. I'm wondering is datetime.datetime objects able to read 'Dec', for example?

Yes. Use the %b format directive when calling datetime.datetime.strptime().
http://docs.python.org/library/datetime.html#datetime.datetime.strptime
http://docs.python.org/library/time.html#time.strftime

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list