datetime.time and midnight

E

Ethan Furman

Greetings, List!

I was curious if anyone knew the rationale behind making midnight False?

--> import datetime
--> midnight = datetime.time(0,0,0)
--> bool(midnight)
False

To my way of thinking, midnight does actually exist so it should be
true. If datetime.time was measuring an *amount* of time, rather than a
certain point during the day, then a time of 0:0:0 should certainly be
False as it would mean no time had passed. However, since midnight does
indeed exist (as many programmers have observed when deadlines approach
;) I would think it should be true.
 
P

Paddy3118

Greetings, List!

I was curious if anyone knew the rationale behind making midnight False?

--> import datetime
--> midnight = datetime.time(0,0,0)
--> bool(midnight)
False

To my way of thinking, midnight does actually exist so it should be
true.  If datetime.time was measuring an *amount* of time, rather than a
certain point during the day, then a time of 0:0:0 should certainly be
False as it would mean no time had passed.  However, since midnight does
indeed exist (as many programmers have observed when deadlines approach
;) I would think it should be true.

Ethan,
Knights are true and seek the light. Evil trolls seek the night and so
their hour is false.

;-)

- Paddy.
 
S

Steven D'Aprano

Paddy3118 said:
Ethan,
Knights are true and seek the light. Evil trolls seek the night and so
their hour is false.

;-)

That's speciest *and* lightist. There's nothing wrong with avoiding the evil
burning day star, that's practically de rigour for programmers.


*wink*
 
M

Mark Dickinson

I'd call this a bug.

....although looking at the source (see the function
time_nonzero in Modules/datetimemodule.c), this
behaviour is clearly intentional. So it's not a bug;
merely a questionable design decision. :)

Mark
 
T

Tim Rowe

2009/2/22 Mark Dickinson said:
I'd call this a bug.

No more so than zero being false. Zero exists too (check my bank
balance). Once you've accepted non-Boolean types having Boolean
values, the logic of what value they have is always going to be a bit
hairy. If you're unsure of the logic, just test against a value or a
range. After all, what value do you *expect* a time of day to have
when interpreted as a Boolean? If you don't have an expectation, why
are you interpreting it as a Boolean?
 
E

Ethan Furman

Tim said:
No more so than zero being false. Zero exists too (check my bank
balance). Once you've accepted non-Boolean types having Boolean
values, the logic of what value they have is always going to be a bit
hairy. If you're unsure of the logic, just test against a value or a
range. After all, what value do you *expect* a time of day to have
when interpreted as a Boolean? If you don't have an expectation, why
are you interpreting it as a Boolean?
As Steve Holden correctly guessed, my use case is with databases (more
accurately, with dBase and VFP dbf files). As I was creating a dbf
date/datetime/time wrapper for the existing datetime data types I
noticed that it is *not* possible to have a false date, nor a false
date/time, so I was surprised with the false time.

So partly because of dates and datetimes, partly because objects are
true unless special effort is made to have them be false, and partly
because midnight is in fact a time of day, and not a lack of a time of
day, I do indeed expect it to be True.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top