Calculating timespan

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

    Calculating timespan

    I've been looking at the date/time classes and I'm at a loss as to how
    to do this (probably too used to other platforms).

    I have two date/time values. One represents 'now' and the other the last
    modified time of a file on disk (from stat). I need to calculate the
    difference in time (i.e., a 'timespan') between the two so I can tell if
    the file has been modified in the past X minutes and do something to it.

    Thanks =)
  • marek.rocki@wp.pl

    #2
    Re: Calculating timespan

    Erhard napisa³(a):
    I've been looking at the date/time classes and I'm at a loss as to how
    to do this (probably too used to other platforms).
    >
    I have two date/time values. One represents 'now' and the other the last
    modified time of a file on disk (from stat). I need to calculate the
    difference in time (i.e., a 'timespan') between the two so I can tell if
    the file has been modified in the past X minutes and do something to it.
    >
    Thanks =)
    You can subtract one datetime object from another:

    from datetime import datetime, timedelta
    span = datetime.now() -
    datetime(year=2 008,month=8,day =27,hour=12,min ute=34,second=5 6)
    if span < timedelta(minut es=37):
    # do something

    Comment

    • Erhard

      #3
      Re: Calculating timespan

      marek.rocki@wp. pl wrote:
      from datetime import datetime, timedelta
      span = datetime.now() -
      datetime(year=2 008,month=8,day =27,hour=12,min ute=34,second=5 6)
      if span < timedelta(minut es=37):
      # do something
      timedelta! Yes, it's obvious that's what I was looking for. I was stuck
      with 'timespan' in my head and couldn't find anything like it in the docs.

      Thank you very much!

      Comment

      Working...