470,604 Members | 2,121 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,604 developers. It's quick & easy.

datetime.fromstr() - how?

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
"""

Jul 18 '05 #1
3 4002
Martin Bless wrote:

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
"""

Jul 18 '05 #2
m.*****@gmx.de (Martin Bless) wrote in message news:<40***************@news.versatel.de>...
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('-')))
Jul 18 '05 #3
Dan Bishop <> wrote in
<ad**************************@posting.google.com >:

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,
})

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Max M | last post: by
15 posts views Thread by Fritz Switzer | last post: by
3 posts views Thread by Andrew S. Giles | last post: by
6 posts views Thread by Ante Perkovic | last post: by
5 posts views Thread by I am Sam | last post: by
11 posts views Thread by Cor Ligthert | last post: by
9 posts views Thread by Phil B | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.