=begin pod

=TITLE class Date

=SUBTITLE Calendar date

    class Date { }

A C<Date> is an immutable object identifying a day in the Gregorian calendar.

Date objects support addition and subtraction of integers, where an
integer is interpreted as the number of days. You can compare Date objects
with the numeric comparison operators C<< ==, <, <=, >, >=, != >>.
Their stringification in C<YYYY-MM-DD> format means that comparing them
with the string operators C<eq, lt, le> etc. also gives the right result.

C<Date.today> creates
an object the current day according to the system clock.

    my $d = Date.new(2015, 12, 24); # Christmas Eve!
    say $d;                         # 2015-12-24
    say $d.year;                    # 2015
    say $d.month;                   # 12
    say $d.day;                     # 24
    say $d.day-of-week;             # 1  (that's Monday)
    say $d.later(days => 20);       # 2016-01-13
    my $n = Date.new('2015-12-31'); # New Year's Eve
    say $n - $d;                    # 7 days between New Years/Christmas Eve
    say $n + 1;                     # 2016-01-01

=head1 Methods

=head2 method new

Defined as:

    proto method new() {*}
    multi method new($year, $month, $day,:&formatter) returns Date:D
    multi method new(:$year!, :$month = 1, :$day = 1) returns Date:D
    multi method new(Str $date)                       returns Date:D
    multi method new(Instant:D $dt)                   returns Date:D
    multi method new(DateTime:D $dt)                  returns Date:D

Usage:

    $date = Date.new(YEAR, MONTH, DAY, FORMATTER?)
    $date = Date.new(year => YEAR, month => MONTH?, day => DAY?,FORMATTER?)
    $date = Date.new(YYYY-MM-DD,FORMATTER?)
    $date = Date.new(now,FORMATTER?)
    $date = Date.new(DateTime.now,FORMATTER?)

Creates a new C<Date> object, either from a triple of (year, month, day)
that can be coerced to integers, or from a string of the form C<YYYY-MM-DD>
(L<ISO 8601|http://en.wikipedia.org/wiki/ISO_8601>), or from an Instant
or DateTime object.  Optionally accepts a formatter as a named parameter.

=head2 method today

Defined as:

    method today(:&formatter) returns Date:D

Usage:

    Date.today

Returns a C<Date> object for the current day.  Optionally accepts a
formatter named parameter.

=head2 method later

Defined as:

    method later(Date:D: *%unit)

Usage:

    DATE.later(DELTA)

Returns a Date object based on the current one, but with a date delta
applied. The date delta can be passed as a named argument where the argument
name is the unit.

Allowed units are C<day>, C<days>, C<week>, C<weeks>, C<month>, C<months>,
C<year>, C<years>.  Please note that the plural forms can only be used with
the C<later> method.

Please note that the special ":2nd" named parameter syntax can be a compact
and self-documenting way of specifying the delta

    say Date.new('2015-12-24').later(:2years);
        # 2017-12-24

Since addition of several different time units is not commutative, only one
unit may be passed.

    my $d = Date.new('2015-02-27');
    say $d.later(month => 1).later(:2days);  # 2015-03-29
    say $d.later(days => 2).later(:1month);  # 2015-04-01
    say $d.later(days => 2).later(:month);   # same, as +True === 1

Negative offsets are allowed, though L<#method earlier> is more idiomatic for
that.

=head2 method earlier

Defined as:

    method earlier(Date:D: *%unit)

Usage:

    DATE.earlier(DELTA)

Returns a Date object based on the current one, but with a date delta
towards the past applied. See L<#method later> for usage.

=head2 method truncated-to

Defined as:

    method truncated-to(Date:D: Cool $unit)

Usage:

    DATE.truncated-to(UNIT)

Returns a C<Date> truncated to the first day of its year, month or week.
For example

    my $c = Date.new('2012-12-24');
    say $c.truncated-to('year');     # 2012-01-01
    say $c.truncated-to('month');    # 2012-12-01
    say $c.truncated-to('week');     # 2012-12-24  (because it's Monday already)

=head2 method succ

Defined as:

    method succ(Date:D:) returns Date:D

Usage:

   DATE.succ

Returns a C<Date> of the following day.

=head2 method pred

Defined as:

    method pred(Date:D:) return Date:D

Usage:

    DATE.pred

Returns a C<Date> of the previous day.

=head2 method Str

Defined as:

    multi method Str(Date:D:) returns Str:D

Usage:

   DATE.Str

Returns the date in C<YYYY-MM-DD> format (L<ISO 8601|http://en.wikipedia.org/wiki/ISO_8601>)

=head2 method gist

Defined as:

    multi method gist(Date:D:) returns Str:D

Usage:

    DATE.gist

Returns the date in C<YYYY-MM-DD> format (L<ISO 8601|http://en.wikipedia.org/wiki/ISO_8601>)

=head1 Functions

=head2 sub sleep

    sub sleep($seconds = Inf --> Nil)

Attempt to sleep for the given number of C<$seconds>.  Returns C<Nil> on
completion.  Accepts C<Int>, C<Num>, C<Rat>, or C<Duration> types as an
argument since all of these also do C<Real>.

    sleep 5                # Int
    sleep 5.2              # Num
    sleep (5/2)            # Rat
    sleep (now - now + 5)  # Duration

It is thus possible to sleep for a non-integer amount of time.  For
instance, the following code shows that C<sleep (5/2)> sleeps for 2.5
seconds and C<sleep 5.2> sleeps for 5.2 seconds:

    my $before = now
    sleep (5/2)
    my $after = now
    say $after-$before  #=> 2.502411561

    my $before = now
    sleep 5.2
    my $after = now
    say $after-$before  #=> 5.20156987

=head2 sub sleep-timer

    sub sleep-timer(Real $seconds = Inf --> Duration)

This function is just like C<sleep>, but returns the amount of time
remaining to sleep as a C<Duration> (which will be 0 if the call was not
interrupted).

    say sleep-timer 3.14  #=> 0

=head2 sub sleep-until

    sub sleep-until(Instant $until --> Bool)

Works just like C<sleep> but checks the current time and goes back to sleep
if accidentally woken up early, to guarantee waiting until the specified
time.  Returns C<True> if the function actually waited, or if the specified
time happens to be the present moment.  Returns C<False> if you asked to
sleep until a time in the past.

To sleep until 10 seconds into the future, one could write something like this:

    say sleep-until now+10     #=> True

Trying to sleep until a time in the past doesn't work:

    my $instant = now - 5;
    say sleep-until $instant   # => False

However if we put the instant sufficiently far in the future, the sleep
should run:

    my $instant = now + 30
    # assuming the two commands are run within 30 seconds of one another...
    say sleep-until $instant   # => True

To specify an exact instant in the future, first create a C<DateTime> at the
appropriate point in time, and cast to an C<Instant>.

    my $instant = DateTime.new(
        year => 2020,
        month => 9,
        day => 1,
        hour => 22,
        minute => 5);
    say sleep-until $instant.Instant  # => True (eventually...)

This could be be used as a primitive kind of alarm clock.  For instance, say
you need to get up at 7am on the 4th of September 2015, but for some reason
your usual alarm clock is broken and you only have your laptop.  You can
specify the time to get up (being careful about timezones, since
C<DateTime.new> uses UTC by default) as an C<Instant> and pass this to
C<sleep-until>, after which you can play an mp3 file to wake you up instead
of your normal alarm clock.  This scenario looks roughly like this:

    # DateTime.new uses UTC by default, so get timezone from current time
    my $timezone = DateTime.now.timezone;
    my $instant = DateTime.new(
        year => 2015,
        month => 9,
        day => 4,
        hour => 7,
        minute => 0,
        timezone => $timezone
    ).Instant;
    sleep-until $instant;
    qqx{mplayer wake-me-up.mp3};

=end pod
