subtract dates with time module

Discussion in 'Python' started by barronmo, Mar 26, 2008.

  1. barronmo

    barronmo Guest

    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
     
    barronmo, Mar 26, 2008
    #1
    1. Advertising

  2. barronmo wrote:

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


    Use strptime to create time-tuples, and use the fields in there to create
    datetime-objects. Then do the math with them.

    Diez
     
    Diez B. Roggisch, Mar 27, 2008
    #2
    1. Advertising

  3. barronmo

    John Machin Guest

    barronmo wrote:
    > 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?

    If you really want to get to datetime, here's a quick bridge:
    >>> import time, datetime
    >>> def mystrptime(astr, format):

    .... return datetime.datetime(*time.strptime(astr, format)[:6])
    ....
    >>> mystrptime('2008-03-31', '%Y-%m-%d')

    datetime.datetime(2008, 3, 31, 0, 0)


    > 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') -


    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)

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


    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
     
    John Machin, Mar 27, 2008
    #3
  4. barronmo

    barronmo Guest

    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
     
    barronmo, Apr 2, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    894
    Rob Schieber
    Sep 30, 2005
  2. Lutek

    How to subtract dates ?

    Lutek, Jan 4, 2005, in forum: Java
    Replies:
    6
    Views:
    53,088
    pac0rro
    Apr 3, 2009
  3. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    223
    Mark Schupp
    Aug 9, 2004
  4. Ruby Ruby
    Replies:
    4
    Views:
    226
    Osuka Adartse
    Dec 27, 2003
  5. Replies:
    1
    Views:
    229
    Jano Svitok
    Jul 17, 2007
Loading...

Share This Page