Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Very simple calendar

by Juerd (Abbot)
on Dec 21, 2003 at 18:03 UTC ( #316223=snippet: print w/replies, xml ) Need Help??
Description: Use with an argument of "yyyy-mm", or with no argument to get the current month. I made this because it was easier than hacking the existing cal tool to do what I want. This snippet is very configurable, but only by hacking its source :)
#!/usr/bin/perl -w

use strict;
use Calendar::Simple qw(calendar);
use POSIX qw(strftime);

my $today = strftime '%Y-%m-%d', localtime;
my ($year, $month) = split /-/, shift || $today;

printf "\n%04d-%02d\n\n", $year, $month;
print "\e[1mMon Tue Wed Thu Fri Sat Sun\e[0m\n";

for (calendar $month, $year, 1) {
    for (@$_) {
        no warnings;
        $_ = sprintf '%3s', $_ || '';
        $_ = "\e[7m$_\e[0m"
            if sprintf("%04d-%02d-%02d", $year, $month, $_) eq $today;
    }
    print "@$_\n";
}
print "\n";
Replies are listed 'Best First'.
•Re: Very simple calendar
by merlyn (Sage) on Dec 21, 2003 at 23:12 UTC

      Even simpler: perl -MDate::Calc=Calendar -e 'print Calendar(@ARGV)' 2003 12

      Yes, simpler. But as I explained in the description, I made this because it was easier than hacking something that already exists. Almost every unix box I use has a handy little utility called cal installed, which does almost exactly what I want.

      Except that it uses the current locale. While usually this is laudable, I just want the same output regardless of the locale I'm using. And I wanted the current day to be in reverse video, and the names of the days in bold. And the weeks to start on Mondays, and the names of the days in three letter English abbreviations. Date::Calc::Calendar does almost all of this. As you can probably see, its layout inspired me :)

      If I didn't have these wishes, I'd still be using cal instead of the Date::Calc oneliner :)

      Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Re: Very simple calendar
by theguvnor (Chaplain) on Dec 21, 2003 at 21:07 UTC

    Cool - I've wanted a cal tool on Windows... Anyone know where can I find out how to "fix" the escape sequences you put in there, to work with Win9x? Looked through the perldoc documentation but couldn't find anything.

    Update: ++Juerd for pointing me to the ansi.sys driver.

    [Jon]

      Just stripped out the escape sequences...
      #!/usr/bin/perl -w use strict; use Calendar::Simple qw(calendar); use POSIX qw(strftime); my $today = strftime '%Y-%m-%d', localtime; my ($year, $month) = split /-/, shift || $today; printf "\n%04d-%02d\n\n", $year, $month; print "Mon Tue Wed Thu Fri Sat Sun\n"; for (calendar $month, $year, 1) { for (@$_) { no warnings; $_ = sprintf '%3s', $_ || ''; } print "@$_\n"; } print "\n";
      diff -u ...

        no warnings;

        Not needed anymore. It was there because I was too lazy to deal with the $_ not being numeric when it's empty, in the sprintf line.

        Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

      Anyone know where can I find out how to "fix" the escape sequences you put in there, to work with Win9x?

      IIRC, there's a little device driver called ANSI.SYS that you can load from CONFIG.SYS. I think it was something like

      DEVICE=X:\PATH\TO\ANSI.SYS
      But it's been a few years since I last used Windows for more than fixing other people's computers.

      Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

        Thanks... it works perfectly.

        [Jon]

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://316223]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2020-05-29 23:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (171 votes). Check out past polls.

    Notices?