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

Reblog this post [with Zemanta]


Tags: , , ,

Comments:

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Copyright © 2009-2012 Joys of Programming All rights reserved.
Desk Mess Mirrored v1.8.1 theme from BuyNowShop.com.