datetime midnight is None - BUG?

27 views
Skip to first unread message

Yebach

unread,
Oct 14, 2016, 5:07:16 AM10/14/16
to web2py-users
Hello

I have a datetime field in db. If I set time to 0:00  - datetime.time(0, 0) the if statement behaves as variable is None

Any ideas why?

Anthony

unread,
Oct 14, 2016, 7:40:42 AM10/14/16
to web2py-users
It does not behave as None but as False (so you can actually test to see if it is None). For details, see https://bugs.python.org/issue13936. At the end of that issue discussion, you will not it was closed with a commit that changed the behavior in Python 3.5 (in 3.5, time(0, 0) evaluates to True). If you are trying to determine if the relevant value is None, just test for that explicitly:

if time_or_none is not None:
    do_something_with
(time_or_none)

Anthony

Yebach

unread,
Oct 14, 2016, 7:58:17 AM10/14/16
to web2py-users
But how to distinguish between empty entry and a value of midnight? 

In my case i have a form with start1, end1 and start2 and end2:
   if start2 exists.
      end2 cannot be empty
   if start1 and end1 and start 2 exist:
      end2 cannot be smaller then start2, except if value 0:00 is inserted

Hope it is clear

Massimo Di Pierro

unread,
Oct 14, 2016, 11:05:41 AM10/14/16
to web2py-users
Anthony answered this question but I will rephrase:

 if not start2 is None:
      end2 cannot be empty
   if not start1 is None and not end1 is None and not start 2 is None:
      end2 cannot be smaller then start2, except if value 0:00 is inserted


Reply all
Reply to author
Forward
0 new messages