how to set default value as current time in TimeField

4,584 views
Skip to first unread message

harryos

unread,
Feb 16, 2010, 2:13:45 AM2/16/10
to Django users

hi
I am using a TimeField and want to set the default value as current
time.I know the field normalizes to a datetime.time object.In a
DateField ,I can put (default=datetime.date.today) and this will set
the current day .Similarly I tried (default=datetime.now().time) for
TimeField ..and can get a default time value when the page is
loaded.But after waiting for a couple of minutes,I loaded the page
again(without restarting the server) and the timevalue shown was the
old one ,not current time.
While using the django admin I can set the time using the javascript
clock,but that is not an option when I use my own (beginner
level)code..Can anyone tell me how I can get the current time as
default value in the field?

Also ,how do I get the duration between two datetime.time objects ?I
tried to create two instances of these and can compare them using > or
< ,but found that - operation is not supported.Any tips are most
welcome

thanks

harry

Masklinn

unread,
Feb 16, 2010, 2:37:58 AM2/16/10
to django...@googlegroups.com
On 16 Feb 2010, at 08:13 , harryos wrote:
>
> hi
> I am using a TimeField and want to set the default value as current
> time.I know the field normalizes to a datetime.time object.In a
> DateField ,I can put (default=datetime.date.today) and this will set
> the current day .Similarly I tried (default=datetime.now().time) for
> TimeField ..and can get a default time value when the page is
> loaded.But after waiting for a couple of minutes,I loaded the page
> again(without restarting the server) and the timevalue shown was the
> old one ,not current time.

Argument are only evaluated once, so `default=datetime.now().time` sets the default to the `datetime.now().time` as it is when evaluated. Once.

Just wrap the thing in a `lambda` and you should be good to go: `default=(lambda:datetime.now().time)`

harryos

unread,
Feb 16, 2010, 2:51:25 AM2/16/10
to Django users
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

harry

Masklinn

unread,
Feb 16, 2010, 2:52:18 AM2/16/10
to django...@googlegroups.com


*defaults*, not arguments of course.

Masklinn

unread,
Feb 16, 2010, 3:00:08 AM2/16/10
to django...@googlegroups.com
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)

Matt Schinckel

unread,
Feb 16, 2010, 7:29:23 AM2/16/10
to Django users

You don't even need to do this. Just remove the () from
datetime.now(), and it will do what you want it to.

If you pass in a callable as the default, this will be called each
time the object is created.

Matt.

Matt Schinckel

unread,
Feb 16, 2010, 7:43:01 AM2/16/10
to Django users

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].

Masklinn

unread,
Feb 16, 2010, 7:43:26 AM2/16/10
to django...@googlegroups.com

Except in this case he wants a `time` object, not a `datetime` one. Are you sure Django handles the coercion/conversion from `datetime` to `time`?

Matt Schinckel

unread,
Feb 16, 2010, 3:56:58 PM2/16/10
to Django users

Sorry, my mistake. For some reason I thought there was a
datetime.time.now() method.

The lambda is the best solution.

Reply all
Reply to author
Forward
0 new messages