Next / Previous / Contents / Shipman's homepage

6.8. getModTime(): Get the file's modification timestamp

docbook5index
# - - -   g e t M o d T i m e

def getModTime(fileName):
    '''Return fileName's modification timestamp.

      [ fileName is a string ->
          if fileName names a readable file ->
            return the modification timestamp of fileName in
            ISO-8601 form
          else ->
            sys.stderr  +:=  error message
            stop execution ]
    '''
    #-- 1
    # [ if fileName names a readable file ->
    #     modEpoch  :=  that file's modification epoch time
    #   else ->
    #     sys.stderr  +:=  error message(s)
    #     stop execution ]
    try:
        status = os.stat(fileName)
        modEpoch = status[stat.ST_MTIME]
    except IOError, details:
        fatal("Can't read '%s': %s." % (fileName, str(details)))

    #-- 2
    # [ local  :=  modEpoch as local time ]
    local = time.localtime(modEpoch)

    #-- 3
    return time.strftime("%Y-%m-%d %H:%M:%S%Z", local)