=head1 NAME Date::Gregorian - dates on the Gregorian Calendar =head1 SYNOPSIS # note: -DateParse uses Date::Parse and requires Perl 5 embedding. use Date::Gregorian qw(date localdate gmdate now -DateParse); # creating non-timezones date object ('floating' time) $date = Date::Gregorian.new($year,$month,$day,$hour,$min,$sec); $date = date [$year,$month,$day,$hour,$min,$sec]; $date = date ( :year($year), :month($month), :day($day), :hour($hour), :min($min), :sec($sec) ); $date = date "2001-11-12 07:13:12"; # or any ISO-8601 format $date = date 1e9; # Unix epoch time if an Int $date = date 0.0; # Perl 6 date if a Float # add a month to it: $date += "1M"; $date += "P1M"; # ISO-8601 Periods supported $date += [0,1,0,0,0,0]; # constructor formats supported $date += 31*24*60*60; # seconds # intuitive arithmetic between dates via Duration::Gregorian $days_between = ( date('2001-11-12') - date('2001-07-04') ).days; # prints 131 # creating date object in local timezone $date = localdate "2001-12-11"; # force local time $date = now; # the same as date(time) # creating absolute date object (UTC) $date = Date::Gregorian.new([$yyyy,$mm,$dd,$HH,$MM,$SS], 'UTC'); $date = gmdate "2001-11-12 17:13"; # creating absolute date object in any other timezone $date = date [$year,$month,$day,$hour,$min,$sec], 'Iceland'; $date = date "2001-11-12 17:13", 'Iceland'; # getting parts out - comprehensive methods available my $year = $date.year; # full my $year_C = $date._year; # year - 1900 (or 1904 on Mac?) my $month = $date.mon; # 1..12 my $month_C = $date._mon; # 0..11 ($year,$month,$day,$hour,$min,$sec)=$date.array; print date([2001,12,11,4,5,6]).truncate; # will print "2001-12-11" # modifying date properties $date.set( :year<1977>, :sec<14> ); $date.year = 1978; $date.sec = 15; # constructing new date based on an existing one: $new_date = $date.clone; $new_date.clone( :year<1977>, :sec<14> ); # constructing a new date, which is the same absolute time as # the original, but expressed in another timezone: $new_date = $date.as_tz('Iceland'); # comparison between absolute dates say $date1 > $date2 ?? "I am older" !! "I am younger"; # comparison between relative dates say $reldate1 > $reldate2 ?? "I am faster" !! "I am slower"; # Adding / Subtracting months and years are sometimes # tricky; in general be sure whether you mean "one month" # or "30 days", etc: say date("2001-01-29") + '1M' - '1M'; # gives "2001-02-01" say date("2000-02-29") + '1Y' - '1Y'; # gives "2000-03-01" =head1 DESCRTIPTION B represents dates on the Gregorian calendar as Perl 6 objects. You can use the C<+>, C<->, C> and C> operators as with native perl data types. =head2 OVERVIEW Date::Gregorian operations fall into these categories: =over =item * creating a new date object from some representation of a date =item * performing operations on a date object (+, -, comparison) =item * getting results, or parts of results back =back =head1 Creating a new Date::Gregorian object You can create a date object by the (exported by default) "date", "localdate" or "gmdate" functions, or by calling the C constructor. C and C are equivalent, both have two arguments (each of which may be composed of multiple parts): The date and (optionally) the timezone. $date1 = date [2000,11,12]; $date2 = Date::Gregorian->new([2000,06,11,13,11,22],'GMT'); $date2 = $date1->new([2000,06,11,13,11,22]); You can also supply the date information in any of the following forms: $date = date("2003-12-31 12:56:59"); $date = date("2003-12-31 12:56:59 +12:00"); $date = date([$year,$month,$day,$hour,$min,$sec]); $date = date( :year(2003), :month(12), :day(31), :hour(12), :min(56), :sec(59), :tz ); See L for more information on the allowable formats of the date. Dates may be I using the constructor, or the C method: $date2 = $date.new(); $date2 = $date.clone(); $date2 = $date.clone( :year($newyear), :sec($newsec) ); =head2 How the Timezone is determined When constructing a new date from scratch, if the timezone information is omitted, then the date is "floating" (unless you set C<$Date::Gregorian::DEFAULT_TIMEZONE>). If the date is being extracted from the system clock, then it always has a time zone set. You can override this behaviour to always assume the local time zone, by setting C<$Date::Gregorian::DEFAULT_TIMEZONE> to C<$Date::Gregorian::LOCAL_TIMEZONE>. C is equivalent to C, and C is equivalent to C $date1 = localdate [2000,11,12]; $date2 = gmdate [2000,4,2,3,33,33]; $date = localdate(time); The I