Compare last modification time with specfied time I'm writing a function in Python that compares the modification time of a file in Linux (using os.stat) with a specific time and compare the dates. If the modification date is older than the date I specified, an event should happen. 

import os
import time
import datetime

# File Modification time in seconds since epoch
file_mod_time = round(os.stat(file).st_mtime)

# Time in seconds since epoch for time, in which logfile can be unmodified.
t = datetime.datetime.today() - datetime.timedelta(minutes=30)
should_time = round(time.mktime(t.timetuple()))

# Time in minutes since last modification of file
last_time = round((int(time.time()) - file_mod_time) / 60, 2)

if (file_mod_time - should_time) &lt; args.time:
    print "CRITICAL:", file, "last modified", last_time, "minutes. Threshold set to 30 minutes"
else:
    print "OK. Command completed successfully", last_time, "minutes ago."


It is working but I have the feeling that this could be largely improved, because there are so many time conversions. Can you give me a hint on how I can make it better?
I think you were actually on the right path. I really do not concur with the "represent dates with floats" approaches: they force you to add many comments that (1) describe in what units your variables are (seconds, microseconds, whatever) and (2) indicate the reference time (epoch time, etc.). Instead, I recommend to manipulate dates directly: (1) the code is readily understandable, and (2) this allows one to do simple prints of the variables and get obviously meaningful results (useful for debugging and maintaining the code, etc.).

Here is a simple implementation:

import os
from datetime import datetime, timedelta

file_mod_time = datetime.fromtimestamp(os.stat(file_name).st_mtime)  # This is a datetime.datetime object!
now = datetime.today()
max_delay = timedelta(minutes=args.time)

if now-file_mod_time &gt; max_delay:
    print "CRITICAL: {} last modified on {}. Threshold set to {} minutes.".format(file_name, file_mod_time, max_delay.seconds/60)
else:
    print "OK. Command completed successfully {} minutes ago.".format((now-file_mod_time).seconds/60)


(This is not exactly what your original code does, but I'm not sure whether you really intended to use both a delay of 30 minutes and args.time, from the text of the question: I merged them. The code can be easily adapted to your case, were it slightly different.)

This is much more legible. There is also really no need to add many comments about what is being done, as everything is explicit (today(), timedelta(), .seconds,…). No need to do much calculations either, or to round things (int(), round()). You also get a neat date+time printing for free (the printed file_mod_time).

More generally, I would advise to always have variables be of the type of object that they represent. In your case, Python offers a nice date+time class, so I would not never suggest to downgrade dates and time intervals to pure numbers, as their meaning is not obvious (units? time corresponding to number 0?).