The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Date::Simple - a simple date object

SYNOPSIS

    my $date  = Date::Simple->new('1972-01-17');
    my $year  = $date->year;
    my $month = $date->month;
    my $day   = $date->day;
    my $date2 = Date::Simple->new($year, $month, $day);

    my $today = Date::Simple->new;
    my $tomorrow = $today + 1;
    print "Tomorrow's date (in ISO 8601 format) is $tomorrow.\n";
    if ($tomorrow->year != $today->year) {
        print "Today is New Year's Eve!\n";
    }

    if ($today > $tomorrow) {
        die "warp in space-time continuum";
    }

    # you can also do this:
    ($date cmp "2001-07-01")
    # and this
    ($date <=> [2001, 7, 1])

DESCRIPTION

This module may be used to create simple date objects. It only handles dates within the range of Unix time. It will only allow the creation of objects for valid dates. Attempting to create an invalid date will return undef.

CONSTRUCTOR

new

    my $date = Date::Simple->new('1972-01-17');
    my $otherdate = Date::Simple->new(2000, 12, 25);

The new method will return a date object if the values passed in specify a valid date. If an invalid date is passed, the method returns undef.

INSTANCE METHODS

next

    my $tomorrow = $today->next;

Returns an object representing tomorrow.

prev

    my $yesterday = $today->prev;

Returns an object representing yesterday.

year

    my $year  = $date->year;

Return the year of the date held in this date object

month

    my $month = $date->month;

Return the month of the date held in this date object

day

    my $day   = $date->day;

Return the day of the date held in this date object

format

Returns a string representing the date, in the format specified. If you don't pass a parameter, an ISO 8601 formatted date is returned.

    my $change_date = $date->format("%d %b %y");
    my $iso_date1 = $date->format("%Y-%m-%d");
    my $iso_date2 = $date->format;

The formatting parameter is similar to one you would pass to strftime(3). This is because we actually do pass it to strftime to format the date.

OPERATORS

Some operators can be used with Date::Simple instances:

  • You can increment or decrement a date by a number of days using the += and -= operators

  • You can construct new dates offset by a number of days using the + and - operators.

  • You can subtract two dates ($d1 - $d2) to find the number of days between them.

  • You can compare two dates using the arithmetic comparison operators.

  • You can interpolate a date instance directly into a string, in the format specified by ISO 8601 (eg: 2000-01-17).

AUTHOR

Marty Pauley <marty@kasei.com>

COPYRIGHT

  Copyright (C) 2001  Kasei

  This program is free software; you can redistribute it and/or modify it
  under the terms of either:
  a) the GNU General Public License;
     either version 2 of the License, or (at your option) any later version.
  b) the Perl Artistic License.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  or FITNESS FOR A PARTICULAR PURPOSE.