DateFunctions.borg

DateFunctions.borg


`---------------------------------------
` FILE:        DateFunctions.borg
` AUTHOR:        Pieter Verheyden
` E-MAIL:        pverheyd@vub.ac.be
` LAST CHANGED:    18/12/2001
`---------------------------------------

{

` ----------------------------
`    HELP FUNCTION
` ----------------------------

` like the is_void function
is_false(el)::
    el~false;


` ----------------------------
`    DATE FUNCTIONS
` ----------------------------

` datetable (of meeting): [day, month, year, [beginhour,beginminutes], [endhour,endminutes]]

` check wheter a given time1 is later than a given time2
` time1 and time2 are tables of the form: [hour, minutes]
laterTime(time1, time2):: {
    hour1:        time1[1];
    hour2:        time2[1];
    minutes1:    time1[2];
    minutes2:    time2[2];

    ((hour1>hour2) | ((hour1=hour2) & (minutes1>minutes2)))
};

` check wheter a given time1 and a given time2 are the same
` time1 and time2 are tables of the form: [hour, minutes]
sameTime(time1, time2):: {
    hour1:        time1[1];
    hour2:        time2[1];
    minutes1:    time1[2];
    minutes2:    time2[2];

    ((hour1=hour2) & (minutes1=minutes2))
};        

` check whether the begintime and endtime are valid
` begintime and endtime are tables of the form: [hour, minutes]
checkBegintimeEndtime(begintime, endtime):: {
    hour1:        begintime[1];
    hour2:        endtime[1];
    minutes1:    begintime[2];
    minutes2:    endtime[2];

    (
    ((hour1>=0) & (hour2>=0))
    &
    ((hour1<24) & (hour2<24))
    &
    ((minutes1>=0) & (minutes2>=0))
    &
    ((minutes1<60) & (minutes2<60))
    &
    (laterTime(endtime, begintime))
    )        
};

` check whether a year is a leap year with following rules:
` 1.een jaar is een schrikkeljaar als het jaartal deelbaar is door 4, behalve als regel 2 geldt.
` 2.een jaar is toch geen schrikkeljaar als het jaartal deelbaar is door 100, behalve als regel 3 geldt.
` 3.een jaar is toch wel een schrikkeljaar als het jaartal deelbaar is door 400.
checkLeapYear(year):: {
    if(((year/400)=(year//400)), true,
        if(((year/100)=(year//100)), false,
            if(((year/4)=(year//4)), true)
        )          
    )
};

` check whether the last day of February is valid
checkEndFebruary(day, year):: {
    if(day=28, true,
        if(day=29,
            {
            leapyear: checkLeapYear(year);
            if(is_false(leapyear), false, true)
            },
            if(day>29, false)
        )
    )
};

` check whether day/month/year is valid
checkDayMonthYear(day, month, year):: {
    thirtyonedaysmonth:[1,3,5,7,8,10,12];
    ` check whether the month is valid
    if(((month>=1) & (month<=12)),
        ` checken whether 29/2 falls in a leap year
        if(month=2, if(day>=28, checkEndFebruary(day, year), true),
            ` check whether the last day is in the given month
            if(((day>=1) & (day<31)), true,
                if(day=31, if(is_false(elementInTable(thirtyonedaysmonth, month)), false, true), false)
            )
        ),
        false
    )
};

` check whether a datetable (date + time) is valid
checkDate(datetable):: {
    if(size(datetable)=5,
        {
        day:        datetable[1];
        month:        datetable[2];
        year:        datetable[3];
        begintime:    datetable[4];
        endtime:    datetable[5];

        if(checkDayMonthYear(day, month, year), 
            if(checkBegintimeEndtime(begintime, endtime), 
                if(oldDate(datetable), false, true),
                false
            ),
            false
        )
        },
        false
    )        
};

` check whether two datetables overlap
overlappingDates(datetable1, datetable2):: {
    if(((datetable1[1]=datetable2[1]) & (datetable1[2]=datetable2[2]) & (datetable1[3]=datetable2[3])),
        {
            begintime1:    datetable1[4];
            begintime2:    datetable2[4];
            endtime1:    datetable1[5];    
            endtime2:    datetable2[5];

            if(((sameTime(begintime1,begintime2)) | (sameTime(endtime1,endtime2))), true,
                if(((sameTime(endtime1,begintime2)) | (sameTime(begintime1,endtime2))), false,
                    if((((laterTime(begintime2,begintime1)) 
                        & ((sameTime(begintime2,endtime1)) | (laterTime(endtime1,begintime2)))) 
                        | ((laterTime(begintime1,begintime2)) & ((sameTime(begintime1,endtime2)) | (laterTime(endtime2,begintime1))))),
                    true,
                    false
                    )
                )
            )                
        },
        false
    )
};

` check whether 2 datetables are identique
sameDates(datetable1, datetable2):: {
    begintimetable1:    datetable1[4];
    begintimetable2:    datetable2[4];
    endtimetable1:    datetable1[5];
    endtimetable2:    datetable2[5];
    
    (
    (datetable1[1]=datetable2[1])
    &
    (datetable1[2]=datetable2[2])
    &
    (datetable1[3]=datetable2[3])
    &
    (begintimetable1[1]=begintimetable2[1])
    &
    (begintimetable1[2]=begintimetable2[2])
    &
    (endtimetable1[1]=endtimetable2[1])
    &
    (endtimetable1[2]=endtimetable2[2])
    )
};

` get the current time (Europe/Brussels time)
` returns [seconds, minutes, hour]
getTime():: {
    clocktime: time();
    ` set GMT to Europe/Brussels time
    if(clocktime[3]=23, clocktime[3]:=0, clocktime[3]:=(clocktime[3]+1));
    clocktime
};

` check whether a date is over or is happening at the moment
oldDate(datetable):: {
    today:date();
    clocktime:getTime();
    now: [clocktime[3],clocktime[2]];    

    (
      (datetable[3]3])
    |
    ((datetable[3]=today[3]) & (datetable[2]2]))
    |
    ((datetable[3]=today[3]) & (datetable[2]=today[2]) & (datetable[1]1]))
    |
    ((datetable[3]=today[3]) & (datetable[2]=today[2]) & (datetable[1]=today[1]) 
        & ((laterTime(now,datetable[4])) | ((laterTime(now,datetable[5])))))
    )

};

` convert minutes to string
` prints 0 (or 00) as 00 
minutesToString(minutes):: {
    stringminutes:"";
    if(minutes=0, 
        stringminutes:=(text(0)+text(0)),
        stringminutes:=text(minutes)
    );
    stringminutes
};

` convert a datetable to a string
dateToString(datetable):: {
    day:        text(datetable[1]);
    month:        text(datetable[2]);
    year:        text(datetable[3]);
    begintimetable:    datetable[4];
    endtimetable:    datetable[5];

    datestring:(day + "/" + month + "/" + year);
    datestring:=datestring + (" from " + text(begintimetable[1]) + "." + minutesToString(begintimetable[2]));
    datestring:=datestring + (" till " + text(endtimetable[1]) + "." + minutesToString(endtimetable[2]));
    datestring
}

}