Date::ICal(3)
π Date::ICal β from the User Contributed Perl Documentation π
okay bestie, sit down, hydrate, and let me tell you about Date::ICal, a whole module that speaks ICal date format β¨ (it's living in the User Contributed Perl Documentation section, section 3, iykyk)
β¨ NAME β¨
Date::ICal β it's a Perl extension for ICalendar date objects, no cap ποΈπ
πΈ VERSION πΈ
$Revision: 682 $
π¦ SYNOPSIS π¦
here's the whole vibe in code form, bestie, read it and weep (affectionately):
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. yes that's a real thing, look it up π)
$ical += 'P6DT12H';
$duration = $ical - $ical2;
$ical3 = $ical - $duration;
like... the math just works. date arithmetic that doesn't make you cry?? we love to see it π«Άβ¨
π DESCRIPTION π
Date::ICal talks the ICal date format. that's its whole thing, that's its personality. it's intended to be a base class for other date/calendar modules that also know about ICal time format ποΈπ«
so like β it's the main character, and the other modules get to build on top of it. iconic behaviour honestly.
π AUTHOR π
Rich Bowen, and the Reefknot team. π alas, Reefknot is no more β rip to a real one π―οΈ
for more modern and accurate modules, go peek at https://github.com/houseabsolute/DateTime.pm/wiki π
last touched by $Author: michal-josef-spacek $ β¨
okay we are SO not done here, bestie β part 2 is coming, don't go anywhere π πΈ
π METHODS π
okay bestie, here's the whole menu of what Date::ICal can do for you:
π new
You can spin up a fresh Date::ICal object out of any valid ICal string:
my $ical = Date::ICal->new( ical => '19971024T120000' );
# will default to the timezone specified in $TZ, see below
Or out of any epoch time:
my $ical = Date::ICal->new( epoch => time );
Or β and honestly, better still β build it from components, which is the main character way to do it:
my $date = Date::ICal->new(
day => 25,
month => 10,
year => 1066,
hour => 7,
min => 15,
sec => 47
);
Call new with no arguments at all and you get a Date::ICal object set to the time right now, no thoughts head empty:
my $ical = Date::ICal->new();
Already got an object β a Date::ICal, or some other subclass of it? You can create a new Date::ICal (or subclass) object using that object as your starting point. This is particularly useful for converting from one calendar to another, and yes it goes hard:
# 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 too, because of course it does. It defaults times to UTC (Greenwich Mean Time, also called Zulu). Want a time in the US "Pacific" timezone, which is GMT-8? Do something like:
my $ical = Date::ICal->new( ical => '19971024T120000',
offset => "-0800");
π«Ά bestie listen β as of version1.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 goes and looks at the environment variable$TZ, if it exists, to determine your local offset. And if$TZisn't set?new()will complain. π
π ical
$ical_string = $ical->ical;
Retrieves, or sets, the date on the object using any valid ICal date/time string. Output is in UTC by default (it ends with a "Z", cute). Wanna get the output in localtime relative to the current machine? Do:
$ical_string = $ical->ical( localtime => 1 );
Wanna get output relative to some arbitrary offset instead? Do:
$ical_string = $ical->ical( offset => '+0545' );
π epoch
$epoch_time = $ical->epoch;
$ical->epoch( 98687431 );
Sets, or retrieves, the epoch time represented by the object β if it's representable as such. (Dates before 1971 or after 2038 will not have an epoch representation. sobbing π)
π§ Internals note: The ICal representation of the date is considered the only authoritative one. That means we may need to reconstruct the epoch time from the ICal representation if we're not sure the two are in synch. We'll need to do clever things to keep track of when the two may not be in synch. And, of course, the same will go for any subclasses of this class.
π offset_to_seconds
$seconds_plus_or_minus = offset_to_seconds($offset);
Changes -0600 to -21600. Not an object method, no side-effects. Just a lil guy doing math. β¨
π offset_from_seconds
$seconds_plus_or_minus = offset_from_seconds($offset_in_seconds);
Changes -18000 (seconds) to -0600 (hours, minutes). Not an object method, no side-effects.
π offset
$offset = $ical->offset;
# We need tests for these.
$ical->offset( '+1100' ); # a number of hours and minutes: UTC+11
$ical->offset( 0 ); # reset to UTC
Sets or retrieves the offset from UTC for this time. This is how you get timezone support β assuming you actually know what your local (or non-local) UTC offset is. Defaults to 0.
π§ Internals note: all times are internally stored in UTC, even though they may have some offset information. Offsets are internally stored in signed integer seconds.
π¨ BE CAREFUL about using this function on objects that were initialized with an offset. If you started an object with:perl my $d = new(ical=>'19700101120000', offset=>'+0100');and you then call:perl $d->offset('+0200');you'll be saying "Yeah, I know I said it was in+0100, but really I want it to be in+0200now and forever." Which may totally be your intention β if you're trying to transpose a whole set of dates to another timezone β but you can also do that at the presentation level with theical()method. Either way will work. your call bestie π
π add
$self->add( year => 3, month => 2, week => 1, day => 12,
hour => 1, min => 34, sec => 59 );
$date->add( duration => 'P1WT1H1M1S' ); # add 1 wk, 1 hr, 1 min, and 1 sec
Adds a duration to a Date::ICal object. Obsessed.
The supported parameters are: duration, eom_mode, year, month, week, day, hour, min, sec or seconds.
'duration' is an ICalendar duration string (see duration_value).
If a value is undefined or omitted, 1 is assumed:
$ical->add( 'min' ); # add a minute
The result will be normalized. That is, the output time will have meaningful values, rather than being 48:73 pm on the 34th of hexadecember. π not the hexadecember.
Adding months or years can be done via three different methods, specified by the eom_mode parameter β which then applies to all additions (or subtractions) of months or years following it in the parameter list.
eom_mode => 'wrap'β this is the default. Adding months or years that result in days beyond the end of the new month will roll over into the following month. For instance, adding one year to Feb 29 will result in Mar 1.eom_mode => 'limit'β the end of the month is never crossed. So adding one year to Feb 29, 2000 will result in Feb 28, 2001. However, adding three more years will result in Feb 28, 2004, not Feb 29.eom_mode => 'preserve'β the same calculation is done as for'limit', except that if the original date is at the end of the month, the new date will also be. For instance, adding one month to Feb 29, 2000 will result in Mar 31, 2000.
All additions are performed in the order specified. For instance, with the default setting of eom_mode => 'wrap', adding one day and one month to Feb 29 will result in Apr 1, while adding one month and one day will result in Mar 30. order matters, periodt. π
π add_overload
$date = $date1 + $duration;
Where $duration is either a duration string, or a Date::ICal::Duration object.
$date += 'P2DT4H7M';
Adds a duration to a date object. Returns a new object, or, in the case of +=, modifies the existing object.
π duration_value
Given a duration string, this function returns the number of days, seconds, and months represented by that duration. In that order. Seems odd to me. This should be considered an internal function, and you should expect the API to change in the very near future. consider this your soft launch π§
π subtract
$duration = $date1 - $date2;
Subtract one Date::ICal object from another to give a duration β the length of the interval between the two dates. The return value is a Date::ICal::Duration object (qv) and it lets you get at each of the individual components, or the entire duration string:
$d = $date1 - $X;
Note that $X can be any of the following:
- If
$Xis anotherDate::ICalobject (or subclass thereof) then$dwill be aDate::ICal::Durationobject:
$week = $d->weeks; # how many weeks apart?
$days = $d->as_days; # How many days apart?
- If
$Xis a duration string, or aDate::ICal::Dirationobject, then$dwill be an object in the same class as$date1:
$newdate = $date - $duration;
π clone
$copy = $date->clone;
Returns a replica of the date object, including all attributes. twin where have you been π¦
π compare
$cmp = $date1->compare($date2);
@dates = sort {$a->compare($b)} @dates;
Compare two Date::ICal objects. Semantics are compatible with sort; returns -1 if $a < $b, 0 if $a == $b, 1 if $a > $b.
π day
my $day = $date->day;
Returns the day of the month.
Day is in the range 1..31.
π month
my $month = $date->month;
Returns the month of the year.
Month is returned as a number in the range 1..12.
π year
my $year = $date->year;
Returns the year.
π jd2greg
($year, $month, $day) = jd2greg( $jd );
Convert number of days on or after Jan 1, 1 CE (Gregorian) to
gregorian year,month,day.
π greg2jd
$jd = greg2jd( $year, $month, $day );
Convert gregorian year,month,day to days on or after Jan 1, 1 CE
(Gregorian). Normalization is performed (e.g. month of 28 means
April two years after given year) for month < 1 or > 12 or day < 1
or > last day of month.
π days_this_year
$yday = Date::ICal::days_this_year($day, $month, $year);
Returns the number of days so far this year. Analogous to the yday attribute of gmtime (or localtime) β except that it works outside of the epoch. slay π
π day_of_week
my $day_of_week = $date->day_of_week
Returns the day of week as 0..6 (0 is Sunday, 6 is Saturday).
π hour
my $hour = $date->hour
Returns the hour of the day.
Hour is in the range 0..23.
π min
my $min = $date->min;
Returns the minute.
Minute is in the range 0..59.
π sec
my $sec = $date->sec;
Returns the second.
Second is in the range 0..60. The value of 60 is (maybe) needed for leap seconds. But I'm not sure if we're going to go there. π
π julian
my $jd = $date->jd;
Returns a listref containing two elements. The date as a julian day, and the time as the number of seconds since midnight. This should not be thought of as a real julian day, because it's not. The module is internally consistent, and that's enough.
This method really only is here for compatibility with previous versions, as the jd method is now thrown over for plain hash references.
See the file INTERNALS for more information about this internal format.
π TODO π
- add
gmtimeandlocaltimemethods, perhaps? - Fix the
INTERNALSfile so that it actually reflects reality
π INTERNALS π
Please see the file INTERNALS for discussion on the internals.
π AUTHOR π
Rich Bowen (DrBacchus) rbowen@rcbowen.com
And the rest of the Reefknot team. See the source for a full list of patch contributors and version-by-version notes. the whole squad fr fr π«Ά
π LICENSE AND COPYRIGHT π
Β© 2001-2022 Rich Bowen
Β© 2022-2023 Michal Josef Ε paΔek
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
π SEE ALSO π
datetime@perl.org mailing list
<https://github.com/houseabsolute/DateTime.pm/wiki>
Time::Local
Net::ICal
and that's the whole page bestie π
this was perl v5.42.0, dated 2025-07-25, and you just finished reading Date::ICal(3). go forth and manipulate some timezones, you absolute icon β¨π
π¦ where this page comes from
- fedora 43: perl-Date-ICal 2.682-7.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms