[net.sources] Day/Date Routines

ries@trwrb.UUCP (Marc Ries) (09/04/86)

[LE]
Dear Netters,

Due to recent requests for Date and Julian Date routines, I  went
back thru my archives and dug up these three items.  Please note,
I did *not* write these (except for the  Makefile).  Jdate.c  and
jday.c  were:   Author:   Robert  G.   Tantzen,  translator:  Nat
Howard.  I don't know who did day.c (will the real author  please
stand up).

# Cut_Here-----Cut_Here-----Cut_Here---------Cut_Here
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	Makefile
#	day.c
#	jday.c
#	jdate.c
export PATH; PATH=/bin:/usr/bin:$PATH
echo shar: "extracting 'Makefile'" '(498 characters)'
if test -f 'Makefile'
then
	echo shar: "will not over-write existing file 'Makefile'"
else
cat << \RiesES_PIECES > 'Makefile'
#
# Makefile: day
#  Created: Thursday, September 04, 1986
#       By: Marc Ries
#
CC     = /bin/cc
OPTS   = -O 
DEFS   = 
CFLAGS = $(OPTS) $(DEFS)
SRCS   = day.c jday.c jdate.c
OBJS   = jday.o jdate.o
LIBS   = 
RANLIB = ucb ranlib libjdates.a

all:	day libjdates

day:	day.o  
	$(CC) $(CFLAGS) -o day day.c $(LIBS)

libjdates:	jday.o jdate.o  
	ar r libjdates.a $(OBJS)
	$(RANLIB)	

clean:
	rm -f $(OBJS) a.out core

shar:
	shar -v day.[1clyh] Makefile > day.shr

edit:
	vi $(SRCS)
 
day.o:	day.c
RiesES_PIECES
fi
echo shar: "extracting 'day.c'" '(2674 characters)'
if test -f 'day.c'
then
	echo shar: "will not over-write existing file 'day.c'"
else
cat << \RiesES_PIECES > 'day.c'

/* ---------------------------------------------------------------------- */
/*        Program to calculate # of days between two dates                */
/* ---------------------------------------------------------------------- */

/* Please note that this program only works from 01-12-1600 A.D. onward   */

#include <stdio.h>

struct date                                 /*   structure to hold date   */
  {
   int     month;
   int    day;
   int    year;
  } date_1;


long int funct1 (y,m)                       /*   part of # of days calc.  */
    int y, m;
    {
     long int result;
     if ( m <= 2 )
       y -= 1;
     result = y;
     return (result);
    }

long int funct2 (m)
    int m;
    {
     long int result;
     if ( m <= 2 )
       result = m + 13;
     else
       result = m + 1;
       return(result);
    }

/* Function to calculate the number of days in dates */

long int day_count (m, d, y)
    int m, d, y;
    {
     long int number;
     number = 1461 *  funct1(y,m) / 4 + 153 * funct2(m) / 5 + d;

     return (number);
    }

main ()
{
    long int number_of_days1;
    int day_of_week, screw_up = 0;

    printf("\n\n*****************************************************************\n");
    printf("THIS PROGRAM WILL COMPUTE THE DAY OF THE WEEK (SUNDAY - SATURDAY)\n");
    printf("\t\tTHAT A GIVEN DATE WILL FALL ON\n");
    printf("*****************************************************************\n\n");

    printf ("Enter a date (mm dd yyyy) i.e. 03 12 1985  \n");
    scanf ("%d %d %d", &date_1.month, &date_1.day, &date_1.year);

    number_of_days1 = day_count (date_1.month, date_1.day, date_1.year);

    printf ("\nThe date is:  " );

    day_of_week = (number_of_days1 - 621049) % 7;

    switch (day_of_week)
      {
        case 0 :
            printf ("Sunday,");
            break;
        case 1 :
            printf ("Monday,");
            break;
        case 2 :
            printf ("Tuesday,");
            break;
        case 3 :
            printf ("Wednesay,");
            break;
        case 4 :
            printf ("Thursday,");
            break;
        case 5 :
            printf ("Friday,");
            break;
        case 6 :
            printf ("Saturday,");
            break;
        default:
            printf ("Something is screwed up -- Maybee you entered\n");
            printf ("a date earlier than 01 12 1600\n\n");
            screw_up = 1;
      }
         printf (" %02d/%02d/%02d\n", date_1.month, date_1.day, date_1.year);

}



RiesES_PIECES
fi
echo shar: "extracting 'jday.c'" '(818 characters)'
if test -f 'jday.c'
then
	echo shar: "will not over-write existing file 'jday.c'"
else
cat << \RiesES_PIECES > 'jday.c'
/*
  Here are two routines, jday.c and jdate.c.
  They are translations from ALGOL in Collected Algorithms of CACM.
*/

/*
** Takes a date, and returns a Julian day. A Julian day is the number of
** days since some base date  (in the very distant past).
** Handy for getting date of x number of days after a given Julian date
** (use jdate to get that from the Gregorian date).
** Author: Robert G. Tantzen, translator: Nat Howard
** Translated from the algol original in Collected Algorithms of CACM
** (This and jdate are algorithm 199).
*/
long
jday(mon, day, year)
int mon, day, year;
{
	long m = mon, d = day, y = year;
	long c, ya, j;

	if(m > 2) m -= 3;
	else {
		m += 9;
		--y;
	}
	c = y/100L;
	ya = y - (100L * c);
	j = (146097L * c) /4L + (1461L * ya) / 4L + (153L * m + 2L)/5L + d + 1721119L;
	return(j);
}
RiesES_PIECES
fi
echo shar: "extracting 'jdate.c'" '(906 characters)'
if test -f 'jdate.c'
then
	echo shar: "will not over-write existing file 'jdate.c'"
else
cat << \RiesES_PIECES > 'jdate.c'
/* Julian date converter. Takes a julian date (the number of days since
** some distant epoch or other), and returns an int pointer to static space.
** ip[0] = month;
** ip[1] = day of month;
** ip[2] = year (actual year, like 1977, not 77 unless it was  77 a.d.);
** ip[3] = day of week (0->Sunday to 6->Saturday)
** These are Gregorian.
** Copied from Algorithm 199 in Collected algorithms of the CACM
** Author: Robert G. Tantzen, Translator: Nat Howard
*/
int *
jdate(j)
long j;
{
	static int ret[4];

	long d, m, y;

	ret[3] = (j + 1L)%7L;
	j -= 1721119L;
	y = (4L * j - 1L)/146097L;
	j = 4L * j - 1L - 146097L * y;
	d = j/4L;
	j = (4L * d + 3L)/1461L;
	d = 4L * d + 3L - 1461L * j;
	d = (d + 4L)/4L;
	m = (5L * d - 3L)/153L;
	d = 5L * d - 3 - 153L * m;
	d = (d + 5L) / 5L;
	y = 100L * y + j;
	if(m < 10) 
		m += 3;
	else {
		m -= 9;
		++y;
	}
	ret[0] =  m;
	ret[1] = d;
	ret[2] = y;
	return(ret);
}

RiesES_PIECES
fi
exit 0
#	End of shell archive