Date-ICal
view release on metacpan or search on metacpan
lib/Date/ICal.pm view on Meta::CPAN
# $Rev: 682 $
package Date::ICal;
use strict;
use warnings;
use vars qw($VERSION $localzone $localoffset @months @leapmonths %add_units);
$VERSION = '2.'.(qw'$Rev: 682 $')[1];
use Carp;
use Time::Local;
use Date::Leapyear qw();
use Date::ICal::Duration;
use overload '<=>' => 'compare',
'fallback' => 1,
'-' => \&subtract,
'+' => \&add_overload;
$localzone = $ENV{TZ} || 0;
$localoffset = _calc_local_offset();
# Documentation {{{
=encoding utf8
=head1 NAME
Date::ICal - Perl extension for ICalendar date objects.
=head1 VERSION
$Revision: 682 $
=head1 SYNOPSIS
use Date::ICal;
$ical = Date::ICal->new( ical => '19971024T120000' );
$ical = Date::ICal->new( epoch => time );
$ical = Date::ICal->new( year => 1964,
month => 10, day => 16, hour => 16,
min => 12, sec => 47 );
$hour = $ical->hour;
$year = $ical->year;
$ical_string = $ical->ical;
$epoch_time = $ical->epoch;
$ical2 = $ical + $duration;
(Where $duration is either a duration string, like 'P2W3DT7H9M', or a
Date::ICal::Duration (qv) object.
$ical += 'P6DT12H';
$duration = $ical - $ical2;
$ical3 = $ical - $duration;
=head1 DESCRIPTION
Date::ICal talks the ICal date format, and is intended to be a base class for
other date/calendar modules that know about ICal time format also.
=head1 AUTHOR
Rich Bowen, and the Reefknot team. Alas, Reefknot is no more. See
L<https://github.com/houseabsolute/DateTime.pm/wiki> for more modern and accurate modules.
Last touched by $Author: michal-josef-spacek $
=head1 METHODS
Date::ICal has the following methods available:
=head2 new
A new Date::ICal object can be created with any valid ICal string:
my $ical = Date::ICal->new( ical => '19971024T120000' );
# will default to the timezone specified in $TZ, see below
Or with any epoch time:
my $ical = Date::ICal->new( epoch => time );
Or, better still, create it with components
my $date = Date::ICal->new(
day => 25,
month => 10,
year => 1066,
hour => 7,
min => 15,
sec => 47
);
If you call new without any arguments, you'll get a Date::ICal object that is
set to the time right now.
my $ical = Date::ICal->new();
If you already have an object in Date::ICal, or some other subclass
thereof, you can create a new Date::ICal (or subclass) object using
that object to start with. This is particularly useful for converting
from one calendar to another:
# Direct conversion from Discordian to ISO dates
my $disco = Date::Discordian->new( disco => '12 Chaos, YOLD 3177' );
my $iso = Date::ISO->new( $disco );
print $iso->iso;
new() handles timezones. It defaults times to UTC (Greenwich
Mean Time, also called Zulu). If you want to set up a time
that's in the US "Pacific" timezone, which is GMT-8, use something
like:
my $ical = Date::ICal->new( ical => '19971024T120000',
offset => "-0800");
Note that as of version 1.44, new() tries to be intelligent
about figuring out your local time zone. If you enter
a time that's not *explicitly* in UTC, it looks at
the environment variable $TZ, if it exists, to determine
your local offset. If $TZ isn't set, new() will complain.
=cut
#}}}
#{{{ sub new
sub new {
my $class = shift;
my ( $self, %args, $sec, $min, $hour, $day, $month, $year, $tz );
# $zflag indicates whether or not this time is natively in UTC
my $zflag = 0;
# First argument can be a Date::ICal (or subclass thereof) object
if ( ref $_[0] ) {
$args{ical} = $_[0]->ical;
} else {
%args = @_;
}
$self = {};
# Date is specified as epoch#{{{
if ( defined( $args{epoch} ) ) {
( $sec, $min, $hour, $day, $month, $year ) =
( gmtime( $args{epoch} ) )[ 0, 1, 2, 3, 4, 5 ];
$year += 1900;
$month++;
$zflag = 1; # epoch times are by definition in GMT
} #}}}
# Date is specified as ical string#{{{
elsif ( defined( $args{ical} ) ) {
# Timezone, if any
$args{ical} =~ s/^(?:TZID=([^:]+):)?//;
$tz = $1;
( run in 1.020 second using v1.01-cache-2.11-cpan-5a3173703d6 )