Calendar-Simple-v2.1.0000755001750001750 014615412115 13661 5ustar00davedave000000000000Calendar-Simple-v2.1.0/Build.PL000444001750001750 204514615412115 15313 0ustar00davedave000000000000use Module::Build; my $build = Module::Build->new( license => 'perl', dist_author => 'Dave Cross ', module_name => 'Calendar::Simple', requires => { perl => '5.10.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 => { '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; Calendar-Simple-v2.1.0/Changes.md000444001750001750 434414615412115 15715 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.1.0] 2024-05-04 ### Changed - Require Perl 5.10 (for // in `pcal`) - Better variable names in `pcal` - Better argument handling logic in `pcal` ### Fixed - Fixed bug in example code (thanks exzr8u92) ## [2.0.3] 2023-08-01 ### Added - Improvements to `pcal` ## [2.0.2] 2023-07-27 ### Fixed - Regenerated package so it includes repo and bugtracker info ## [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.1.0/MANIFEST000444001750001750 34314615412115 15127 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.1.0/META.json000444001750001750 272014615412115 15440 0ustar00davedave000000000000{ "abstract" : "Perl extension to create simple calendars", "author" : [ "Dave Cross " ], "dynamic_config" : 1, "generated_by" : "Module::Build version 0.4234", "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.10.0" } } }, "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" } Calendar-Simple-v2.1.0/META.yml000444001750001750 152314615412115 15270 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.4234, 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.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' Calendar-Simple-v2.1.0/Makefile.PL000444001750001750 100214615412115 15761 0ustar00davedave000000000000# Note: this file was auto-generated by Module::Build::Compat version 0.4234 require 5.010000; 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.1.0/README000444001750001750 106614615412115 14701 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.1.0/bin000755001750001750 014615412115 14431 5ustar00davedave000000000000Calendar-Simple-v2.1.0/bin/pcal000555001750001750 132214615412115 15431 0ustar00davedave000000000000#!/usr/bin/perl use strict; use warnings; use v5.10.0; use Calendar::Simple; 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 ", $_ : ' ' } @$_; print "\n"; } Calendar-Simple-v2.1.0/lib000755001750001750 014615412115 14427 5ustar00davedave000000000000Calendar-Simple-v2.1.0/lib/Calendar000755001750001750 014615412115 16140 5ustar00davedave000000000000Calendar-Simple-v2.1.0/lib/Calendar/Simple.pm000444001750001750 1515414615412115 20112 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.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. =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 $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"; } 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.1.0/t000755001750001750 014615412115 14124 5ustar00davedave000000000000Calendar-Simple-v2.1.0/t/date_span.t000444001750001750 110314615412115 16377 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.1.0/t/no_dt.t000444001750001750 254714615412115 15561 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.1.0/t/pod.t000444001750001750 23414615412115 15207 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.1.0/t/pod_coverage.t000444001750001750 27414615412115 17066 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.1.0/t/test.t000444001750001750 246514615412115 15434 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); }