date / time

Tobias Klausmann klausman-un070202 at tuts.net
Wed Apr 3 05:34:31 EST 2002


mixo <mixo at beth.uniforum.org.za> wrote:
> 
> Suppose there is a given date, 2002-03-01 (YYYY-MM-DD).
> How can you get the day (2002-02-28 ) before the given date and
> check that it is valid?

(I replace the interpreters >>> prompt with ### for better
 legibility in USENET posts)

### import time
### date="2002-03-01"
### time.strptime(date,"%Y-%m-%d")
(2002, 3, 1, 0, 0, 0, 4, 60, 0)
### time.mktime(time.strptime(date,"%Y-%m-%d"))
1014937200.0
### time.mktime(time.strptime(date,"%Y-%m-%d"))-(24*60*60)
1014850800.0
### time.ctime(time.mktime(time.strptime(date,"%Y-%m-%d"))-(24*60*60))
'Thu Feb 28 00:00:00 2002'
###

Just remember that time.ctime() will give you local time
while time.gmtime() will give you UTC. The input date is
assumed to be the local time. If you drift between TZs, make
use of the time.tzname variable.

By the way: what do you mean by "valid"?

Greets/HTH,
Tobias




More information about the Python-list mailing list