Rand Stats

Date::Event

zef:tbrowder

Actions Status Actions Status Actions Status

NAME

Date::Event - Provides a class suitable for use with calendars or any program using the Raku Date type

SYNOPSIS

use Date::Event;

my $date = Date.new: :year(2024), :month(12), :day(31);

# Enter an Etype with a known enum EType name:
my $e  = Date::Event.new: :$date, :Etype<Birthday>;

# Enter an Etype with a known enum EType number
my $e2 = Date::Event.new: :$date, :name<Easter>,:Etype(12); # Liturgy

# Smart match on the enum attribute
if $e2.Etype ~~ Liturgy {
    say $e2.name; # OUTPUT: «Easter␤»
}

DESCRIPTION

Date::Event is a class that provides basic attributes to describe an event occurring on a particular Date. It is suitable for multiple instances on a Date and is currently defined as follows:

unit class Date::Event;

enum EType is export (
    Unknown     => 0,
    Birth       => 1,
    Christening => 2,
    Baptism     => 3,
    BarMitzvah  => 4,
    BatMitzvah  => 5,
    Graduation  => 6,
    Wedding     => 7,
    Anniversary => 8,
    Retirement  => 9,
    Death       => 10,
    Birthday    => 11,
    Liturgy     => 12,

    Holiday     => 100,
    Astro       => 150,
    Other       => 200,
);

has Str   $.set-id                  = "";
has Str   $.id                      = "";
has Str   $.name;                   = "";
has Str   $.short-name              = "";
has       $.Etype                   = 0;
has Date  $.date;
has Date  $.date-observed;
has Str   $.notes                   = "";
has Bool  $.is-calculated           = False;

# For use with Date::Utils:
has UInt  $.nth-value;
has UInt  $.nth-dow;
has UInt  $.nth-month-number;

# Attributes for Astro events:
has Numeric  $.lat where {  -90 <= $_ <=  90 };
has Numeric  $.lon where { -180 <= $_ <= 180 };
has DateTime $.time;

submethod TWEAK {
    $!Etype = self.etype($!Etype)
}

multi method etype(Str $v? --> UInt) {
    my %m = EType.enums;
    if $v.defined {
        return %m{$v}
    }
    else {
        return %m{self.Etype}
    }
}

multi method etype(UInt $v? --> EType) {
    if $v.defined {
        return EType($v)
    }
    else {
        return EType(self.Etype)
    }
}

method lat(Numeric $v?) {
    if $v.defined {
        $!lat = $v
    }
    else {
        return $!lat
    }
}

method lon(Numeric $v?) {
    if $v.defined {
        $!lon = $v
    }
    else {
        return $!lon
    }
}

# A default event is normally set on a certain date.  Many holidays
# are an exception in that they are calculated based on one or more
# date criteria or conversion from another calendar (e.g., from Jewish
# to Gregorian).  }

#= Enable the user to change the attribute
#= during development
method is-calculated(Bool $v?) {
    if $v.defined {
        $!is-calculated = $v
    }
    else {
        return $!is-calculated
    }
}

Published modules showing similar use are 'Holidays::US:Federal' and 'Holidays::Miscellaneous'. They include special databases that use the 'Date::Event' class to provide a common interface (API) for working with dates. Those modules are being integrated into the next version of published module 'Calendar'.

AUTHOR

Tom Browder tbrowder@acm.org

COPYRIGHT AND LICENSE

© 2023-2024 Tom Browder

This library is free software; you may redistribute it or modify it under the Artistic License 2.0.