[Tutor] cookie expiration date format

Luke Paireepinart rabidpoobear at gmail.com
Sat Mar 17 03:31:27 CET 2007


Tim Johnson wrote:
> Hi:
> I want to be able to expire a cookie programmatically.
> In other languages, I just set the expiration date to 'yesterday'.
> If I look at the documentation at:
> http://docs.python.org/lib/node644.html
> for the Cookie object, I see the following:
> -----------------------------------------------------------
> expires
>     Integer expiry date in seconds since epoch,
> -----------------------------------------------------------
> I'm not clear what datatype is needed here.
> Can anyone clarify this for me?
>   
Sounds like it's an integer or float, such as returned by time.time()
 >>> import time
 >>> time.time()
1174098190.796 #seconds since epoch
 >>> _ / 60
19568303.179933332#minutes since epoch
 >>> _ / 60
326138.3863322222 #hours ..
 >>> _ / 24
13589.099430509259# days
 >>> _ / 365.25
37.204926572236161 #years
 >>> .205 * 1.2
0.24599999999999997  #months ( fractional part of year )
 >>>

So today is 37 years 2.5 months from January 1, 1970.
1970 + 37 = 2007, and January 1 + 2.5 months = March 16.

If you wanted the cookie to expire 24 hours from now,
time.time() + 3600 * 24 #(seconds in a day)

See http://en.wikipedia.org/wiki/Unix_epoch for more info on the epoch.
look into the time and the datetime modules, there should be an easy way 
to find the seconds since epoch for whatever date you want.
HTH,
-Luke


More information about the Tutor mailing list