Date format again
Hello, this guy finally emailed his script to me. The problem he is having is with "$year". Here's the dating part of the code: --- $date = `/bin/date`; chop($date); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; $thisday = (Sun,Mon,Tue,Wed,Thu,Fri,Sat)[(localtime) [6]]; $s = (localtime)[0]; $m = (localtime)[1]; $m = ($m + 35) ; $h = (localtime)[2]; if ($m >=60) { $m = ($m - 60); $h = ($h + 1); } if ($h >=24) { $h = 1; } else { $h = ($h + 10) ; } $month = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[(localtime)[4]]; $year = (localtime)[5]; $year = ($year + 1900); $day = (localtime)[3]; # this coding is creating problem ($sec,$min,$hour,$mday,$mon,$year,undef,undef,undef) = localtime(); $mon++; # adjust from 0-11 to 1-12 $year %= 100; $theDate = sprintf("%02u%02u%02u", $mday, $mon, $year); -- Now, when he prints "$theDate", everything is ok with it (format-030302). But when he prints just "$year" it displays "2" instead of "2002" Any ideas? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Date format again
[ Don't Cc: me, I read this mailing list, thank you ] >> Troy May <[EMAIL PROTECTED]> writes: > ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; > $thisday = (Sun,Mon,Tue,Wed,Thu,Fri,Sat)[(localtime) [6]]; > $s = (localtime)[0]; > $m = (localtime)[1]; > $m = ($m + 35) ; Is adding 35 minutes to the current date what this is all about? How about: ($sec, $min, $hour, $mday, $mon, $year) = (localtime(time+35*60))[0..5]; and save yourself from all that adding and carrying? By the way, in the original program, if you happen to run it at, say 23:59:59 and the clock changes to 0:00:00 while the program is still running, between one of the calls to localtime, you'll get horribly incorrect results. > $year %= 100; > Now, when he prints "$theDate", everything is ok with it (format-030302). > But when he prints just "$year" it displays "2" instead of "2002" 2002 % 100 == 2 -- Marcelo -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Date format again
$year is a number. Numbers do not retain leading zeros. "$year %= 100;" does not return the string "02"; it returns the number 2. Try something like this - $s_year = sprintf("%02u", $year); print "$s_year"; The sprintf function basically converts the number into a string. "sprintf returns a string formatted by the usual printf conventions of the C library function sprintf." -- http://www.perldoc.com/perl5.6.1/pod/func/sprintf.html It's just the same thing you did with the $theDate variable only applied to a single item of the list on the right side of the assignment. - Original Message - From: "Troy May" <[EMAIL PROTECTED]> To: "Beginners CGI List" <[EMAIL PROTECTED]> Sent: Sunday, March 03, 2002 12:52 AM Subject: Date format again > Hello, this guy finally emailed his script to me. The problem he is having > is with "$year". Here's the dating part of the code: > > --- > $date = `/bin/date`; > chop($date); > ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; > $thisday = (Sun,Mon,Tue,Wed,Thu,Fri,Sat)[(localtime) [6]]; > $s = (localtime)[0]; > $m = (localtime)[1]; > $m = ($m + 35) ; > > $h = (localtime)[2]; > > if ($m >=60) > { > $m = ($m - 60); > $h = ($h + 1); > } > if ($h >=24) > { > $h = 1; > } > else > { > $h = ($h + 10) ; > } > > $month = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[(localtime)[4]]; > $year = (localtime)[5]; > $year = ($year + 1900); > > $day = (localtime)[3]; > > > # this coding is creating problem > > ($sec,$min,$hour,$mday,$mon,$year,undef,undef,undef) = localtime(); > $mon++; # adjust from 0-11 to 1-12 > $year %= 100; > $theDate = sprintf("%02u%02u%02u", $mday, $mon, $year); > > -- > > Now, when he prints "$theDate", everything is ok with it (format-030302). > But when he prints just "$year" it displays "2" instead of "2002" > > Any ideas? > > > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]