|
Calendar - Perl extension for calendar convertion |
Calendar - Perl extension for calendar convertion
use Calendar;
my $date = Calendar->new_from_Gregorian(12, 16, 2006);
print $date->date_string("Gregorian date: %M %W %d %Y"), "\n";
my $newdate = $date + 7;
print $date->date_string("Gregorian date of next week: %D"), "\n";
$newdate = $date-7;
print $newdate->date_string("Absolute date of last week: %A\n");
my $diff = $date-$newdate;
printf "There is %d days between %s and %s\n",
$diff, $date->date_string("%D"), $newdate->date_string("%D");
$date->convert_to_Julian;
print $date->date_string("Julian date: %M %W %d %Y"), "\n";
Calendar is a class for calendar convertion or calculation. The algorithm is from emacs calendar library. Most functions of this class is simply rewrite from elisp to perl.
use Calendar::Gregorian;
Calendar::Gregorian->new(732662);
Calendar::Gregorian->new(12, 17, 2006);
Calendar::Gregorian->new(-year=>2006, -month=>12, -day=>17);
use Calendar;
Calendar->new_from_Gregorian(732662);
Calendar->new_from_Gregorian(12, 17, 2006);
Calendar->new_from_Gregorian(-year=>2006, -month=>12, -day=>17);
Calendar object can convert from each other. The function is name `convert_to_Package'. For example:
$date = Calendar->new_from_Gregorian(12, 17, 2006);
$date->convert_to_Julian;
Now $date is a Julian calendar date. If you want maintain $date not change, use Calendar->new_from_Julian($date->absolute_date) instead.
Calendar overload several operator.
$newdate = $date + 1;
The $newdate is next day of $date. You CANNOT add a date to another date.
$newdate = $date - 1;
The $newdate is the last day of $date. When a date substract from another date, returns the days between the two date. For example:
$newdate = $date + 7;
$days = $newdate - $date; # $days is 7
The return value is different type, you should always be careful.
if ( $date2 > $date1 ) {
print "$date2 is after $date1.\n";
}
else {
print "$date1 is after $date2.\n";
}
Every calendar class has a format template: $default_format. You can set the template. The format function is `date_string'. The format specifications as following:
%% PERCENT %A Absoute date %d numeric day of the month, with leading zeros (eg 01..31) %D MM/DD/YY %m month number, with leading zeros (eg 01..31) %M month numer %W day of the week %Y year
For chinese calendar, the following specifications are available:
%S sexagesimal name, eg. "丙戌" %D day name, eg. "初二" %Z zodiac name, eg. "狗" %m month name in chinese, eg. "十一月" %W week day name in chinese, eg. "星期一"
Ye Wenbin <wenbinye@gmail.com>
Copyright (C) 2006 by ywb
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.7 or, at your option, any later version of Perl 5 you may have available.
|
Calendar - Perl extension for calendar convertion |