pike.git / lib / modules / Calendar.pmod / Event.pmod

version» Context lines:

pike.git/lib/modules/Calendar.pmod/Event.pmod:10:   constant M_NAME="---JanFebMarAprMayJunJulAugSepOctNovDec"/3;   constant WD_NAME="---MonTueWedThuFriSatSun"/3;      function(mixed...:TimeRange) std_day=master()->resolv("Calendar")["Day"];   function(mixed...:TimeRange) std_second=master()->resolv("Calendar")["Second"];      // ----------------------------------------------------------------   // base classes   // ----------------------------------------------------------------    + //! module Calendar + //! submodule Event + //! subclass Event + //! <ref>Event</ref> is a base class, defining what + //! methods an Event need to have.    -  + //! method array(TimeRange) scan(TimeRange in) + //! This calculates the eventual events that + //! is contained or overlapped by the given timerange. + //! + //! Example: + //! <tt>Event.christmas_eve->scan(Year(2000))</tt> + //! => <tt>({ Day(Sun 24 Dec 2000) })</tt> + //! + //! <ref>scan</ref> uses <ref>next</ref> if not overloaded. + //! + //! note: + //! <ref>scan</ref> can return an array of overlapping timeranges. + //! + //! This method must use <tt>in->calendar_object-></tt><i>type</i> + //! to create the returned timeranges, and must keep the ruleset.    - //! @[Event] is a base class, defining the - //! methods an Event need to have. + //! method TimeRange next(TimeRange from,void|int(0..1) including) + //! method TimeRange previous(TimeRange from,void|int(0..1) including) + //! This calculates the next or previous occurance of the event, + //! from the given timerange's <b>start</b>, + //! including any event occuring at the start if that flag is set. + //! + //! It returns zero if there is no next event. + //! + //! This method is virtual in the base class. +    class Event   {    string name;    string id="?";       constant is_event=1;    -  //! These functions calculate the next or previous occurance of the event, -  //! from the given timerange's @b{start@}, -  //! including any event occuring at the start if the -  //! @[including] flag is set. -  //! -  //! @returns -  //! Zero is returned if there is no next event. -  //! -  //! @note -  //! These methods are virtual in the base class. +     TimeRange next(void|TimeRange from,void|int(0..1) including);    TimeRange previous(void|TimeRange from,void|int(0..1) including);      // what events in this period? -  //! This calculates the eventual events that -  //! are contained or overlapped by the given timerange. -  //! -  //! @example -  //! @code{Event.christmas_eve->scan(Year(2000))@} -  //! => @code{({ Day(Sun 24 Dec 2000) })@}. -  //! -  //! @[scan()] uses @[next()] if not overloaded. -  //! -  //! @note -  //! @[scan()] can return an array of overlapping timeranges. -  //! -  //! This method must use @tt{in->calendar_object->@i{type@}@} -  //! to create the returned timeranges, and must keep the ruleset. +     array(TimeRange) scan(TimeRange in)    {    array res=({});    TimeRange t=next(in,1);    for (;;)    {    if (!t || !t->overlaps(in)) return res;    res+=({t});    t=next(t);    }
pike.git/lib/modules/Calendar.pmod/Event.pmod:179:    else jd=(floatp(from->julian_day())    ?(int)floor(from->julian_day())    :(from->julian_day()-1));    jd=scan_jd(from->calendar(),jd+nd-1,-1);    if (jd==NODAY) return 0;    return (from->calendar()->Day)("julian_r",jd,from->ruleset())*nd;    }   }       - //! This is created by the @[Namedays] classes + //! module Calendar + //! submodule Event + //! subclass Nameday + //! This is created by the <ref>Namedays</ref> classes   //! to represent an event for a name. -  +    class Nameday   {    inherit Day_Event;       constant is_nameday=1;       int jd;       void create(string _name,int _jd)    {
pike.git/lib/modules/Calendar.pmod/Event.pmod:207:    if (direction==1) return sjd<=jd?jd:NODAY;    else return sjd>=jd?jd:NODAY;    }       string _sprintf(int t)    {    return t=='O'?sprintf("Nameday(%s:%O)",id,name):0;    }   }    + //! module Calendar + //! submodule Event + //! subclass Namedays   //! This contains a ruleset about namedays.   //! inherits Event -  +    class Namedays   {    inherit Event;    constant is_namedays=1;       int leapdayshift;    int first_year=-1;    int last_year=-1;       array namelist;
pike.git/lib/modules/Calendar.pmod/Event.pmod:235:    {    id=_id;    name=_name;    namelist=_names;    first_year=start||-1;    last_year=stop||-1;    leapdayshift=_leapdayshift||2000;    lookup=_lookup;    }    + //! method array(string) names(TimeRange t)   //! Gives back an array of names that occur during   //! the time period, in no particular order. -  +     array(string) names(TimeRange t)    {   // optimize this?    return predef::`|(({}),@values(namedays(t)));    }    -  + //! method mapping(TimeRange:array(string)) namedays(TimeRange t)   //! Gives back an table of days with names that occur during   //! the time period. Note that days without names will not   //! appear in the returned mapping. -  +     mapping(TimeRange:array(string)) namedays(TimeRange t)    {    int jd=t->julian_day();    mapping res=([]);    function(mixed...:TimeRange) day=t->calendar()->Day;    Ruleset rules=t->ruleset();    [int y,int yjd,int leap]=gregorian_yjd(jd);    if (first_year!=-1 && y<first_year)    [y,yjd,leap]=gregorian_year(first_year),jd=yjd;    if (last_year!=-1 && y>last_year) return res;
pike.git/lib/modules/Calendar.pmod/Event.pmod:302:    foreach ((array)namedays(in),[TimeRange t,array(string) s])    res[t]=predef::`|(@map(s,Nameday,t->julian_day()));    return res;    }       array(TimeRange) scan(TimeRange in)    {    return indices(namedays(in));    }    -  //! @decl TimeRange previous(TimeRange from,void|int(0..1) including) -  //! @decl TimeRange next(TimeRange from,void|int(0..1) including) + //! method TimeRange previous(TimeRange from,void|int(0..1) including) + //! method TimeRange next(TimeRange from,void|int(0..1) including)       static TimeRange _find(TimeRange t,int including,int direction)    {    int jd=(int)t->julian_day();       jd+=direction*!including;       [int y,int yjd,int leap]=gregorian_yjd(jd);    int ld;    if (y<leapdayshift) ld=55-1; // 24 feb
pike.git/lib/modules/Calendar.pmod/Event.pmod:619:    }    }    }       string describe()    {    return sprintf("%s %2d julian",M_NAME[mn],md);    }   }    + //! module Calendar + //! submodule Event + //! subclass Date   //! This class represents the event of a given gregorian date.   //! For instance,   //! <tt>Event.Date(12,10)->next(Day())</tt>   //! finds the next 12 of October.   //! -  + //! method void create(int month_day,int month) + //! The event is created by a given month day and + //! a month number (1=January, 12=December). +    class Date   {    inherit Day_Event;       int md,mn;       int yd;    -  //! @decl void create(int month_day, int month) -  //! The event is created by a given month day and -  //! a month number (1=January, 12=December). +     void create(int _md,int _mn)    {    md=_md;    mn=_mn;    name=M_NAME[mn]+" "+md;       yd=M_YD[mn]+md;    }       int scan_jd(Calendar realm,int jd,int(-1..1) direction)
pike.git/lib/modules/Calendar.pmod/Event.pmod:669:    if (yd>M_ED[2][mn]) return NODAY; // will never happen       if (direction==1)    [y,yjd,leap]=gregorian_yjd(yjd+365+leap);    else    [y,yjd,leap]=gregorian_yjd(yjd-1);    }    }   }    + //! module Calendar + //! submodule Event + //! subclass Date_Weekday   //! This class represents the event that a given gregorian date appears   //! a given weekday.   //! For instance,   //! <tt>Event.Date_Weekday(12,10,5)->next(Day())</tt>   //! finds the next 12 of October that is a friday.   //! -  + //! method void create(int month_day,int month,int weekday) + //! The event is created by a given month day, + //! a month number (1=January, 12=December), and a + //! weekday number (1=Monday, 7=Sunday). + //! + //! note: + //! The week day numbers used + //! are the same as the day of week in the <ref>ISO</ref> calendar - + //! the <ref>Gregorian</ref> calendar has 1=Sunday, 7=Saturday. +    class Date_Weekday   {    inherit Day_Event;       int md,mn;       int yd;    int jd_wd;    -  //! @decl void create(int month_day,int month,int weekday) -  //! The event is created by a given month day, -  //! a month number (1=January, 12=December), and a -  //! weekday number (1=Monday, 7=Sunday). -  //! -  //! @note -  //! The week day numbers used -  //! are the same as the day of week in the @[ISO] calendar - -  //! the @[Gregorian] calendar has 1=Sunday, 7=Saturday. +     void create(int _md,int _mn,int wd)    {    md=_md;    mn=_mn;    name=M_NAME[mn]+" "+md+" "+WD_NAME[wd];       yd=M_YD[mn]+md;    jd_wd=(wd+6)%7;    }   
pike.git/lib/modules/Calendar.pmod/Event.pmod:730:    if (yd>M_ED[2][mn]) return NODAY; // will never happen       if (direction==1)    [y,yjd,leap]=gregorian_yjd(yjd+365+leap);    else    [y,yjd,leap]=gregorian_yjd(yjd-1);    }    }   }    + //! module Calendar + //! submodule Event + //! subclass Monthday_Weekday   //! This class represents the event that a given gregorian   //! day of month appears a given weekday.   //! For instance, - //! @code{Event.Monthday_Weekday(13,5)->next(Day())@} + //! <tt>Event.Monthday_Weekday(13,5)->next(Day())</tt>   //! finds the next friday the 13th.   //! -  + //! method void create(int month_day,int weekday) + //! The event is created by a given month day, + //! and a weekday number (1=Monday, 7=Sunday). + //! + //! note: + //! The week day numbers used + //! are the same as the day of week in the <ref>ISO</ref> calendar - + //! the <ref>Gregorian</ref> calendar has 1=Sunday, 7=Saturday. +    class Monthday_Weekday   {    inherit Day_Event;       int md;    int jd_wd;    -  //! @decl void create(int month_day, int weekday) -  //! The event is created by a given month day, -  //! and a weekday number (1=Monday, 7=Sunday). -  //! -  //! @note -  //! The week day numbers used -  //! are the same as the day of week in the @[ISO] calendar - -  //! the @[Gregorian] calendar has 1=Sunday, 7=Saturday. +     void create(int _md,int wd)    {    md=_md;    name=md+","+WD_NAME[wd];    jd_wd=(wd+6)%7;    }       int scan_jd(Calendar realm,int jd,int(-1..1) direction)    {    if (md>31 || md<1) return 0;
pike.git/lib/modules/Calendar.pmod/Event.pmod:792:    {    foreach (enumerate(12),int i)    if (w[i]==jd_wd && z[i]<=jd &&    z[i]-yjd<M_ED[leap][i+1]) return z[i];    [y,yjd,leap]=gregorian_yjd(yjd-1);    }    }    }   }    + //! module Calendar + //! submodule Event + //! subclass Weekday   //! This class represents any given weekday.   //! For instance,   //! <tt>Event.Weekday(5)->next(Day())</tt>   //! finds the next friday.   //!   //! These are also available as the pre-defined events   //! "monday", "tuesday", "wednesday", "thursday",   //! "friday", "saturday" and "sunday".   //! -  + //! method void create(int weekday) + //! The event is created by a given + //! weekday number (1=Monday, 7=Sunday). + //! + //! note: + //! The week day numbers used + //! are the same as the day of week in the <ref>ISO</ref> calendar - + //! not the <ref>Gregorian</ref> or <ref>Julian</ref> + //! calendar that has 1=Sunday, 7=Saturday. +    class Weekday   {    inherit Day_Event;    constant is_weekday=1;       int jd_wd;    -  //! @decl void create(int weekday) -  //! The event is created by a given -  //! weekday number (1=Monday, 7=Sunday). -  //! -  //! @note -  //! The week day numbers used -  //! are the same as the day of week in the @[ISO] calendar - -  //! not the @[Gregorian] or @[Julian] -  //! calendar that have 1=Sunday, 7=Saturday. +     void create(int wd,void|string _id)    {    jd_wd=(wd+6)%7; // convert to julian day numbering    name=WD_NAME[wd];    if (!id) id=name; else id=_id;    }       int scan_jd(Calendar realm,int jd,int(-1..1) direction)    {    if (direction==-1) return jd-(jd-jd_wd)%7;
pike.git/lib/modules/Calendar.pmod/Event.pmod:1046:    }       string describe()    {    return sprintf("%s %2d %s %+2d %s [<=%+d]",    M_NAME[mn],md,WD_NAME[wd],n,    inclusive?"incl":"",offset);    }   }    + //! module Calendar + //! submodule Event + //! subclass SuperEvent   //! This class holds any number of events,   //! and adds the functionality of event flags.   //! - //! @note + //! note:   //! Scanning (scan_events,next,etc) will drop flag information. - //! Dig out what you need with @tt{->@[holidays]@} et al first. + //! Dig out what you need with -><ref>holidays</ref> et al first. +    class SuperEvent   {    inherit Event;       constant is_superevent=1;    string name="SuperEvent";       mapping(Event:multiset(string)) flags=([]);       array(Event) events=({});
pike.git/lib/modules/Calendar.pmod/Event.pmod:1097:    m_delete(flags,e);    }    else events|=({e});       foreach (events,Event e)    if (e->is_day_event) day_events+=({e});    else if (e->is_namedays) namedays+=({e});    else other_events+=({e});    }    -  //! @decl SuperEvent filter_flag(string flag) -  //! @decl SuperEvent holidays() -  //! @decl SuperEvent flagdays() + //! method SuperEvent filter_flag(string flag) + //! method SuperEvent holidays() + //! method SuperEvent flagdays()    //! Filter out the events that has a certain flag set.    //! Holidays (flag "h") are the days that are marked    //! red in the calendar (non-working days),    //! Flagdays (flag "f") are the days that the flag    //! should be visible in (only some countries).       SuperEvent filter_flag(string flag)    {    array res=({});    multiset m;