How can time be compared

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dave Elfers
    New Member
    • Aug 2010
    • 9

    How can time be compared

    Hello,

    I am writing a script that is going to download and read XML data from a server. The server updates the xml data every 4 hours. Instead of downloading a new copy of the xml everytime the script runs, I want to look at the timestamp of the local file to see if it is less than 4 hours old. If it is, I will read the local copy, but if not I will download a new copy and read it. I can't seem to find any good way of doing this. Any help would be appreciated.
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    Try os.stat() and use either ctime, mtime, or stime, depending on your requirements http://docs.python.org/library/os.html. Note that all times must be in the same units, seconds since the epoch, or datetime format.

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Here's a simple example using os.path.getmtim e and time.time:
      Code:
      import time, os
      
      def mtime_diff(fn):
          '''
          Return the difference between the current time and the time of
          last modification of a given file name in seconds.'''
          return time.time() - os.path.getmtime(fn)
      
      diff = mtime_diff('file_name')
      fourhours = 60*60*4.
      print "The file was last modified %.2f hours ago." % (diff/fourhours)

      Comment

      • Dave Elfers
        New Member
        • Aug 2010
        • 9

        #4
        thank you sir. That did the trick!

        Comment

        Working...