Solved

File's date

Posted on 1998-11-25
14
193 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Port 80 requests 16 96
Perl output collect 7 67
need a perl -nle command to split string with delimiter ( a delimiter can be another string) 2 86
Regular Expression for URL 10 97
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

810 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