Date::ICal(3)

πŸ“¦
version 2.682 Β· 2 versions
, in fedora 43

πŸŽ€ 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 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 goes and looks at the environment variable $TZ, if it exists, to determine your local offset. And if $TZ isn'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 +0200 now 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 the ical() 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 $X is another Date::ICal object (or subclass thereof) then $d will be a Date::ICal::Duration object:
$week = $d->weeks; # how many weeks apart?
$days = $d->as_days; # How many days apart?
  • If $X is a duration string, or a Date::ICal::Diration object, then $d will 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 gmtime and localtime methods, perhaps?
  • Fix the INTERNALS file 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 🫢


Β© 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