Discussion:
subtract dates with time module
barronmo
2008-03-26 23:47:45 UTC
Permalink
I'm trying to get the difference in dates using the time module rather
than datetime because I need to use strptime() to convert a date and
then find out how many weeks and days until that date. I'm a beginner
so any help would be appreciated. Here is the code:

def OBweeks(ptID):
qry = 'SELECT short_des FROM problems WHERE patient_ID = %s;' %
(ptID)
results = EMR_utilities.getAllData(qry)
for items in results:
r = re.search('\d\d\d\d-\d\d-\d\d', items)
if r:
d = time.strptime(r.group(), "%Y-%m-%d') -
time.localtime()
weeks, days = divmod(d.days, 7)
return '%s %s/7 weeks' % (weeks, days)

This isn't working. I'm getting "unsupported operand type for -:
'time.struct_time' and 'time.struct_time" error.

Thanks, Mike
Gabriel Genellina
2008-03-27 00:49:18 UTC
Permalink
Post by barronmo
I'm trying to get the difference in dates using the time module rather
than datetime because I need to use strptime() to convert a date and
then find out how many weeks and days until that date. I'm a beginner
qry = 'SELECT short_des FROM problems WHERE patient_ID = %s;' %
(ptID)
results = EMR_utilities.getAllData(qry)
r = re.search('\d\d\d\d-\d\d-\d\d', items)
d = time.strptime(r.group(), "%Y-%m-%d') -
time.localtime()
weeks, days = divmod(d.days, 7)
return '%s %s/7 weeks' % (weeks, days)
'time.struct_time' and 'time.struct_time" error.
I don't see why you don't want to use datetime; it's the easiest way to
compute the time difference.
To obtain the date object dt from the database, you might use the
following, based on your posted code:

import datetime
match = re.search('(\d\d\d\d)-(\d\d)-(\d\d)', items)
if match:
y, m, d = (int(x) for x in match.groups())
dt = datetime.date(y, m, d)

Then, weeks and days are easily computed (similar to your own code):

now = datetime.date.today() # evaluate once outside the loop
delta = now - dt
weeks, days = divmod(delta.days, 7)
--
Gabriel Genellina
Diez B. Roggisch
2008-03-27 09:34:59 UTC
Permalink
Post by barronmo
I'm trying to get the difference in dates using the time module rather
than datetime because I need to use strptime() to convert a date and
then find out how many weeks and days until that date. I'm a beginner
Use strptime to create time-tuples, and use the fields in there to create
datetime-objects. Then do the math with them.

Diez
John Machin
2008-03-27 10:56:29 UTC
Permalink
Post by barronmo
I'm trying to get the difference in dates using the time module rather
than datetime because I need to use strptime() to convert a date and
then find out how many weeks and days until that date.
datetime.datetime.strptime was introduced in Python 2.5; what version
are you using?
Post by barronmo
import time, datetime
... return datetime.datetime(*time.strptime(astr, format)[:6])
...
Post by barronmo
mystrptime('2008-03-31', '%Y-%m-%d')
datetime.datetime(2008, 3, 31, 0, 0)
Post by barronmo
I'm a beginner
qry = 'SELECT short_des FROM problems WHERE patient_ID = %s;' %
(ptID)
results = EMR_utilities.getAllData(qry)
r = re.search('\d\d\d\d-\d\d-\d\d', items)
d = time.strptime(r.group(), "%Y-%m-%d') -
You have " at the start and ' at the end of what's supposed to be a
string constant. That's a syntax error; it won't run. Please don't serve
up what you thought you might have run -- use copy/paste.

In this particular case, you can just use time.mktime to convert a time
tuple into days since the epoch.

# untested
days = time.mktime(time.strptime(r.group(), "%Y-%m-%d")) -
int(time.mktime(time.localtime()))
weeks, days = divmod(days, 7)
Post by barronmo
time.localtime()
weeks, days = divmod(d.days, 7)
return '%s %s/7 weeks' % (weeks, days)
'time.struct_time' and 'time.struct_time" error.
It *is* working. That is the correct result of the code that you
executed. There is is nothing in the time docs to suggest that
attempting to subtract time tuples produces anything useful.

HTH,
John
barronmo
2008-04-02 20:07:30 UTC
Permalink
Thanks for the help everyone. I ended up with the following:

def OBweeks(ptID):
qry = 'SELECT short_des FROM problems WHERE patient_ID = %s;' %
(ptID)
results = EMR_utilities.getAllData(qry)
for items in results:
r = re.search('\d\d\d\d-\d\d-\d\d', str(items))
if r:
edc = datetime.date(*map(int, r.group().split('-')))
pregnancy = datetime.timedelta(weeks=-40)
conception = edc + pregnancy
howfar = datetime.date.today() - conception
weeks, days = divmod(howfar.days, 7)
return '%s %s/7 weeks' % (weeks, days)
else: pass

Mike

Loading...