Mailing List Archive

Check for valid date
Hi, I'm trying to use the time module to check whether a date is
actually a valid day or not.
Unfortunately, it looks like the strptime function only performs a basic
range check. (for example, it will accept 02/30/199)
Here is a little test I did:

>>> strptime('02/30/1999','%m/%d/%Y)
(1999, 2, 30, 0, 0, 0, 61, 0)
>>> mktime((1999, 2, 30, 0, 0, 0, 61, 0))
920361600.0
>>> ctime(920361600.0)
'Tue Mar 2 00:00:00 1999'

Is there a way to check test whether the day actually exists or not ?


I also have a totally different problem : I'm trying to get python 1.5.2
to run with Tcl/Tk 8.1. Has anyone been able to do that on AIX ?
I found 2 patches for tkinter that were supposed to fix the problems,
but unfortunately, they don't work for AIX.

Thanks

Remi.


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
Check for valid date [ In reply to ]
On Mon, Jun 21, 1999 at 04:44:20PM +0000, Remi Delon wrote:
> From: Remi Delon <rdelon@my-deja.com>
> Newsgroups: comp.lang.python
> Subject: Check for valid date
> Date: Mon, 21 Jun 1999 16:44:20 GMT
> To: python-list@python.org
>
> Hi, I'm trying to use the time module to check whether a date is
> actually a valid day or not.
> Unfortunately, it looks like the strptime function only performs a basic
> range check. (for example, it will accept 02/30/199)
> Here is a little test I did:
>
> >>> strptime('02/30/1999','%m/%d/%Y)
> (1999, 2, 30, 0, 0, 0, 61, 0)

Right. strptime does what it has to do, it parses the given string.

> >>> mktime((1999, 2, 30, 0, 0, 0, 61, 0))
> 920361600.0

But this should raise an error, in my opinion. Hmm... what about
DateError?

> >>> ctime(920361600.0)
> 'Tue Mar 2 00:00:00 1999'
>
> Is there a way to check test whether the day actually exists or not ?
>
groeten,
Gerrit.

--
The Dutch Linuxgames homepage: http://linuxgames.nl.linux.org
Personal homepage: http://www.nl.linux.org/~gerrit/

Discoverb is a python program in Dutch and English which tests the words you
learned by asking it. Homepage: http://www.nl.linux.org/~gerrit/discoverb/
Check for valid date [ In reply to ]
you could try making sure that

strftime(your_format, strptime(date2check, your_format)) == date2check.

and checking for OverflowError & ValueError in the process.

scott



On Mon, Jun 21, 1999 at 04:44:20PM +0000, Remi Delon wrote:
| Hi, I'm trying to use the time module to check whether a date is
| actually a valid day or not.
| Unfortunately, it looks like the strptime function only performs a basic
| range check. (for example, it will accept 02/30/199)
| Here is a little test I did:
|
| >>> strptime('02/30/1999','%m/%d/%Y)
| (1999, 2, 30, 0, 0, 0, 61, 0)
| >>> mktime((1999, 2, 30, 0, 0, 0, 61, 0))
| 920361600.0
| >>> ctime(920361600.0)
| 'Tue Mar 2 00:00:00 1999'
|
| Is there a way to check test whether the day actually exists or not ?
|
|
| I also have a totally different problem : I'm trying to get python 1.5.2
| to run with Tcl/Tk 8.1. Has anyone been able to do that on AIX ?
| I found 2 patches for tkinter that were supposed to fix the problems,
| but unfortunately, they don't work for AIX.
|
| Thanks
|
| Remi.
|
|
| Sent via Deja.com http://www.deja.com/
| Share what you know. Learn what you don't.
|
| --
| http://www.python.org/mailman/listinfo/python-list
Check for valid date [ In reply to ]
Hi Remi,

I use a two-step procedure:

>>>def check_date(year, month, day):
... tup1 = (year, month, day, 0,0,0,0,0,0)
... try:
... date = time.mktime (tup1)
... tup2 = time.localtime (date)
... if tup1[:2] != tup2[:2]:
... return "Date not valid."
... else:
... return "Date valid."
... except OverflowError:
... return "Date not valid."
...
>>> import time
>>> print date_valid(1999,2,29)
Date not valid.
>>> print date_valid(1999,2,28)
Date valid.

Paolo


Remi Delon wrote:
>
> Hi, I'm trying to use the time module to check whether a date is
> actually a valid day or not.
> Unfortunately, it looks like the strptime function only performs a basic
> range check. (for example, it will accept 02/30/199)
> Here is a little test I did:
>
> >>> strptime('02/30/1999','%m/%d/%Y)
> (1999, 2, 30, 0, 0, 0, 61, 0)
> >>> mktime((1999, 2, 30, 0, 0, 0, 61, 0))
> 920361600.0
> >>> ctime(920361600.0)
> 'Tue Mar 2 00:00:00 1999'
>
> Is there a way to check test whether the day actually exists or not ?
>
> I also have a totally different problem : I'm trying to get python 1.5.2
> to run with Tcl/Tk 8.1. Has anyone been able to do that on AIX ?
> I found 2 patches for tkinter that were supposed to fix the problems,
> but unfortunately, they don't work for AIX.
>
> Thanks
>
> Remi.
>
> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.

--
/* Paolo G. Cantore paolo.cantore@basf-ag.de */
/* BASF AG ZOI/AP Zentralabteilung Informatik */
/* D-67056 Ludwigshafen Tel. +49-621-60-43987 */
/* Germany Fax. +49-621-60-20833 */
Check for valid date [ In reply to ]
On Tue, Jun 22, 1999 at 02:07:38PM +0200, Paolo G. Cantore wrote:
> From: "Paolo G. Cantore" <paolo.cantore@zxa.basf-ag.de>
> Newsgroups: comp.lang.python
> Subject: Re: Check for valid date
> Date: Tue, 22 Jun 1999 14:07:38 +0200
> To: python-list@python.org
>
> Hi Remi,
>
> I use a two-step procedure:
>
> >>>def check_date(year, month, day):
> ... tup1 = (year, month, day, 0,0,0,0,0,0)
> ... try:
> ... date = time.mktime (tup1)
> ... tup2 = time.localtime (date)
> ... if tup1[:2] != tup2[:2]:
> ... return "Date not valid."
> ... else:
> ... return "Date valid."
> ... except OverflowError:
> ... return "Date not valid."
> ...
> >>> import time
> >>> print date_valid(1999,2,29)
> Date not valid.
> >>> print date_valid(1999,2,28)
> Date valid.
>

I would return 0 or 1, instead of what you're doing.

groeten,
Gerrit.

--
The Dutch Linuxgames homepage: http://linuxgames.nl.linux.org
Personal homepage: http://www.nl.linux.org/~gerrit/

Discoverb is a python program in Dutch and English which tests the words you
learned by asking it. Homepage: http://www.nl.linux.org/~gerrit/discoverb/