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

#!/usr/bin/perl -w use CGI qw(:all); use Date::Calc qw(Days_in_Month Day_of_Week Month_to_Text); print "Content-type:text/html\n\n"; my $Path="/home/public/davis/calender_2month.htm"; my %EMB = (); my $i; &getmonthvalue; exit(0); sub getmonthvalue { my $year=param("year"); my $month=param("month"); if(!$year) { $year = 2010; } if(!$month) { $month=1; } &monthvalue($month,$year,0); if($month >= 12) { $month = 1; $year = $year + 1; }else{ $month = $month + 1; } &monthvalue($month,$year,42); &buildnextprevious($month,$year,); print &printFile($Path, \%EMB); } sub monthvalue { my $month = shift; my $year = shift; $i = shift; my $monthtotext = Month_to_Text($month); my $noofdays = Days_in_Month($year,$month); my $dayofweek = Day_of_Week($year,$month,1); $EMB{"mnth$i"} = $monthtotext; $EMB{yr}=$year; $EMB{nextyr}=$year; $noofdays = $noofdays + $i; for(my $j=$i,$k=1;$j < $noofdays;$j++,$k++){ $EMB{$j + $dayofweek} = $k; } } sub buildnextprevious { my $newmonth = shift; my $newyear=shift; # NEXT SECTION if($newmonth == 1) { $EMB{yr} = $newyear - 1; } $EMB{Next} = qq( <a href="/cgi-bin/davis/calender_2month.pl?month= +$newmonth&year=$newyear">Next</a>); # PREVIOUS SECTION if($newmonth == 2) { $newyear = $newyear-1; $newmonth = 14; }if($newmonth == 1) { $newyear = $newyear - 1; $newmonth = 11; }else{ $newmonth = $newmonth - 2; } $EMB{Previous} = qq( <a href="/cgi-bin/davis/calender_2month.pl?mo +nth=$newmonth&year=$newyear">Previous</a>); } sub printFile { my $readFile = shift; my $storeHashRef = shift; my $retFile=""; open (TEMPLATE, $readFile) or die "Open failed for template file * +* $readFile **"; my $Line = ""; while (<TEMPLATE>) { if(/\^/) { $Line = $_; while($Line =~ s/\^(\w*)\^//) { if (!defined $$storeHashRef{$1}) { $$storeHashRef{$1}=""; } } s/\^(\w*)\^/$$storeHashRef{$1}/g; } $retFile .= $_; } close (TEMPLATE); return $retFile; }# end of : sub printFile {
  • Comment on How do i select the past date and highlight the current date?my code is
  • Download Code

Replies are listed 'Best First'.
Re: How do i select the past date and highlight the current date?my code is
by Corion (Patriarch) on Apr 27, 2010 at 07:42 UTC

    Is all that HTML and CGI handling necessary for your question? For date calculations, I would look at DateTime, or Date::Calc, or do the calculations in a loop using POSIX::strftime, Time::Local and a timestamp, adding 22 hours worth of seconds until the date changes:

    use strict; use POSIX qw(strftime); sub day_before { my ($ts) = @_; my $ts_now = strftime '%Y%m%d', $ts; $ts -= 22*60*60 while strftime('%Y%m%d', $ts) eq $ts_now; return strftime '%Y%m%d', $ts; }; print day_before(time);

      I would be interested to know why you use 22 hours rather than 23 or 24. I guess because 24 hours might skip a day at change of hours (to/from daylight savings time where I am but no doubt called otherwise elsewhere) and perhaps in some places the change is more than one hour.

        Exactly. Because I don't want to have to think about when a daylight savings time change happens in date calculations, I'm not adding/subtracting 24 hours, as there is one day every year which only has 23 hours and one day which has 25 hours. I'm not adding/subtracting exactly 23 hours because I don't want to think about the boundary conditions. So I settle on 22 hours.

Re: How do i select the past date and highlight the current date?my code is
by Marshall (Canon) on Apr 27, 2010 at 09:14 UTC
    I am not quite sure why this is so complex. I use GMT (Greenwich Mean Time) or what is now called UTC Universal Coordinated Time for log file entries and database entries. I convert that time into local time for user presentation when needed. There are a number of modules that do this very well.

    There is no ambiguity with a UTC time. When you set your local clock back or forward one hour, the UTC time marches onward - it is a continuously incrementing number - UTC has no concept of "daylight savings time". I avoid having chron jobs start between 1:00-2:00 am local time as this is a boundary condition when the local time might "revert" to a previous time.

Re: How do i select the past date and highlight the current date?my code is
by Ratazong (Monsignor) on Apr 27, 2010 at 07:32 UTC

    Have you had a look at Date::Calc::Delta_Days?

    If the delta between today (localtime (*)) and your date is negative, it is a day in the past; if it is zero, it is today. Then you can highlight/select the respective dates... .

    HTH, Rata

    (*): use $day = (localtime)[3]; $mon = (localtime)[4]+1; $year =(localtime)[5]+1900