[comp.lang.fortran] Date functionality

fpb@ittc.wec.com (Frank P. Bresz) (07/28/90)

Hello,
	I am looking for an algorithm to give me the day of the week when
handed the current date.  I can't use any builtin routines from any OS
(this is targeted for a real brain-damaged machine).  Any Help appreciated.

					Frank P. Bresz }*{
					ITTC Network Administrator
+--------------------+
|fbresz@ittc.wec.com |
|uunet!ittc!fbresz   |
|(412)733-6749       |
|Fax: (412)733-6444  |
+--------------------+

alan@dmsmelb.dms.oz (Alan Miller) (08/01/90)

In article <171@ittc.wec.com> fbresz@ittc.wec.com writes:
>Hello,
>	I am looking for an algorithm to give me the day of the week when
>handed the current date.  I can't use any builtin routines from any OS
>(this is targeted for a real brain-damaged machine).  Any Help appreciated.
>
>					Frank P. Bresz }*{
>					ITTC Network Administrator
>+--------------------+
>|fbresz@ittc.wec.com |
>|uunet!ittc!fbresz   |
>|(412)733-6749       |
>|Fax: (412)733-6444  |
>+--------------------+


The following Fortran function takes the date and gives day of the week:
(N.B. for some old Fortran compilers you will need to remove the underline
characters and shorten variable names to 6 characters.)

      character *9 function day_of_week(year, month, day)
      integer year, month, day, yr, mnth, hundreds, day_ptr
c
c       Number the months starting from March; January & February are
c       treated as months 11 & 12 of the previous year.
c
      character day_name(0:6)*9
      data day_name / 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 
     +  'Thursday', 'Friday', 'Saturday' /
      mnth = month - 2
      if (mnth .le. 0) then
        mnth = mnth + 12
        yr = year - 1
      else
        yr = year
        end if
      hundreds = yr / 100
c
c       The days are numbered from Sunday (0) to Saturday (6).
c       The function mod(n,7) returns the remainder after n is divided
c       by 7.
c
      yr = yr - (100 * hundreds)
      day_ptr = mod(((((day + (((26 * mnth) - 2) / 10)) + (5 * hundreds)
     +  ) + yr) + (yr / 4)) + (hundreds / 4),7)
      day_of_week = day_name(day_ptr)
      return 
      end