叶文彬 > Calendar-Any > Calendar::Any

Download:
Calendar-Any-0.5.tar.gz

Dependencies

Annotate this POD

CPAN RT

New  2
Open  0
View/Report Bugs
Module Version: 0.5   Source  

NAME ^

Calendar::Any - Perl extension for calendar convertion

VERSION ^

version 0.5

SYNOPSIS ^

   use Calendar::Any;
   my $date = Calendar::Any->new_from_Gregorian(12, 16, 2006);
   print $date->date_string("Gregorian date: %M %W %d %Y"), "\n";

   my $newdate = $date + 7;
   print $newdate->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->to_Julian;
   print $date->date_string("Julian date: %M %W %d %Y"), "\n";

DESCRIPTION ^

Calendar::Any 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.

Constructor

new

All class of Calendar should accept absolute date to construct the object. Other type of argument may also be acceptable. For example:

    use Calendar::Any::Gregorian;
    Calendar::Any::Gregorian->new(732662);
    Calendar::Any::Gregorian->new(12, 17, 2006);
    Calendar::Any::Gregorian->new(-year=>2006, -month=>12, -day=>17);
new_from_{Module}

Calendar::Any has AUTOLOAD function that can automatic call new function from package. So the following construct are also valid:

    use Calendar::Any;
    Calendar::Any->new_from_Gregorian(732662);
    Calendar::Any->new_from_Gregorian(12, 17, 2006);
    Calendar::Any->new_from_Gregorian(-year=>2006, -month=>12, -day=>17);

Convertion

Calendar::Any object can convert from each other. The function is name `to_{Module}'. For example:

    $date = Calendar::Any->new_from_Gregorian(12, 17, 2006);
    $date->to_Julian;

Now $date is a Julian calendar date. If you want maintain $date not change, use Calendar::Any->new_from_Julian($date->absolute_date) instead.

Operator

Calendar overload several operator.

+

A Calendar object can add a number of days. For example:

    $newdate = $date + 1;

The $newdate is next day of $date. You CANNOT add a date to another date.

-

If a date substract from a number of days, that means the date before the number of days. For example:

    $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
<=>

Two date can compare from each other. For example:

    if ( $date2 > $date1 ) {
        print "$date2 is after $date1.\n";
    } else {
        print "$date2 is before $date1.\n";
    }    
""

That means you can simply print the date without explicitly call a method. For detail, read "Format date" section.

Format date

Every calendar class has a format template: $default_format. You can set the template. For example:

   Calendar::Any::Gregorian::$default_format = "%F";
   print Calendar::Any::Gregorian::today();  # 2012-05-18

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)
   %F       YYYY-MM-DD
   %D       MM/DD/YYYY
   %m       month number, with leading zeros (eg 01..31)
   %M       month name
   %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. "星期一"

Meanwhile, %Y, %m and %d now stand for Gregorian year, month and day.

Other method

absoute_date

The number of days elapsed between the Gregorian date 12/31/1 BC. The Gregorian date Sunday, December 31, 1 BC is imaginary.

astro_date

Astronomers use a simple counting of days elapsed since noon, Monday, January 1, 4713 B.C. on the Julian calendar. The number of days elapsed is called the "Julian day number" or the "Astronomical day number".

new_from_Astro

There is no package Calendar::Any::Astro. use new_from_Astro and astro_date to convert between other type of calendar and astro calendar.

today

Get the current date of local time.

weekday

The weekday number. 0 for sunday and 1 for monday.

weekday_name

The full name of the weekday.

month

The number of month, range from 1 to 12.

month_name

The full name of month.

day

The number of day in the month. The first day in the month is 1.

year

The year number.

AUTHOR ^

Ye Wenbin <wenbinye@gmail.com>

SEE ALSO ^

Calendar::Any::Gregorian, Calendar::Any::Julian, Calendar::Any::Chinese

COPYRIGHT ^

Copyright (c) 2012 by Ye Wenbin

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)
syntax highlighting: