Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

datetime.time and midnight

62 views
Skip to first unread message

Ethan Furman

unread,
Feb 21, 2009, 5:44:42 PM2/21/09
to pytho...@python.org
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~

MRAB

unread,
Feb 21, 2009, 6:55:23 PM2/21/09
to pytho...@python.org
I think it's because midnight is to the time of day what zero is to
integers, or an empty string is to strings, or an empty container ...

Paddy3118

unread,
Feb 22, 2009, 12:51:10 AM2/22/09
to

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

;-)

- Paddy.

Steven D'Aprano

unread,
Feb 22, 2009, 1:54:41 AM2/22/09
to
Paddy3118 wrote:

> 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*


--
Steven

Gabriel Genellina

unread,
Feb 22, 2009, 2:20:31 AM2/22/09
to pytho...@python.org
En Sat, 21 Feb 2009 21:55:23 -0200, MRAB <goo...@mrabarnett.plus.com>
escribió:

> Ethan Furman wrote:
>> 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.
>>

> I think it's because midnight is to the time of day what zero is to
> integers, or an empty string is to strings, or an empty container ...

So chr(0) should be False too...

--
Gabriel Genellina

Mark Dickinson

unread,
Feb 22, 2009, 4:18:21 AM2/22/09
to
On Feb 21, 10:44 pm, Ethan Furman <et...@stoneleaf.us> wrote:

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

I'd call this a bug.

Mark

Mark Dickinson

unread,
Feb 22, 2009, 4:47:54 AM2/22/09
to

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

Steve Holden

unread,
Feb 22, 2009, 9:08:29 AM2/22/09
to pytho...@python.org
Ethan Furman wrote:
> 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.

One might ask the rationale behind treating a time as Boolean in the
first place. Let me guess: you are retrieving the times from a database,
and you are testing of the NULL value?

If that's the case, a simple "is not None" will overcome the objection.
Though I do agree it's a little weird for bool(midnight) to return
False. Probably worth a bug report. I imagine it's just returning the
bool of the underlying integer.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

D'Arcy J.M. Cain

unread,
Feb 22, 2009, 10:10:19 AM2/22/09
to Gabriel Genellina, pytho...@python.org
On Sun, 22 Feb 2009 05:20:31 -0200
"Gabriel Genellina" <gags...@yahoo.com.ar> wrote:
> En Sat, 21 Feb 2009 21:55:23 -0200, MRAB <goo...@mrabarnett.plus.com>
> escribió:
> > I think it's because midnight is to the time of day what zero is to
> > integers, or an empty string is to strings, or an empty container ...
>
> So chr(0) should be False too...

>>> chr(0)
'\x00'

That's not an empty string. This is like...

>>> bool([0])
True

Now if Python had a char type one might expect the zero char to be
False but a collection of anything, even if the elements are False, is
not empty and hence is True.

As someone else said, there's not much point in casting time to boolean
but if you do, it is a base type, not a sequence so I wouldn't expect
to apply sequence logic to the outcome.

--
D'Arcy J.M. Cain <da...@druid.net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.

MRAB

unread,
Feb 22, 2009, 10:53:59 AM2/22/09
to pytho...@python.org
Gabriel Genellina wrote:
> En Sat, 21 Feb 2009 21:55:23 -0200, MRAB <goo...@mrabarnett.plus.com>
> escribió:
>
>> Ethan Furman wrote:
>>> 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.
>>>
>> I think it's because midnight is to the time of day what zero is to
>> integers, or an empty string is to strings, or an empty container ...
>
> So chr(0) should be False too...
>
If that returned a character, then yes, but it returns a non-empty
string, so no. :-)

Tim Rowe

unread,
Feb 22, 2009, 12:09:29 PM2/22/09
to pytho...@python.org
2009/2/22 Mark Dickinson <dick...@gmail.com>:

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?


--
Tim Rowe

Joshua Judson Rosen

unread,
Feb 22, 2009, 4:06:35 PM2/22/09
to
"D'Arcy J.M. Cain" <da...@druid.net> writes:
>
> On Sun, 22 Feb 2009 05:20:31 -0200
> "Gabriel Genellina" <gags...@yahoo.com.ar> wrote:
> > En Sat, 21 Feb 2009 21:55:23 -0200, MRAB <goo...@mrabarnett.plus.com>
> > escribió:
> > > I think it's because midnight is to the time of day what zero is to
> > > integers, or an empty string is to strings, or an empty container ...
> >
> > So chr(0) should be False too...
>
> >>> chr(0)
> '\x00'
>
> That's not an empty string. This is like...
>
> >>> bool([0])
> True
>
> Now if Python had a char type one might expect the zero char to be
> False but a collection of anything, even if the elements are False, is
> not empty and hence is True.

And, as Halmos said:

A box that contains a hat and nothing else is not the same thing as
a hat....

:)

--
Don't be afraid to ask (Lf.((Lx.xx) (Lr.f(rr)))).

Ethan Furman

unread,
Feb 22, 2009, 4:20:59 PM2/22/09
to pytho...@python.org
Tim Rowe wrote:
> 2009/2/22 Mark Dickinson <dick...@gmail.com>:
> 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.
--
~Ethan~

Joshua Judson Rosen

unread,
Feb 22, 2009, 7:52:25 PM2/22/09
to
Ethan Furman <et...@stoneleaf.us> writes:
>
> [...]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.

While it's not a lack of `time of day', it /is/ a lack of /elapsed/
time in the day ;)

Just as if you were using a plain integer or float to count the time :)

0 new messages