Reputation: 2172
I am writing a program in Python that requires comparison of atime, mtime, and ctime of several directories. For this purpose I am using os.stat("my_directory/")
. What I get as a result is a string which includes these times. For a sample directory I have:
st_atime=1418911410L
st_mtime=1418911410L
st_ctime=1404656050L
My problem is that I have some confusion with these numbers. I would like to know if these numbers are convertible to actual time? or, if one number (lets say ctime) is smaller than the other (like atime), does it mean that ctime is earlier than atime or later? I have searched many websites to learn this, but my attempts went without a success. Could anyone help me? thanks in advance.
Upvotes: 15
Views: 23082
Reputation: 50540
You can get something useful from this by using the stat module to interpret the stat()
results and converting from the epoch to a datetime:
import os
import datetime
print datetime.datetime.fromtimestamp(os.stat(".").st_atime)
This prints out a datetime object showing the last time the current directory was accessed:
datetime.datetime(2014, 12, 17, 7, 19, 14, 947384)
Upvotes: 2
Reputation: 6692
Also strftime
can do this apart from other examples,
time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1418911410))
Upvotes: 3
Reputation: 1121366
Quoting the os.stat()
function documentation:
Note: The exact meaning and resolution of the
st_atime
,st_mtime
, andst_ctime
attributes depend on the operating system and the file system. For example, on Windows systems using the FAT or FAT32 file systems,st_mtime
has 2-second resolution, andst_atime
has only 1-day resolution. See your operating system documentation for details.
For Linux, that system documentation is the stat (2)
manpage:
struct timespec st_atim; /* time of last access */
struct timespec st_mtim; /* time of last modification */
struct timespec st_ctim; /* time of last status change */
where timespec
is defined in man time(2)
:
Its time represents seconds and nanoseconds since the Epoch.
where the Epoch is the UNIX Epoch. The higher the value, the more time has passed since that Epoch (which is the 1st of January 1970, midnight UTC).
The Python time
module deals in time in the same way, and the datetime
module has class methods that'll give you a datetime
object from such a value as well:
>>> import datetime
>>> datetime.datetime.fromtimestamp(1418911410L)
datetime.datetime(2014, 12, 18, 14, 3, 30)
As you can see above, the three fields represent access time, modification time and status change time, respectively.
Upvotes: 11
Reputation: 30151
You can convert those timestamps to datetime
objects with this Python code:
import datetime
datetime.datetime.fromtimestamp(1418911410L) # or whatever number you have
Upvotes: 3
Reputation: 360572
ctime - the last time the file's inode was changed (e.g. permissions changed, file renamed, etc..)
mtime - last time the file's CONTENTS were changed
atime - last time the file was accessed.
The numbers are simply unix timestamps - signed 32bit ints representing seconds since Jan 1/1970, aka the epoch.
And yes, smaller numbers = earlier in time.
Upvotes: 19