' WEEKDAY$(3.0)  Form Day of a Week Abbreviated Name       09/30/1992-02/03/2010
' ------------------------------------------------------------------------------
' Copyright (C) 1992-2010 by Vladimir Veytsel                      www.davar.net

' Type -------------------------------------------------------------------------

'    Function

' Description ------------------------------------------------------------------

'    WEEKDAY$ function returns the name of the day of a week (abbreviated
'    to three characters) for the specified date.

' Parameter --------------------------------------------------------------------

'    Spec_Date$  - Specified date in the form of MM-DD-YY or MM-DD-CCYY
'                  (should be a valid date in Gregorian calendar)

' Value ------------------------------------------------------------------------

'    Three character day of the week identification for the specified date.

' Notes ------------------------------------------------------------------------

'  - Delimiters of parameter date fields are irrelevant and can be any
'    symbols.  Date fields are extracted from fixed positions.

'  - IF century part is not specified (total year length is 2 characters),
'       THEN CURRENT century is assumed (two digit of century get appended
'            before YY).

'  - If specified date is EMPTY,
'       then day of a week identification is returned for the CURRENT date.

'  - IF specified date doesn't represent a proper Gregorian calendar date,
'       THEN "***" is returned instead of day of a week identification
'            as an eye-catcher for an error in date specification.

'  - Day of a week is determined by the formula that was derived from the
'    algorithm suggested by Christian Zeller in 1883 (Zeller's Congruence).

'    D  - Day   within specified month
'    M  - Month within specified year
'    Y  - Year (full 4-digit including century)

'    If (M<3)
'       M=M+12
'       Y=Y-1

'    WDI=(D+INT((13*M-27)/5)+Y+INT(Y/4)-INT(Y/100)+INT(Y/400)) MOD 7

'    WDI - Week Day Index for the given date (0-Sun, 1-Mon, ..., 6-Sat)

'  - Formula works for dates after 09/14/1752 - when England and its
'    colonies switched from Julian calendar to currently used Gregorian.

' Examples ---------------------------------------------------------------------

'    WEEKDAY$(""          )="Fri"  - Today 03-08-2002
'    WEEKDAY$("*1-01-1996")="***"
'    WEEKDAY$("12-31-1996")="Tue"
'    WEEKDAY$("01-01-1997")="Wed"
'    WEEKDAY$("12-31-1997")="Wed"
'    WEEKDAY$("01-01-1998")="Thu"
'    WEEKDAY$("12-31-1998")="Thu"
'    WEEKDAY$("01-01-1999")="Fri"
'    WEEKDAY$("12-31-1999")="Fri"
'    WEEKDAY$("01-01-2000")="Sat"
'    WEEKDAY$("12-31-2000")="Sun"
'    WEEKDAY$("01-01-2001")="Mon"
'    WEEKDAY$("12-31-2001")="Mon"
'    WEEKDAY$("01-01-2002")="Tue"
'    WEEKDAY$("12-31-2002")="Tue"
'    WEEKDAY$("01-01-2003")="Wed"
'    WEEKDAY$("12-31-2003")="Wed"
'    WEEKDAY$("01-01-2004")="Thu"
'    WEEKDAY$("12-31-04"  )="Fri"

' External Function ------------------------------------------------------------

     #INCLUDE ONCE "VALDGRE"

' Start Function ---------------------------------------------------------------

     DEFINT A-Z  ' All defaulted variables are integer

     FUNCTION WEEKDAY$(Spec_Date$)

' Check Date Validity and Handle Default Empty Date (Current) ------------------

     IF (LEN(Spec_Date$)=0) THEN
        DT$=DATE$
     ELSEIF (VALDGRE%(Spec_Date$)) THEN
        DT$=Spec_Date$
     ELSE
        WEEKDAY$="***"
        EXIT FUNCTION
     END IF

' Form Date Working Variables --------------------------------------------------

     Month=VAL(LEFT$(DT$,2))
     Day  =VAL( MID$(DT$,4,2))

     IF (LEN(DT$)=8) THEN
        Year=VAL(MID$(DATE$,7,2)+RIGHT$(DT$,2))  ' Use current century
     ELSE
        Year=VAL(RIGHT$(DT$,4))
     END IF

' Form and Return Day of a Week Value to the Point of Invocation ---------------

     IF (Month<3) THEN
        Month=Month+12
        Year=Year-1
     END IF

     Day_Index=(Day+INT((13*Month-27)/5)+Year+INT(Year/4)-INT(Year/100)+ _
                INT(Year/400)) MOD 7

     WEEKDAY$=MID$("SunMonTueWedThuFriSat",3*Day_Index+1,3)

' Finish Function --------------------------------------------------------------

     END FUNCTION