Ctime, atime and mtime - how to interpret them?

I am writing a Python program that requires comparing the atime, mtime, and ctime of multiple directories. For this I use os.stat("my_directory/")

. As a result, I get a string that includes these times. For the examples directory, I:

st_atime=1418911410L
st_mtime=1418911410L
st_ctime=1404656050L

      

My problem is that I have a confusion with these numbers. I would like to know if these numbers are convertible to actual time? or if one number (say ctime) is less than another (say atime), does that mean ctime is earlier than atime or later? I searched many sites to find out, but my attempts were unsuccessful. Can anyone help me? thank you in advance.

+3


source to share


5 answers


You can get something useful out of this by using the stat module to interpret the results stat()

and go from epoch to date and time:

import os
import datetime

print datetime.datetime.fromtimestamp(os.stat(".").st_atime)

      



This outputs a datetime object showing the last access to the current directory:

datetime.datetime(2014, 12, 17, 7, 19, 14, 947384)

      

+1


source


ctime is the last time the inode file was changed (for example, permissions were changed, the file was renamed, etc.)
mtime is the last time the file was changed CONTENTS
atime is the last time the file was accessed.

The numbers are just unix timestamps - signed 32-bit ints representing seconds since January 1/1970, aka epochs.



And yes, smaller numbers = ahead of time.

+4


source


You can convert these timestamps to objects datetime

with this Python code:

import datetime

datetime.datetime.fromtimestamp(1418911410L)  # or whatever number you have

      

+2


source


Quoting the os.stat()

documentation for the functions
:

Note . The exact meaning and resolution of the attributes st_atime

, st_mtime

and st_ctime

are operating system and file system dependent. For example, on Windows systems using FAT or FAT32 file systems, it st_mtime

has a 2 second resolution, but st_atime

only one day resolution. See the documentation for your operating system for details.

For Linux, this 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 is timespec

defined in man time(2)

:

Its time is seconds and nanoseconds from the epoch.

where Epoch is the UNIX Era . The higher the value, the more time has passed since that epoch (which is January 1, 1970, midnight UTC).

A Python module works the same way over time, and has class methods that also give you an object from that value: time

datetime

datetime

>>> 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.

+2


source


strftime

Can also do it separately from other examples,

time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1418911410))

      

+1


source







All Articles