Calendar-Simple-v2.0.1000755001750001750 014000030406 13644 5ustar00davedave000000000000Calendar-Simple-v2.0.1/Build.PL000444001750001750 175714000030406 15307 0ustar00davedave000000000000use Module::Build; my $build = Module::Build->new( license => 'perl', dist_author => 'Dave Cross ', module_name => 'Calendar::Simple', requires => { perl => '5.6.0', Carp => 0, 'Time::Local' => 0, Config => 0, }, recommends => { DateTime => 0, }, build_requires => { 'Test::More' => 0, }, build_recommends => { 'Test::Pod' => 0, 'Test::Pod::Coverage' => 0, }, create_makefile_pl => 'traditional', script_files => [ 'bin/pcal' ], meta_merge => { resources => { repository => { type => 'git', url => 'https://github.com/davorg/calendar-simple.git', web => 'https://github.com/davorg/calendar-simple', }, bugtracker => { web => 'https://github.com/davorg/calendar-simple/issues', } }, }, ); $build->create_build_script; Calendar-Simple-v2.0.1/Changes.md000444001750001750 356414000030406 15703 0ustar00davedave000000000000# Changelog All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [2.0.1] 2021-01-14 ### Added - Details of bugtracker ## [2.0.0] 2020-06-22 ### Changed - Code fixes - Refactoring to lower code complexity - Documentation fixes (thanks MANWAR) - Default week start date is now Monday ## [1.23] 2018-02-22 ### Fixed - Packaging ## [1.22] 2018-02-22 ### Fixed - File permissions - Call to `timelocal()` ### Added - CI testing ## [1.21] 2010-04-02 ### Fixed - Tests - Perlcritic issues ## [1.20] 2008-04-19 ### Added - Minimum Perl version ## [1.19] 2008-03-09 ### Added - Licence ## [1.18] 2008-03-08 ### Fixed - "Undef" bugs - Packaging improvements ## [1.17] 2006-10-16 ### Fixed - Packaging of `pcal` example ## [1.16] 2006-10-15 ### Added - `date_span()` function - `pcal` example ## [1.15] 2006-10-15 ### Fixed - More idiomatic Perl - Better tests ## [1.14] 2006-09-10 ### Changed - Switch to manual version number ## [1.13] 2005-05-31 ### Fixes - Build.PL ## [1.12] 2005-03-01 ### Added - Pod coverage tests ## [1.11] 2004-12-04 ### Changed - Skip DT tests if not installed ## [1.10] 2004-10-30 ### Added - More test coverage - Better packaging ## [1.09] 2004-10-23 ### Added - More test coverage ## [1.08] 2003-03-14 ### Added - Support for DateTime (if it's installed) ## [1.07] 2003-02-19 ### Fixed - Problems with short weeks - Better packaging and testing ## [1.06] 2002-07-30 ### Fixed - Fixed the previous fix ## [1.05] 2002-07-30 ### Fixed - "Undefined value" errors ## [1.04] 2002-07-13 ### Changed - Documentation fixes ## [1.03] 2002-07-12 ### Added - More documentation - More tests ## [0.01] 2002-07-12 ### Added - All the things Calendar-Simple-v2.0.1/MANIFEST000444001750001750 34314000030406 15112 0ustar00davedave000000000000Changes.md Makefile.PL Build.PL MANIFEST README lib/Calendar/Simple.pm t/test.t t/pod.t t/no_dt.t t/pod_coverage.t t/date_span.t bin/pcal META.yml Module meta-data (added by MakeMaker) META.json Calendar-Simple-v2.0.1/META.json000444001750001750 224414000030406 15424 0ustar00davedave000000000000{ "abstract" : "Perl extension to create simple calendars", "author" : [ "Dave Cross " ], "dynamic_config" : 1, "generated_by" : "Module::Build version 0.4231", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "Calendar-Simple", "prereqs" : { "build" : { "requires" : { "Test::More" : "0" } }, "configure" : { "requires" : { "Module::Build" : "0.42" } }, "runtime" : { "recommends" : { "DateTime" : "0" }, "requires" : { "Carp" : "0", "Config" : "0", "Time::Local" : "0", "perl" : "v5.6.0" } } }, "provides" : { "Calendar::Simple" : { "file" : "lib/Calendar/Simple.pm", "version" : "v2.0.1" } }, "release_status" : "stable", "resources" : { "license" : [ "http://dev.perl.org/licenses/" ] }, "version" : "v2.0.1", "x_serialization_backend" : "JSON::PP version 4.04" } Calendar-Simple-v2.0.1/META.yml000444001750001750 131514000030406 15252 0ustar00davedave000000000000--- abstract: 'Perl extension to create simple calendars' author: - 'Dave Cross ' build_requires: Test::More: '0' configure_requires: Module::Build: '0.42' dynamic_config: 1 generated_by: 'Module::Build version 0.4231, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Calendar-Simple provides: Calendar::Simple: file: lib/Calendar/Simple.pm version: v2.0.1 recommends: DateTime: '0' requires: Carp: '0' Config: '0' Time::Local: '0' perl: v5.6.0 resources: license: http://dev.perl.org/licenses/ version: v2.0.1 x_serialization_backend: 'CPAN::Meta::YAML version 0.018' Calendar-Simple-v2.0.1/Makefile.PL000444001750001750 100214000030406 15744 0ustar00davedave000000000000# Note: this file was auto-generated by Module::Build::Compat version 0.4231 require 5.006000; use ExtUtils::MakeMaker; WriteMakefile ( 'NAME' => 'Calendar::Simple', 'VERSION_FROM' => 'lib/Calendar/Simple.pm', 'PREREQ_PM' => { 'Carp' => 0, 'Config' => 0, 'Test::More' => 0, 'Time::Local' => 0 }, 'INSTALLDIRS' => 'site', 'EXE_FILES' => [ 'bin/pcal' ], 'PL_FILES' => {} ) ; Calendar-Simple-v2.0.1/README000444001750001750 106614000030406 14664 0ustar00davedave000000000000Calendar::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 Note: The older "perl Makefile.PL; make; make test; make install" sequence will also work. DEPENDENCIES This module has no external dependencies. COPYRIGHT AND LICENCE This script is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Copyright (C) 2002-2018 Magnum Solutions Ltd., All rights reserved. Calendar-Simple-v2.0.1/bin000755001750001750 014000030406 14414 5ustar00davedave000000000000Calendar-Simple-v2.0.1/bin/pcal000555001750001750 106314000030406 15416 0ustar00davedave000000000000#!/usr/bin/perl -w use strict; 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 @month = calendar($mon, $yr); my $mon_name = $months[$mon - 1]; my $pad = int((20 - length("$mon_name $yr")) / 2); print "\n", ' ' x $pad, "$months[$mon - 1] $yr\n"; print "Mo Tu We Th Fr Sa Su\n"; foreach (@month) { print map { $_ ? sprintf "%2d ", $_ : ' ' } @$_; print "\n"; } Calendar-Simple-v2.0.1/lib000755001750001750 014000030406 14412 5ustar00davedave000000000000Calendar-Simple-v2.0.1/lib/Calendar000755001750001750 014000030406 16123 5ustar00davedave000000000000Calendar-Simple-v2.0.1/lib/Calendar/Simple.pm000555001750001750 1502214000030406 20072 0ustar00davedave000000000000# $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.0.1'; 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. =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. 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); while (my @wk = splice @mon, 0, 7) { push @month, \@wk; } $#{$month[-1]} = 6; return wantarray ? @month : \@month; } =head2 date_span This function returns a cut-down version of a month data structure which begins and ends on dates other than the first and last dates of the month. Any weeks that fall completely outside of the date range are removed from the structure and any days within the remaining weeks that fall outside of the date range are set to C. As there are a number of parameters to this function, they are passed using a named parameter interface. The parameters are as follows: =over 4 =item year The required year. Defaults to the current year if omitted. =item mon The required month. Defaults to the current month if omitted. =item begin 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. The default is 1 (for Monday). B As of version 2.0.0, the default C has changed. Previously, it was Sunday; now, it is Monday. This is so the default behaviour matches that of the standard Unix C 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: use Calendar::Simple 'date_span'; =cut 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; $i = -1; while (defined $cal[-1][$i] and $cal[-1][$i] > $end) { $cal[-1][$i--] = undef; } return @cal; } sub _get_first { my ($mon, $year, $start_day) = @_; my $first; if ($dt) { $first = DateTime->new(year => $year, month => $mon, day => 1)->day_of_week % 7; } else { $first = (localtime timelocal 0, 0, 0, 1, $mon -1, $year)[6]; } $first -= $start_day; $first += 7 if ($first < 0); return $first; } sub _days { my ($mon, $yr) = @_; return $days[$mon - 1] unless $mon == 2; return _isleap($yr) ? 29 : 28; } sub _isleap { return 1 unless $_[0] % 400; return unless $_[0] % 100; return 1 unless $_[0] % 4; return; } sub _validate_params { my ($mon, $year, $start_day) = @_; my @now = (localtime)[4, 5]; $mon = ($now[0] + 1) unless $mon; $year = ($now[1] + 1900) unless $year; $start_day = 1 unless defined $start_day; croak "Year $year out of range" if $year < 1970 && !$dt; croak "Month $mon out of range" if ($mon < 1 || $mon > 12); croak "Start day $start_day out of range" if ($start_day < 0 || $start_day > 6); return ($mon, $year, $start_day); } 1; __END__ =head2 EXAMPLE A simple C replacement would therefore look like this: #!/usr/bin/perl use strict; 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 @month = calendar($mon, $yr); print "\n$months[$mon -1] $yr\n\n"; print "Su Mo Tu We Th Fr Sa\n"; foreach (@month) { print map { $_ ? sprintf "%2d ", $_ : ' ' } @$_; print "\n"; } A version of this example, called C, is installed when you install this module. =head2 Date Range This module will make use of L if it is installed. By using L it can use any date that C can represent. If L 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 =head1 AUTHOR Dave Cross =head1 ACKNOWLEDGEMENTS With thanks to Paul Mison for the start day patch. =head1 COPYRIGHT Copyright (C) 2002-2008, Magnum Solutions Ltd. All Rights Reserved. =head1 LICENSE This script is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 SEE ALSO L, L, L =cut Calendar-Simple-v2.0.1/t000755001750001750 014000030406 14107 5ustar00davedave000000000000Calendar-Simple-v2.0.1/t/date_span.t000555001750001750 110314000030406 16365 0ustar00davedave000000000000use strict; use warnings; use Test::More tests => 14; use_ok('Calendar::Simple', 'date_span'); my @span = date_span(year => 2006, mon => 10, begin => 15, end => 28); is(@span, 3); is($span[0][6], 15); is($span[2][5], 28); @span = date_span(year => 2006, mon => 10, begin => 17, end => 24); is(@span, 2); ok(!defined $span[0][0]); is($span[0][1], 17); ok(!defined $span[1][6]); is($span[1][1], 24); @span = date_span(year => 2006, mon => 10); is(@span, 6); ok(defined $span[0][6]); is($span[3][1], 17); ok(!defined $span[-1][6]); is($span[-1][1], 31); Calendar-Simple-v2.0.1/t/no_dt.t000555001750001750 254714000030406 15547 0ustar00davedave000000000000use 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'); Calendar-Simple-v2.0.1/t/pod.t000555001750001750 23414000030406 15175 0ustar00davedave000000000000use strict; use warnings; use Test::More; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; all_pod_files_ok(); Calendar-Simple-v2.0.1/t/pod_coverage.t000555001750001750 27414000030406 17054 0ustar00davedave000000000000use strict; use warnings; use Test::More; eval "use Test::Pod::Coverage 1.00"; plan skip_all => "Test::Pod::Coverage 1.00 required for testing POD coverage" if $@; all_pod_coverage_ok(); Calendar-Simple-v2.0.1/t/test.t000555001750001750 246514000030406 15422 0ustar00davedave000000000000use 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); }