Julian date routines needed
Neil Woods
ex1neil at cybaswan.UUCP
Tue Feb 19 23:56:07 AEST 1991
In article <1753 at manta.NOSC.MIL> grantk at manta.NOSC.MIL (Kelly J. Grant) writes:
>Howdy networld
>
>About 100,000 years ago I had some Julian date routines (in dBASE) to
>convert YY/MM/DD into a format suitable for math, and also to convert
>back to normal dates. We called these JULTOCAL and CALTOJUL. I now
>have a need for these algorithms for a UNIX program. Would any of
>you kind souls have these routines lying around in your book of tricks
>[right next to your hanoi.c file maybe :-)]
The following should do the trick:
------------(Cut Here)------------
#include <stdio.h>
/* returns Julian day number --given a string of the form xx/yy/zz.
** To calculate the day of the week use the return value % 7.
*/
long
julian_day (date_str)
char *date_str;
{
long jdn;
int month, day, year;
sscanf (date_str, "%d/%d/%d", &month, &day, &year);
if (year < 100)
year += 1900; /* assume 20th century */
if (year < 1000)
year += 2000; /* assume 21st */
jdn = (long) year * 367 + month * 275 / 9
- (year + (month > 2)) * 7 / 4
- ((year - (month < 3)) / 100 + 1) * 3 / 4 + day + 1721029L;
return (jdn);
}
/* converts from a given Julian day number to a formatted string of the form
** xx/yy/zz and places it in date_string. Returns the century (e.g. 19).
*/
int
julian_date (jdn, date_string)
long jdn;
char *date_string;
{
long year, month, day, temp_var;
int century;
temp_var = jdn - 1721119L;
year = (4 * temp_var - 1) / 146097L;
temp_var = 4 * temp_var - 1 - 146097L * year;
day = temp_var / 4;
temp_var = (4 * day + 3) / 1461;
day = 4 * day + 3 - 1461 * temp_var;
day = (day + 4) / 4;
month = (5 * day - 3) / 153;
day = 5 * day - 3 - 153 * month;
day = (day + 5) / 5;
year = 100 * year + temp_var;
if (month < 10)
month += 3;
else
{
month -= 9;
year++;
}
century = (int)year / 100;
year = year - (century * 100);
sprintf (date_string, "%02ld/%02ld/%02ld", month, day, year);
return ((int) century);
}
/* Return the difference between two dates in days as a signed long */
long
diff_date (date1, date2)
char *date1, *date2;
{
return (julian_day (date2) - julian_day (date1));
}
int
main (argc, argv)
int argc;
char *argv[];
{
if (argc != 3)
{
fprintf (stderr, "Usage: diffdate first_date second_date\n");
fprintf (stderr, "Where each date is of the form mm/dd/yy\n");
return 1;
}
printf ("Difference in days = %ld\n", diff_date (argv[1], argv[2]));
return 0;
}
>Muchos gracias in advance
>
>Kelly
Your Welcome.
Neil.
--
Neil Woods. | JANET: ex1neil at uk.ac.swan.pyr
University College Swansea, | UUCP : ...!mcvax!ukc!swan.pyr!ex1neil
Wales, UK. | ARPA : ex1neil%pyr.swan.ac.uk at nfsnet-relay.ac.uk
More information about the Comp.lang.c
mailing list