subtract dates with time module

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • barronmo

    #1

    subtract dates with time module

    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.g etAllData(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 "unsupporte d operand type for -:
    'time.struct_ti me' and 'time.struct_ti me" error.

    Thanks, Mike
  • Diez B. Roggisch

    #2
    Re: subtract dates with time module

    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

    Comment

    • John Machin

      #3
      Re: subtract dates with time module

      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.dateti me.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.dateti me(*time.strpti me(astr, format)[:6])
      ....
      >>mystrptime('2 008-03-31', '%Y-%m-%d')
      datetime.dateti me(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.g etAllData(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(tim e.strptime(r.gr oup(), "%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 "unsupporte d operand type for -:
      'time.struct_ti me' and 'time.struct_ti me" 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

      Comment

      Working...