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

I was wondering if anyone could give me an idea of
how to feed the $days figure generated by
Date::Calc into HTML::CalendarMonth. I have a
data file of $days (731158; 731178; etc.) which I'd like to project
onto the calMonth html so that the dates
occurring in the file show highlighted on the ensuing
calendar.

I'd be grateful for any assistance.

Replies are listed 'Best First'.
Re: Date::Calc to HTML::CalendarMonth
by mojotoad (Monsignor) on Nov 11, 2002 at 22:08 UTC
    The second half of your question involves how to "highlight" these days on your calendar object. I'm not sure what you mean by highlight, but you will want to read up on how manipulations are performed on an HTML::Element, on which HTML:::ElementTable is built, from which HTML::CalendarMonth is derived.

    You might be looking to do something like this, presuming your day numbers are in an array @days:

    use HTML::AsSubs; ... # your @days gets filled in here my $c = HTML::CalendarMonth->new(month => $month, year => $year); $c->item(@days)->attr(bgcolor => 'wheat'); # Or maybe you want a link my $link = 'http://www.somewhere.com/' foreach my $day (@days) { my $href = $link . "?$day"; $c->item($day)->wrap_content(a({href => $href})); }

    Matt

      Thanks very much for your reply (& by email). The first is very much what I'm looking for but unfortunately I can't find a way through to creating (@days). I've been working on it for hours but to no avail.

      I am looking at a range over several months, and I've been using push to strip down the array, hoping to create a further calendar with the remainder. Unfortunately this doesnot happen :(

      #!/usr/bin/perl -w use CGI qw(:standard); use Date::Calc qw(:all); use HTML::AsSubs; use HTML::Element; use HTML::CalendarMonth; print "Content-type: text/html\n\n"; @dates = (731158, 731159, 731160, 731161, 731178, 731600); foreach $days (@dates) { ($year,$month,$day) = Add_Delta_Days(1,1,1, $days - 1); if (($year eq 2002) && ($month eq 01)) { push (@days, $day); } else { print "ok"; } } my $c = HTML::CalendarMonth->new(month => $month, year => $year); $c->item(@days)->attr(bgcolor => 'wheat'); print $c->as_HTML;

      As I mentioned I thought that the 'push' would strip away the used scalars within @dates so I could use the foreach again to generate another calendar.

      If anyone could set me in the right direction to get this sorted I'd be very appreciative.

      (I also couldn't get this to work at all using strict so I turned it off in the end).

Re: Date::Calc to HTML::CalendarMonth
by Mr. Muskrat (Canon) on Nov 11, 2002 at 19:08 UTC

    First, how did you get Date::Calc to generate the data file of days? I only ask because when I used those values in this:

    #!/usr/bin/perl use strict; use warnings; use Date::Calc qw(:all); foreach (731158, 731178) { my ($year,$month,$day,$hour,$min,$sec) = Time_to_Date([$_]); print "$year $month $day $hour:$min:$sec\n"; }
    I see that both are the same date, 1970-10-13.

    How about posting some code?

Solution for Date::Calc to HTML::CalendarMonth
by Mr. Muskrat (Canon) on Nov 12, 2002 at 16:13 UTC

    jonnyfolk, I think that I have finally figured out how to do what you want.

    This takes the sorted days values stored in @dates. It will create as many calendars as needed for each day in @dates.

    Updated: Added more comments for jonnyfolk!

      That's brilliant, Mr Muskrat. I ran a few more dates through there and it works just as I'd imagined. Thanks very much (I shall have to work slowly through the script because I'm a little lost in places, but I shall get there!)

      Thanks also to mojotoad for supplying the toolkit and words of support!

        Thank you! I love to help out when I can.

        Sorry for the misdirection at first but I was misunderstanding what you were wanting and how you were getting you data.

        I have added more comments to try and explain what is going on. It should make it easier to work your way through it.

Re: Date::Calc to HTML::CalendarMonth
by Mr. Muskrat (Canon) on Nov 11, 2002 at 19:11 UTC
      absolutely, the code I'm using is:
      my $d = $query->param('days'); #2 digit my $m = $query->param('months'); #2 digit my $y = $query->param('years'); #4 digit my $Dd = $query->param('Dd'); $days = Date_to_Days($y,$m,$d);
      I'll check out the modules you mention, though I'm still interested in getting the html::calmonth working as it seems to be geared up for this. Thanks for your help.

        I meant that you might want to use Date::Calc::Object and Date::Calendar with HTML::CalendarMonth.

        What is the point of calculating Date_to_Days? Why not store it in epoch seconds instead?

        Quick example to illustrate epoch seconds...

        #!/usr/bin/perl use strict; use warnings; use Time::Local; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst); $mday = 13; $mon = 9; # 0 - 11, so this is October $year = 1973; # use 12pm my $seconds = timelocal(0,0,12,$mday,$mon,$year); print "epoch seconds: $seconds.\n"; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($sec +onds); $year += 1900; $mon++; printf "%02d:%02d:%02d %04d-%02d-%02d\n", ($hour,$min,$sec,$year,$mon, +$mday);

        update: You could also use the Date_to_Time and Time_to_Date functions of Date::Calc.

        #!/usr/bin/perl use strict; use warnings; use Date::Calc qw(:all); my ($sec,$min,$hour,$day,$month,$year, $seconds); $sec = 0; $min = 0; $hour = 12; $day = 13; $month = 10; $year = 1973; $seconds = 0; # use 12pm $seconds = Date_to_Time($year,$month,$day, $hour,$min,$sec); print "epoch seconds: $seconds.\n"; ($year,$month,$day, $hour,$min,$sec) = Time_to_Date($seconds); printf "%02d:%02d:%02d %04d-%02d-%02d\n", ($hour,$min,$sec,$year,$mont +h,$day);