[Tutor] date conversion
Brian Gustin
brian at daviesinc.com
Wed Apr 5 16:56:00 CEST 2006
I wrote something similar on my blog some time back..
http://phplogix.com/codefool/?postid=13
This is for converting Apache logfiles to a ISO formatted date for
mysql.. It could easily be reversed, with a little tweaking.. :)
to wit:
def mreplace(s, chararray, newchararray):
for a, b in zip(chararray, newchararray):
s = s.replace(a, b)
return s
olddatestring =
('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')
newdatestring =
('01','02','03','04','05','06','07','08','09','10','11','12')
def clftosql(date):
ttime = date.split(":",1)
time = ttime[1]
datelist = ttime[0].split('/')
#should be , E.G DD,MM,YYYY we need YYYY-MM-DD for sql
day = datelist[0]
month = datelist[1]
year = datelist[2]
newdate = year+'-'+month+'-'+day+' '+time
return newdate
then you can take a date stamp from an Apache CLF log like this :
01/Oct/2000:00:00:00 (after you regex it out of the log with:
datechk =
re.compile('([0-9]+/.../[0-9][0-9][0-9][0-9]:[0-9][0-9]:[0-9][0-9]:[0-9][0-9])')
of course)
and run the functions thus:
mydate = mreplace(dated,olddatestring,newdatestring)
mydate1 = clftosql(mydate)
Ray Allen wrote:
> I would like Python to convert a date returned by MySQL (2006-04-05) to a
> user readable format such as 05-Apr-2006 for display and then to convert it
> back to ISO format for update. What is the most convenient way of doing
> this? I'm struggling to understand the datetime module's functionality.
> Ray Allen
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
> !DSPAM:4433d45e204351006614580!
>
>
More information about the Tutor
mailing list