How to calculate the year of day in Perl
Posted by Joys of Programming on in Perl
Perl has two functions time and localtime to get the current time. time gives the current time in epochs and localtime helps to see the time in human readable format.
What is day of the year?
Day of the year is the number of days passed since January 1st of this year. localtime gives a lot of information like hours, minutes, seconds, month, day and year. It also returns day of the year
use Switch;
my $time = time();
my ($sec, $min, $hours, $mday, $month, $year, $wday, $yday, $isdst) = localtime($time);
print "Today is ", $yday;
my $pos = "th";
switch($yday%10){
case [1] {$pos ="st";}
case [2] {$pos = "nd";}
case [3] {$pos = "rd";}
}
print $pos," of the year ", $year+1900,"\n";
which generates the following output
Today is 326th of the year 2009
Comments:
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=e4a15675-7412-4578-95be-cfef886b7d19)