mktime and tupples

M.-A. Lemburg mal at lemburg.com
Thu Oct 28 04:38:46 EDT 1999


Rico Albanese wrote:
> 
> Hi everyone,
> 
> I am writing a web application and I am trying to get a string which
> represents a date (eg. 16071999) and generate from this the
> corresponding Unix Time in secs from Epoch.  So with the following code
> I am trying to generate a tuple which is suitable to pass to
> time.mktime() as a parameter.  I have imported all of the required
> modules but the following code bombs out at the line "thetime=...".  Are
> the variables y, m, d strings? How do I create a tuple (ie. variable
> "atime" below) so that it is a valid parameter for time.mktime()?  Here
> is my code :
> 
> yr=re.search('[0-9]{1,4}$',c_date)  #pull year from 16071999 eg. 1999
> dymnth=re.search('^[0-9]{1,4}',c_date)  #pull out day/month from
> 16071999 eg. 1607
> daymonth=dymnth.group()
> dy=re.search('^[0-9]{1,2}',daymonth)  #pull out day from 16071999 eg. 16
> 
> mnth=re.search('[0-9]{1,2}$',daymonth)  #pull out month from 16071999
> eg. 07
> y=yr.group()
> m=mnth.group()
> d=dy.group()
> atime=(y,m,d,0,0,0,0,0,0)
> thetime=time.mktime(atime)

How about using time.strptime() instead ?

thetime = time.strptime('16071999','%d%m%Y')

You'll find more on date/time with Python in:

	http://starship.skyport.net/~lemburg/mxDateTime.html

-- 
Marc-Andre Lemburg
______________________________________________________________________
Y2000:                                                    64 days left
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/






More information about the Python-list mailing list