Hi GK, On Friday 08 October 2010 10:02:34 Gopal Karunakar wrote: > Hi All, > > I am using the Time::localtime module in my perl script for getting a > particular date format. It was working fine till last month, but for the > month of October it is seen that it's showing as September. I am pasting a > copy of my code below: > > sub getLocalTime > { > my $tm = localtime; > my ($day, $month, $year, $hour, $min, $sec) = ($tm->mday, $tm->mon, > $tm->year, $tm->hour, $tm->min, $tm->sec); > $year = 1900 + $year; > my $enddate = "$month-$day-$year $hour:$min:$sec"; > }
This is not your entire code. Next time please post your entire code which reproduces this behaviour. Here's a more complete code: <<<<<<<<<<<<<< #!/usr/bin/perl use strict; use warnings; use Time::localtime; sub getLocalTime { my $tm = localtime; my ($day, $month, $year, $hour, $min, $sec) = ($tm->mday, $tm->mon, $tm->year, $tm->hour, $tm->min, $tm->sec); $year = 1900 + $year; return "$month-$day-$year $hour:$min:$sec"; } print getLocalTime(), "\n"; >>>>>>>>>>>>>> Now, the problem is that reading from http://perldoc.perl.org/functions/localtime.html : {{{ $mon is the month itself, in the range 0..11 with 0 indicating January and 11 indicating December }}} So it always returns 9 for October (and 8 for September). If you want to manage dates, you'd better look into a CPAN abstraction such as DateTime, see: http://perl-begin.org/topics/date-and-time/ . A few other notes on your code: > sub getLocalTime Please avoid camelCase. words_separated_by_underscore is better. > { > my $tm = localtime; > my ($day, $month, $year, $hour, $min, $sec) = ($tm->mday, $tm->mon, > $tm->year, $tm->hour, $tm->min, $tm->sec); > $year = 1900 + $year; This is better written as $year += 1900; Or you can do «$tm->year+1900» in the assignment. > my $enddate = "$month-$day-$year $hour:$min:$sec"; You probably want a return statement here. Maybe also look into POSIX::strftime() > } Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ Parody of "The Fountainhead" - http://shlom.in/towtf <rindolf> She's a hot chick. But she smokes. <go|dfish> She can smoke as long as she's smokin'. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/