views:

112

answers:

5

I want to trap values that are like this (in which there is not 'time info' on the datetime):

datetime.datetime(2009, 4, 6, 0, 0)

Is there a better way to detect these values other than testing hour/minute/second?

if value.hour == 0 and value.minute == 0 and value.second == 0:
     # do stuff
+1  A: 

I see nothing wrong with your method, but you could compare it to a 'zeroed' time object.

someDateTime = datetime.datetime(2009, 4, 6, 0, 0)
someDateTime.time() == datetime.time(0)
Mark
A: 

Another option:

if not (value.hour or value.minute or value.second):
    # do stuff
Ned Batchelder
Even if both your alternative and 'if not value.hour and not value.minute and not value.second' do the same, I find the latter expresses the intended behavior more clearly.
Adriano Varoli Piazza
+1  A: 

You would actually need to check microsecond as well. Another option:

NO_TIME = datetime.time(0) # can be stored as constant

if (value.time() == NO_TIME):
   # do stuff
Kathy Van Stone
+3  A: 

don't neglect that HMS==0 for 1 in 86,400 fully completed time values! depending on what you're doing, that might not be negligible.

Dustin Getz
Totally aware of that, and in the application there should never be an instance where that would be a valid input value (famous last words).
T. Stone
+6  A: 

The time method works here. Evaluates as boolean false if there's zero'd-out time info.

if not value.time():
    # do stuff
hanksims
mmm that's clean
T. Stone