Alternative to strptime()?

Remco Gerlich scarblac-spamtrap at pino.selwerd.nl
Tue Sep 12 03:53:59 EDT 2000


Mio Nino Marquez wrote in comp.lang.python:
> First, thanks for the help.
> However, we still couldn't figure out how to do it. <g>
> 
> Here's our code snippet:
> 
> import rfc822, time
> 
> sDate = '2000-12-20 00:00:00' #date the MSSQL db is giving
> dTime = rfc822.parsedate(sDate) #request for a 9-tuple
> dtuple = time.strftime('%Y%m%d', dTime) #we'd like to have the date
> formatted as yyyymmdd
> return dtuple

Can't you just hack that up by hand, if the format returned by MSSQL is always
that?

Something like

import string # Or use string methods in 1.6 and up

sDate = '2000-12-20 00:00:00' # Date from MSSQL
date_part = string.split(sDate)[0]
year, month, day = map(int, string.split(date_part, '-'))
return "%04d%02d%02d" % (year, month, day)

It will bug when sDate's format changes, but so would a strptime solution...

If you really need the tuple format:

def mssql_to_timetuple(s):
   import time, string
   Date, Time = string.split(s)
   year, month, day = map(int, string.split(Date, '-'))
   hour, minute, sec = map(int, string.split(Time, ':'))
   return time.localtime(time.mktime(
       (year, month, day, hour, minute, sec, 0, 0, -1)))

-- 
Remco Gerlich,  scarblac at pino.selwerd.nl
Hi! I'm a .sig virus! Join the fun and copy me into yours!



More information about the Python-list mailing list