date / time

mixo mixo at beth.uniforum.org.za
Wed Apr 3 06:36:09 EST 2002


Tobias Klausmann wrote:

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

Thanks.

By "valid" date I meant subtracting  a day, and  then finding that the
new date does actually exsist.


One note/question on time.gmtime():
+++++++++++
time.gmtime( time.mktime(time.strptime(date,"%Y-%m-%d"))-(24*60*60))
+++++++++++

returns
+++++++++++
(2002, 2, 27, 22, 0, 0, 2, 58, 0)
+++++++++++

There is a day missing. If you add 1 second , i.e ((24*60*60)+1) you get
the required dated. What's going on?




More information about the Python-list mailing list