syber_punk has asked for the wisdom of the Perl Monks concerning the following question:

hey monks, I have a problem I'm writing some code to copy a a file from a floppy to a directory on the c drive named c:\temp_ (file creation date in the 09_16_2005 format e.g. c:\temp_09_16_2005. So I wrote a script that will get the modify access and creation date on a specified file but it`s a month early I found the majority of the code online at http://www.infocopter.com/perl_corner/date-time.htm I would be greatful for any help the code is below
my ($atime, $mtime, $ctime) = (stat("c:\SPTHistory1.txt"))[8..10]; printf qq~ Accessed: %s Modified: %s Created: %s ~, get_real_time($atime), get_real_time($mtime), get_real_time($ctime); sub get_real_time ($) { my ($sec,$min,$hour,$mon,$mday,$year,$wday,$yday,$isdst) = gmt +ime $_[0]; $year += 1900; return "$year\_$mday\_$mon"; }
Although I appreciate the help I still cannot get it to work I will admit it may be my own fault I am sort a n00bie
  • Comment on strange error why is my code showing atime mtime and ctime a month to early
  • Download Code

Replies are listed 'Best First'.
Re: strange error why is my code showing atime mtime and ctime a month to early
by Fletch (Bishop) on Sep 16, 2005 at 17:22 UTC

    Erm, you are aware that . . .

    perldoc -f localtime . . . and $mon is the month itself, in the range 0..11 with 0 indica +ting January and 11 indicating December.

    And consider using POSIX::strftime which is aware of this and handles things in a locale aware manner.

Re: strange error why is my code showing atime mtime and ctime a month to early
by graff (Chancellor) on Sep 16, 2005 at 22:19 UTC
    Fletch gave you the answer you were looking for -- you need to add 1 to $mon, just like you add 1900 to $year. But I wondered if you might rather want your string to be:
    return sprintf( "%d_%2.2d_%2.2d", $year, $mon, $mday ); # e.g. "2005_09_16"
    (update: original "join" approach would have produced "2005_9_16", which drives me nuts.)

    That way, a standard ascii-betic sort of file names would put them in chronological order (assuming the date string is the first part that differs from one file name to the next).

    I suppose some people prefer to see a sorted list that puts things together according to the month they were created, regardless of the year, but I've never understood people like that.