TGC Codebase Backup



Calculate Day of the Week by Mdj

26th Jan 2015 23:55
Summary

This programs accepts a date and returns the day of the week for that date.



Description

This programs accepts a date and returns the day of the week for that date.

Please let me know how this program could be improved. I originally had CASE statements for some of the conditions but changed to IF/THEN.

You'd think the process is simple right? Without using an anchor date? Show me some code that doesn't use a known anchor point and that is simpler than this method. In the code is a link showing where I got the information to implement Kraitchik's version of previous work by Gauss.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    MAXIMIZE WINDOW
SET TEXT FONT "Atari Classic Smooth"
SET TEXT SIZE 16
CLS RGB(1,78,156)
INK RGB(244,237,237)

//vars
dotw$ AS STRING
dotw3 AS STRING
adate$ AS STRING
adate$ = "09112001"
PRINT "adate$ = ";adate$

dotw$ = dayoftheweek$(adate$)
dotw3$=  Midstr(dotw$,1,3)
PRINT "Day of the week = ";dotw$; "  (";dotw3$;")"

WAIT KEY
END

FUNCTION dayoftheweek$(adate$ AS STRING)
     Rem Project: DOTW by Mark D. Jacobsen
     Rem Created: Monday, January 19, 2015
     Rem ***** Main Source File *****
     // input a date$
     // output  dotw$
     
     //vars
     temp as integer = 0
     m AS INTEGER = 0
     c AS INTEGER = 0
     y AS INTEGER = 0
     d AS INTEGER = 0
     zeromonth$ AS STRING
     zeroday$  AS STRING
     zeroyear$  AS STRING
     zeromonth AS INTEGER 
     zeroday AS INTEGER
     zeroyear AS INTEGER
     
     zeromonth$ = Midstr(adate$, 1,2)
     zeromonth = VAL(zeromonth$)
     zeroday$ = Midstr(adate$,3,2)
     zeroday =  VAL(zeroday$)
     zeroyear$ = Midstr(adate$,5,4)
     zeroyear  = VAL(zeroyear$)
     zerodd$=Midstr(adate$,7,2)
     zerodd = VAL(zerodd$)

//   Kraitchik's variation on Gauss
//   For the Gregorian calendar, take the century of the year (ex, the year 1986 would be 1900, 2014 would be 2000).
//   [Century/100] mod    4          0     1     2     3
//                        c          0     5     3     1
     temp = VAL(zeroyear$) / 100
     temp = temp mod 4

     IF temp = 0
          c = 0
     ENDIF
     IF temp = 1
          c = 5
     ENDIF
     IF temp = 2
          c = 3
     ENDIF
     IF temp = 3
          c = 1
     ENDIF
     
//   Month    1     2     3     4     5     6     7     8     9     10         11     12
//   m        1     4     3     6     1     4     6     2     5     0          3         5
     IF zeromonth = 1
          m = 1
     ENDIF
     IF zeromonth = 2
          m = 4
     ENDIF
     IF zeromonth = 3
          m = 3
     ENDIF
     IF zeromonth = 4
          m = 6
     ENDIF
     IF zeromonth = 5
          m = 1
     ENDIF
     IF zeromonth = 6
          m = 4
     ENDIF
     IF zeromonth = 7
          m = 6
     ENDIF
     IF zeromonth = 8
          m = 2
     ENDIF
     IF zeromonth = 9
          m = 5
     ENDIF
     IF zeromonth = 10
          m = 0
     ENDIF
     IF zeromonth = 11
          m = 3
     ENDIF
     IF zeromonth = 12
          m = 5
     ENDIF

remstart
Last two digits of the year                                                                                                                                y
00     06      __        17        23        28        34   __   45   51   56     62     __     73     79     84     90     __         0
01     07      12        18        __        29        35   40   46   __   57     63     68     74    __      85     91     96         1
02     __      13        19        24        30        __   41   47   52   58    __      69     75     80     86     __     97         2
03     08      14        __        25        31        36   42   __   53   59     64     70    __      81     87     92     98         3
__     09      15        20        26        __        37   43   48   54   __     65     71     76     82     __     93     99         4
04     10      __        21        27        32        38   __   49   55   60     66      __    77     83     88     94     __         5
05     11     16         22        __        33        39   44   50   __   61     67     72     78     __     89     95     __         6
* IF 0month = 1 or 2 
* y = y - 1
remend

      //   *subtract 1  from dates in January or February
      IF zeromonth = 1 OR zeromonth = 2
            zerodd = zerodd -1
      ENDIF
       
      IF zerodd=00 OR zerodd=06 OR zerodd=17 OR zerodd=23 OR zerodd=28 OR zerodd=34 OR zerodd=45 OR zerodd=51 OR zerodd=56 OR zerodd=62 OR zerodd=73 OR zerodd=79 OR zerodd=84 OR zerodd=90
            y=0
      ENDIF
      IF zerodd=01 OR zerodd=07 OR zerodd=12 OR zerodd=18 OR zerodd=29 OR zerodd=35 OR zerodd=40 OR zerodd=46 OR zerodd=57 OR zerodd=63 OR zerodd=68 OR zerodd=74  OR zerodd=85 OR zerodd=91 OR zerodd=96
            y =1
      ENDIF
      IF zerodd=02 OR zerodd=13 OR zerodd=19 OR zerodd=24 OR zerodd=30 OR zerodd=41 OR zerodd=47 OR zerodd=52 OR zerodd=58 OR zerodd=69 OR zerodd=75 OR zerodd=80 OR zerodd=86 OR zerodd=97
            y=2
      ENDIF
      IF zerodd=03 OR zerodd=08 OR zerodd=14 OR zerodd=25 OR zerodd=31 OR zerodd=36 OR zerodd=42 OR zerodd=53 OR zerodd=59 OR zerodd=64 OR zerodd=70 OR zerodd=81 OR zerodd=87 OR zerodd=92 OR zerodd=98
            y=3
      ENDIF
      IF zerodd=09 OR zerodd=15 OR zerodd=20 OR zerodd=26 OR zerodd=37 OR zerodd=43 OR zerodd=48 OR zerodd=54 OR zerodd=65 OR zerodd=71 OR zerodd=76 OR zerodd=82 OR zerodd=93 OR zerodd=99 
            y=4
      ENDIF
      IF zerodd=04 OR zerodd=10 OR zerodd=21 OR zerodd=27 OR zerodd=32 OR zerodd=38 OR zerodd=49 OR zerodd=55 OR zerodd=60 OR zerodd=66 OR zerodd=77 OR zerodd=83 OR zerodd=88 OR zerodd=94
            y=5
      ENDIF
      IF zerodd=05 OR zerodd=11 OR zerodd=16 OR zerodd=22 OR zerodd=33 OR zerodd=39 OR zerodd=44 OR zerodd=50 OR zerodd=61 OR zerodd=67 OR zerodd=72 OR zerodd=78 OR zerodd=89 OR zerodd=95
            y=6
      ENDIF

      dotw = (zeroday + m + c + y) 
      dotw = dotw mod 7
     
      //    1 = Sunday ... 7 = Saturday
      IF dotw = 1 THEN dotw$ = "Sunday"
      IF dotw = 2 THEN dotw$ = "Monday"
      IF dotw = 3 THEN dotw$ = "Tuesday"
      IF dotw = 4 THEN dotw$ = "Wednesday"
      IF dotw = 5 THEN dotw$ = "Thursday"
      IF dotw = 6 THEN dotw$ = "Friday"
      IF dotw = 7 THEN dotw$ = "Saturday"

      //   http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
      //   Kraitchik's variation on Gauss
     
ENDFUNCTION dotw$

function Midstr(s$,pos,length)
      rem --- an extended version of mid$() where you can set the length of return
      t$ = left$(right$(s$,len(s$)-(pos-1)),length)
endfunction t$