On Feb 16, 6:00 pm, Masklinn <maskl...@masklinn.net> wrote:
> On 16 Feb 2010, at 08:51 , harryos wrote:
>
> > thanks ,that worked..
>
> > any idea about calculating the duration? I can do a - between two
> > datetime.datetime objects to get a timedelta.. but that doesn't work
> > with datetime.time objects
>
> Nope, seems datetime.time doesn't define __add__ and __sub__. Apparently, 
> only datetime.datetime and datetime.date support timedelta-type operations 
> (which kind-of makes sense, as timedeltas can span days).
>
> You have the option of converting to a datetime via time.strftime and 
> datetime.strptime, even though it's ugly and probably quite inefficient:
>
>     >>> t
>     datetime.time(8, 56, 20, 653330)
>     >>> datetime.strptime(t.strftime('%H:%M:%S'), '%H:%M:%S')
>     datetime.datetime(1900, 1, 1, 8, 56, 20)

Or use datetime.combine to combine both time objects with the same
date, and then add/subtract.

As Masklinn says, you hit overflow very easily with addition and
subtraction of time objects/timedeltas.  For instance, even with a
short timedelta:

datetime.time(20,0) + datetime.timedelta(hours=3)

What is the value of this expression? datetime.time cannot exceed
datetime.time(23,59,59). Does it tick over to (0,0,0) again.

Sorry, bad pun.

[I actually have a use case similar to this - where the values I store
are open/close times. Sometimes, the close time can be earlier than
the open time, if the shop is open past midnight. It gets messy
quickly].

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to