Calendar-Simple

 view release on metacpan or  search on metacpan

Build.PL  view on Meta::CPAN

    'Test::Pod'           => 0,
    'Test::Pod::Coverage' => 0,
  },
  create_makefile_pl => 'traditional',
  script_files       => [ 'bin/pcal' ],
  meta_merge => {
    'meta-spec' => { version => 2 },
    resources => {
      repository => {
        type => 'git',
        url => 'https://github.com/davorg-cpan/calendar-simple.git',
        web => 'https://github.com/davorg-cpan/calendar-simple',
      },
      bugtracker => {
        web => 'https://github.com/davorg-cpan/calendar-simple/issues',
      },
    },
  },
);

$build->create_build_script;

META.json  view on Meta::CPAN

{
   "abstract" : "Perl extension to create simple calendars",
   "author" : [
      "Dave Cross <dave@mag-sol.com>"
   ],
   "dynamic_config" : 1,
   "generated_by" : "Module::Build version 0.4234",
   "license" : [
      "perl_5"
   ],
   "meta-spec" : {
      "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",

META.json  view on Meta::CPAN

   },
   "provides" : {
      "Calendar::Simple" : {
         "file" : "lib/Calendar/Simple.pm",
         "version" : "v2.1.0"
      }
   },
   "release_status" : "stable",
   "resources" : {
      "bugtracker" : {
         "web" : "https://github.com/davorg-cpan/calendar-simple/issues"
      },
      "license" : [
         "http://dev.perl.org/licenses/"
      ],
      "repository" : {
         "type" : "git",
         "url" : "https://github.com/davorg-cpan/calendar-simple.git",
         "web" : "https://github.com/davorg-cpan/calendar-simple"
      }
   },
   "version" : "v2.1.0",
   "x_serialization_backend" : "JSON::PP version 4.16"
}

META.yml  view on Meta::CPAN

---
abstract: 'Perl extension to create simple calendars'
author:
  - 'Dave Cross <dave@mag-sol.com>'
build_requires:
  Test::More: '0'
configure_requires:
  Module::Build: '0.42'
dynamic_config: 1
generated_by: 'Module::Build version 0.4234, CPAN::Meta::Converter version 2.150010'
license: perl
meta-spec:

META.yml  view on Meta::CPAN

    file: lib/Calendar/Simple.pm
    version: v2.1.0
recommends:
  DateTime: '0'
requires:
  Carp: '0'
  Config: '0'
  Time::Local: '0'
  perl: v5.10.0
resources:
  bugtracker: https://github.com/davorg-cpan/calendar-simple/issues
  license: http://dev.perl.org/licenses/
  repository: https://github.com/davorg-cpan/calendar-simple.git
version: v2.1.0
x_serialization_backend: 'CPAN::Meta::YAML version 0.018'

README  view on Meta::CPAN

Calendar::Simple
----------------

This is a very simple module that models a calendar month.

INSTALLATION

To install this module type the following:

   perl Build.PL
   ./Build
   ./Build test
   ./Build install

bin/pcal  view on Meta::CPAN


my @months = qw(January February March April May June July August
                September October November December);

my $month      = shift // (localtime)[4] + 1;
my $year       = shift // (localtime)[5] + 1900;
my $start_date = shift // 1;

$start_date %= 7;

my @month = calendar($month, $year, $start_date);
my $month_name = $months[$month - 1];
my $pad = int((20 - length("$month_name $year")) / 2);
print "\n", ' ' x $pad, "$month_name $year\n";

my @days = qw(Su Mo Tu We Th Fr Sa);
push @days, splice @days, 0, $start_date;
print "@days\n";

foreach (@month) {
  print map { $_ ? sprintf "%2d ", $_ : '   ' } @$_;

lib/Calendar/Simple.pm  view on Meta::CPAN

# $Id$

=head1 NAME

Calendar::Simple - Perl extension to create simple calendars

=head1 SYNOPSIS

  use Calendar::Simple;

  my @curr      = calendar;             # get current month
  my @this_sept = calendar(9);          # get 9th month of current year
  my @sept_2002 = calendar(9, 2002);    # get 9th month of 2002
  my @monday    = calendar(9, 2002, 1); # get 9th month of 2002,
                                        # weeks start on Monday

  my @span      = date_span(mon   => 10,  # returns span of dates
                            year  => 2006,
                            begin => 15,
                            end   => 28);

=cut

package Calendar::Simple;

use 5.006;
use strict;
use warnings;

use base 'Exporter';

our @EXPORT = qw(calendar);
our @EXPORT_OK = qw(date_span);
our $VERSION = '2.1.0';

use Time::Local;
use Carp;

eval "use DateTime";
my $dt = ! $@;
$dt = 0 if $ENV{CAL_SIMPLE_NO_DT};

my @days = qw(31 xx 31 30 31 30 31 31 30 31 30 31);

=head1 DESCRIPTION

A very simple module that exports one function called C<calendar>.

=head2 calendar

This function returns a data structure representing the dates in a month.
The data structure returned is an array of array references. The first
level array represents the weeks in the month. The second level array
contains the actual days. By default, each week starts on a Sunday and
the value in the array is the date of that day. Any days at the beginning
of the first week or the end of the last week that are from the previous or
next month have the value C<undef>.

If the month or year parameters are omitted then the current month or
year are assumed.

A third, optional parameter, start_day, allows you to set the day each
week starts with, with the same values as localtime sets for wday
(namely, 0 for Sunday, 1 for Monday and so on).

=cut

sub calendar {
  my ($mon, $year, $start_day) = _validate_params(@_);

  my $first = _get_first($mon, $year, $start_day);

  my @mon = (1 .. _days($mon, $year));

  my @first_wk = (undef) x 7;
  @first_wk[$first .. 6] = splice @mon, 0, 6 - $first + 1;

  my @month = (\@first_wk);

lib/Calendar/Simple.pm  view on Meta::CPAN

The first day of the required span. Defaults to the first if omitted.

=item end

The last day of the required span. Defaults to the last day of the month
if omitted.

=item start_day

Indicates the day of the week that each week starts with. This takes the same
values as the optional third parameter to C<calendar>. The default is 1
(for Monday).

B<NOTE:> As of version 2.0.0, the default C<start_day> has changed. Previously,
it was Sunday; now, it is Monday. This is so the default behaviour matches
that of the standard Unix C<cal> command.

=back

This function isn't exported by default, so in order to use it in your
program you need to use the module like this:

lib/Calendar/Simple.pm  view on Meta::CPAN

sub date_span {
  my %params = @_;

  my ($mon, $year, $start_day) = _validate_params(
    @params{ qw[mon year start_day] },
  );

  my $begin = $params{begin} || 1;
  my $end   = $params{end}   || _days($mon, $year);

  my @cal = calendar($mon, $year, $start_day);

  shift @cal while $cal[0][6] < $begin;

  my $i = 0;
  while (defined $cal[0][$i] and $cal[0][$i] < $begin) {
    $cal[0][$i++] = undef;
  }

  pop @cal while $cal[-1][0] > $end;

lib/Calendar/Simple.pm  view on Meta::CPAN

  use warnings;
  use Calendar::Simple;

  my @months = qw(January February March April May June July August
                  September October November December);

  my $mon = shift // (localtime)[4] + 1;
  my $yr  = shift // (localtime)[5] + 1900;
  my $sd  = shift // 1;

  my @month = calendar($mon, $yr, $sd);
  print "\n$months[$mon -1] $yr\n\n";

  my @days = qw(Su Mo Tu We Th Fr Sa);
  push @days, splice @days, 0, $sd;
  print "@days\n";

  foreach (@month) {
    print map { $_ ? sprintf "%2d ", $_ : '   ' } @$_;
    print "\n";
  }

lib/Calendar/Simple.pm  view on Meta::CPAN

=head2 Date Range

This module will make use of L<DateTime> if it is installed. By using
L<DateTime> it can use any date that C<DateTime> can represent. If L<DateTime>
is not installed it uses Perl's built-in date handling and therefore
can't deal with dates before 1970 and it will also have problems with dates
after 2038 on a 32-bit machine.

=head2 EXPORT

C<calendar>

=head1 AUTHOR

Dave Cross <dave@mag-sol.com>

=head1 ACKNOWLEDGEMENTS

With thanks to Paul Mison <cpan@husk.org> for the start day patch.

=head1 COPYRIGHT

t/no_dt.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More tests => 33;

$ENV{CAL_SIMPLE_NO_DT} = 1;
use_ok('Calendar::Simple');
use Config;

my @month = calendar(9, 2002);

is(@month, 6);
is(@{$month[0]}, 7);
is($month[0][6], 1);
ok(not defined $month[-1][-1]);
is($#{$month[-1]}, 6);

@month = calendar(2, 2009);
is(@month, 5);
is($month[0][6], 1);
is($month[4][5], 28);
ok(defined $month[-1][-2]);
is($#{$month[-1]}, 6);

@month = calendar(1, 2002);
ok(not defined $month[0][0]);
is($month[0][1], 1);
is($month[4][3], 31);
ok(not defined $month[4][6]);
ok(not defined $month[-1][-1]);
is($#{$month[-1]}, 6);

@month = calendar(1, 2002, 1);
ok(not defined $month[0][0]);
is($month[0][1], 1);
is($month[4][3], 31);
ok(not defined $month[4][4]);
ok(not defined $month[-1][-1]);
is($#{$month[-1]}, 6);

@month = calendar();
ok(@month);

eval { @month = calendar(-1) };
ok($@);

eval { @month = calendar(13) };
ok($@);

eval { @month = calendar(1, 2000, -1) };
ok($@);

eval { @month = calendar(1, 2000, 7) };
ok($@);

@month = calendar(2, 2000);
ok(@month);

SKIP: {
  skip 'Not a problem since perl 5.11.0', 1
    if $] >= 5.011;
  skip 'Not a problem on 64-bit systems', 1
    if defined $Config{use64bitint};

  eval { @month = calendar(2, 2100) };
  ok($@);
}

eval { @month = calendar(2, 1500) };
ok($@);

@month = calendar(2, 2004);
ok(@month);

my $month = calendar();
is(ref $month, 'ARRAY');

t/test.t  view on Meta::CPAN

use strict;
use warnings;

use Test::More tests => 34;
use_ok('Calendar::Simple');

my @month = calendar(9, 2002);

is(@month, 6);
is(@{$month[0]}, 7);
is($month[0][-1], 1);
ok(not defined $month[-1][-1]);
is($#{$month[-1]}, 6);

@month = calendar(2, 2009);
is(@month, 5);
is($month[0][-1], 1);
is($month[4][5], 28);
ok(defined $month[-1][-2]);
is($#{$month[-1]}, 6);

@month = calendar(1, 2002);
ok(not defined $month[0][0]);
is($month[0][1], 1);
is($month[4][3], 31);
ok(not defined $month[4][6]);
ok(not defined $month[-1][-1]);
is($#{$month[-1]}, 6);

@month = calendar(1, 2002, 1);
ok(not defined $month[0][0]);
is($month[0][1], 1);
is($month[4][3], 31);
ok(not defined $month[4][4]);
ok(not defined $month[-1][-1]);
is($#{$month[-1]}, 6);

@month = calendar(2, 2004, 3);
ok(@month);

@month = calendar();
ok(@month);

eval { @month = calendar(-1) };
ok($@);

eval { @month = calendar(13) };
ok($@);

eval { @month = calendar(1, 2000, -1) };
ok($@);

eval { @month = calendar(1, 2000, 7) };
ok($@);

@month = calendar(2, 2000);
ok(@month);

@month = calendar(2, 2004);
ok(@month);

my $month = calendar();
is(ref $month, 'ARRAY');

SKIP: {
  eval { require DateTime };
  skip "DateTime not installed", 2, if $@ || $ENV{CAL_SIMPLE_NO_DT};
  @month = calendar(1,1500);
  ok(@month);

  @month = calendar(2, 2100);
  ok(@month);
}



( run in 0.609 second using v1.01-cache-2.11-cpan-c333fce770f )