p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Perl (http://p2p.wrox.com/forumdisplay.php?f=196)
-   -   File time stamp keep printing epoch (http://p2p.wrox.com/showthread.php?t=75864)

geek.shrek August 25th, 2009 12:06 AM

File time stamp keep printing epoch
 
Hi,

I'm trying to get a file time stamp to record the file last modifed.
I would like to print as a real time, but I keep getting epoch time instead.

Code:

use Time::Local;

sub getTime
{
  my $filestat = (stat($myfile))[9];
  my $mystamp = scalar(localtime($filestat));

  print "File Stamp " . $mystamp;
}

It's printing "Thu Jan 1 10:00:00 1970"

Can someone tell me what did I do wrong here?[:confused:]

Thanks,

Leigh August 25th, 2009 01:26 AM

My guess is that you are not passing a valid filename to the sub.
Are you using strict and warnings?

Code:

use strict;
use warnings;
use Time::Local;

sub getTime
{
  my $myfile=shift;
  my $filestat = (stat($myfile))[9];
  my $mystamp = scalar(localtime($filestat));

  print "File Stamp " . $mystamp;
}
getTime("timestamp.pl");

Gives me:
Code:

C:\Temp>perl timestamp.pl
File Stamp Tue Aug 25 15:23:37 2009

But if I remove the 'use strict', 'use warnings' and 'my $myfile=shift' lines, I get the same output as you are getting, because this line:
Code:

my $filestat = (stat($myfile))[9];
has nothing to do a stat() on.

geek.shrek August 30th, 2009 09:17 PM

Thanks for the reply :)

I found out that it's printing epoch because I have File::stat in my library. :(


All times are GMT -4. The time now is 12:29 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.