datetime.fromstr() - how?

M

Martin Bless

Below is what I'm currently using to construct datatime objects from
strings. Date and time objects are made up similar. To and from string
conversions are frequently needed in SQL neighborhood.

(1) Is there reason enough to ask for a 'datetime.fromstr(...)'
method? At least to convert ISO timestamps?

(2) You've got a better/different idea? Glad to see. Don't let me die
with this open question ... ;-)

mb - Martin


import datetime,time

class mysql_date(datetime.date):
"Represent and convert 'YYYY-MM-DD'-like date values."

def fromstr(Cls,s,f='%Y-%m-%d'):
try:
return Cls(*time.strptime(s,f)[:3])
except:
return None
fromstr = classmethod(fromstr)

dt = mysql_date.fromstr('2004-07-20')
print repr(dt)
print dt

""" should print:
mysql_date(2004, 7, 20)
2004-07-20
"""
 
J

Jeff Sandys

Martin said:
Below is what I'm currently using to construct datatime objects from
strings. Date and time objects are made up similar. To and from string
conversions are frequently needed in SQL neighborhood.

(1) Is there reason enough to ask for a 'datetime.fromstr(...)'
method? At least to convert ISO timestamps?
Does this help?
http://www.python.org/cgi-bin/moinmoin/WorkingWithTime
(2) You've got a better/different idea? Glad to see. Don't let me die
with this open question ... ;-)

mb - Martin

import datetime,time

class mysql_date(datetime.date):
"Represent and convert 'YYYY-MM-DD'-like date values."

def fromstr(Cls,s,f='%Y-%m-%d'):
try:
return Cls(*time.strptime(s,f)[:3])
except:
return None
fromstr = classmethod(fromstr)

dt = mysql_date.fromstr('2004-07-20')
print repr(dt)
print dt

""" should print:
mysql_date(2004, 7, 20)
2004-07-20
"""
 
D

Dan Bishop

Below is what I'm currently using to construct datatime objects from
strings. Date and time objects are made up similar. To and from string
conversions are frequently needed in SQL neighborhood.

(1) Is there reason enough to ask for a 'datetime.fromstr(...)'
method? At least to convert ISO timestamps?

(2) You've got a better/different idea? Glad to see. Don't let me die
with this open question ... ;-)

import datetime
def strToDate(ymd):
return datetime.date(*map(int, ymd.split('-')))
 
M

Martin Bless

Dan Bishop <> wrote in
import datetime
def strToDate(ymd):
return datetime.date(*map(int, ymd.split('-')))

Yes, thanks, I like your solution better. This way I don't have to use
'time' as a second module. For those interested, here's what I'm using
now.

It's a pity the 'datetime' module lacks 'time.strptime()'
functionality. In contrast it does provide 'strftime()' methods.

mb - Martin


# date,time,datetime,timestamp handling for MySQLdb
# independant from mxdate

class mysql_date(datetime.date):
"Represent and convert 'YYYY-MM-DD'-like date values."

def fromstr(Cls,s):
try:
return Cls(*map(int,s.split('-'))[:3])
except:
return None
fromstr = classmethod(fromstr)

class mysql_time(datetime.time):
"Represent and convert 'HH:MM:SS'-like time values."

def fromstr(Cls,s):
"Construct instance from string."
try:
return Cls(*map(int,s.split(':'))[:3])
except:
return None
fromstr = classmethod(fromstr)

class mysql_datetime(datetime.datetime):
"Represent and convert 'YYYY-MM-DD HH:MM:SS'-like datetime
values."

def fromstr(Cls,s):
"Initialize instance from string."
try:
return Cls(*map(int,s.replace('-',' ').replace(':','
').split())[:6])
except:
return None
fromstr = classmethod(fromstr)

class mysql_timestamp(datetime.datetime):
"Represent and convert 'YYYYMMDDHHMMSS'-like timestamp values."

def fromstr(Cls,s,f='%Y%m%d%H%M%S'):
"Initialize instance from string."
try:
return
Cls(*map(int,[s[0:4],s[4:6],s[6:8],s[8:10],s[10:12],s[12:14]]))
except:
return None
fromstr = classmethod(fromstr)

def __str__(self):
return self.strftime('%Y%m%d%H%M%S')

myconv = MySQLdb.converters.conversions.copy()
myconv.update({
FIELD_TYPE.TIMESTAMP : mysql_timestamp.fromstr,
FIELD_TYPE.DATETIME : mysql_datetime.fromstr,
FIELD_TYPE.DATE : mysql_date.fromstr,
FIELD_TYPE.TIME : mysql_time.fromstr,
})
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top