Solved

File's date

Posted on 1998-11-25
14
175 Views
Last Modified: 2010-03-04
Hi,

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
0
Comment
Question by:yesnaud
  • 5
  • 4
  • 2
  • +2
14 Comments
 
LVL 4

Expert Comment

by:martinag
ID: 1206580
@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

Martin
0
 
LVL 4

Expert Comment

by:martinag
ID: 1206581
It should be
@time = localtime(stat($filename))

Martin
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1206582
Actually, $time[8] is a flag indicating whether or not it's daylight savings time (in the _local_ time zone.)
0
 
LVL 1

Author Comment

by:yesnaud
ID: 1206583
All I get is value 1 in $time[0-8]

Any idea why?

0
 
LVL 1

Author Comment

by:yesnaud
ID: 1206584
All I get is value 1 in $time[0-8]

Any idea why?

0
 
LVL 1

Author Comment

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

Y.
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1206586
post code....
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:yesnaud
ID: 1206587
Ok, got it now... thanks... I'll give the points to martin...

Y.
0
 
LVL 4

Accepted Solution

by:
martinag earned 20 total points
ID: 1206588
Ok.

Martin
0
 
LVL 84

Expert Comment

by:ozo
ID: 1206589
$time = localtime((stat $filename)[9]);
0
 
LVL 4

Expert Comment

by:martinag
ID: 1206590
9?

Martin
0
 
LVL 4

Expert Comment

by:binkzz
ID: 1206591
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..

Binkzz
0
 
LVL 4

Expert Comment

by:martinag
ID: 1206592
yesnaud, are you still there?

Martin
0
 
LVL 84

Expert Comment

by:ozo
ID: 1206593
perldoc -f stat
    stat FILEHANDLE
    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:

                ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
                   $atime,$mtime,$ctime,$blksize,$blocks)
                       = 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) =
                                                            localtime(time);

            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
            (`localtime(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.

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now