File's date


I want to get the date and last modified time of a file.
I use stat() for the modified time but it returns an huge number which doesn't look like a modified time. I suppose there is some kind of conversion to make....

Can you help me out? Note: I am under a windows o/s
Who is Participating?
martinagConnect With a Mentor Commented:

@time = localtime((stat($filename))
will give you a date. Here's how to get all the info:
$time[0] is the seconds
$time[1] is the minutes
$time[2] is the hours
$time[3] is the monthday beginning with 1
$time[4] is the monthnumber beginning with 0
$time[5] is the year (add 1900)
$time[6] is the week day with 0 for Sunday
$time[7] is the day of the year
$time[8] is the time zone

It should be
@time = localtime(stat($filename))

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Actually, $time[8] is a flag indicating whether or not it's daylight savings time (in the _local_ time zone.)
yesnaudAuthor Commented:
All I get is value 1 in $time[0-8]

Any idea why?

yesnaudAuthor Commented:
All I get is value 1 in $time[0-8]

Any idea why?

yesnaudAuthor Commented:
Sorry made a mistake, now I get values, but does not seems to be coherent with the actual file's date and time...

post code....
yesnaudAuthor Commented:
Ok, got it now... thanks... I'll give the points to martin...

$time = localtime((stat $filename)[9]);

9 is the position of the modified time of the file. 8 is the
value of the last time the file was accessed and 10 is the value
for when the file was created.

These three contain the number of seconds since 1st jan 1970,
they're converted to seconds because then they are a lot easier
to calculate with.

ozo; give us some form of background..

yesnaud, are you still there?

perldoc -f stat
    stat EXPR
    stat    Returns a 13-element list giving the status info for a file,
            either the file opened via FILEHANDLE, or named by EXPR.
            If EXPR is omitted, it stats `$_'. Returns a null list
            if the stat fails. Typically used as follows:

                       = stat($filename);

            Not all fields are supported on all filesystem types.
            Here are the meaning of the fields:

              0 dev      device number of filesystem
              1 ino      inode number
              2 mode     file mode  (type and permissions)
              3 nlink    number of (hard) links to the file
              4 uid      numeric user ID of file's owner
              5 gid      numeric group ID of file's owner
              6 rdev     the device identifier (special files only)
              7 size     total size of file, in bytes
              8 atime    last access time since the epoch
              9 mtime    last modify time since the epoch
             10 ctime    inode change time (NOT creation time!) since the epoch
             11 blksize  preferred block size for file system I/O
             12 blocks   actual number of blocks allocated

            (The epoch was at 00:00 January 1, 1970 GMT.)

            If stat is passed the special filehandle consisting of
            an underline, no stat is done, but the current contents
            of the stat structure from the last stat or filetest are
            returned. Example:

                if (-x $file && (($d) = stat(_)) && $d < 0) {
                    print "$file is executable NFS file\n";

            (This works on machines only for which the device number
            is negative under NFS.)

            In scalar context, `stat()' returns a boolean value
            indicating success or failure, and, if successful, sets
            the information associated with the special filehandle

perldoc -f localtime
    localtime EXPR
            Converts a time as returned by the time function to a 9-
            element array with the time analyzed for the local time
            zone. Typically used as follows:

                #  0    1    2     3     4    5     6     7     8
                ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =

            All array elements are numeric, and come straight out of
            a struct tm. In particular this means that `$mon' has
            the range `0..11' and `$wday' has the range `0..6' with
            sunday as day `0'. Also, `$year' is the number of years
            since 1900, that is, `$year' is `123' in year 2023, and
            *not* simply the last two digits of the year.

            If EXPR is omitted, uses the current time

            In scalar context, returns the ctime(3) value:

                $now_string = localtime;  # e.g., "Thu Oct 13 04:54:34 1994"

            This scalar value is not locale dependent, see the
            perllocale manpage, but instead a Perl builtin. Also see
            the `Time::Local' module, and the strftime(3) and
            mktime(3) function available via the POSIX module. To
            get somewhat similar but locale dependent date strings,
            set up your locale environment variables appropriately
            (please see the perllocale manpage) and try for example:

                use POSIX qw(strftime);
                    $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;

            Note that the `%a' and `%b', the short forms of the day
            of the week and the month of the year, may not
            necessarily be three characters wide.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.