#ifndef librock_ISOLATED /**************************************************************/ #define librock_IMPLEMENT_gregor #include /**************************************************************/ #endif #ifdef librock_IMPL_LIDESC #ifndef librock_NOIMPL_LIDESC_gregor /**************************************************************/ /* License awareness system. See http://www.mibsoftware.com/librock */ #include /* librock_LIDESC_HC=553e181388455f0eef17ccd9e2a51c1f3ae8daf0 */ char *librock_LIDESC_gregor[] = { "\n" __FILE__ librock_LIDESC_sdncal "\n", 0 }; /**************************************************************/ #endif #endif #ifdef librock_MANUAL_gregor librock_CHISEL subject /hard/time/ /* librock_gregor - Gregorian serial day number conversions (Scott E. Lee's sdncal library) */ /**/ #include void librock_SdnToGregorian( long int sdn, int *pYear, int *pMonth, int *pDay); /* Convert a SDN to a Gregorian calendar date. If the input SDN is less * than 1, the three output values will all be set to zero, otherwise * *pYear will be >= -4714 and != 0; *pMonth will be in the range 1 to 12 * inclusive; *pDay will be in the range 1 to 31 inclusive. */ long int librock_GregorianToSdn( int inputYear, int inputMonth, int inputDay); /* Convert a Gregorian calendar date to a SDN. Zero is returned when the * input date is detected as invalid or out of the supported range. The * return value will be > 0 for all valid, supported dates, but there are * some invalid dates that will return a positive value. To verify that a * date is valid, convert it to SDN and then back and compare with the * original. */ char *librock_MonthNameShort[13]; /* * Convert a Gregorian month number (1 to 12) to the abbreviated (three * character) name of the Gregorian month (null terminated). An index of * zero will return a zero length string. */ char *librock_MonthNameLong[13]; /* * Convert a Gregorian month number (1 to 12) to the name of the Gregorian * month (null terminated). An index of zero will return a zero length * string. */ /**/ /*The Gregorian calendar is the one which is most popular in use today in the world, so it is usually called "the calendar." The concept of SDN (serial date number) makes it easy to do date arithmetic. Watch out for days which cross over the conversion dates mentioned in the "Valid Range" section. */ /* This package defines a set of routines that convert calendar dates to * and from a serial day number (SDN). The SDN is a serial numbering of * days where SDN 1 is November 25, 4714 BC in the Gregorian calendar and * SDN 2447893 is January 1, 1990. This system of day numbering is * sometimes referred to as Julian days, but to avoid confusion with the * Julian calendar, it is referred to as serial day numbers here. The term * Julian days is also used to mean the number of days since the beginning * of the current year. * * The SDN can be used as an intermediate step in converting from one * calendar system to another (such as Gregorian to Jewish). It can also * be used for date computations such as easily comparing two dates, * determining the day of the week, finding the date of yesterday or * calculating the number of days between two dates. * * SDN values less than one are not supported. If a conversion routine * returns an SDN of zero, this means that the date given is either invalid * or is outside the supported range for that calendar. * * At least some validity checks are performed on input dates. For * example, a negative month number will result in the return of zero for * the SDN. A returned SDN greater than one does not necessarily mean that * the input date was valid. To determine if the date is valid, convert it * to SDN, and if the SDN is greater than zero, convert it back to a date * and compare to the original. For example: */ int y1, m1, d1; int y2, m2, d2; long int sdn; ... sdn = GregorianToSdn(y1, m1, d1); if (sdn > 0) { SdnToGregorian(sdn, &y2, &m2, &d2); if (y1 == y2 && m1 == m2 && d1 == d2) { ... date is valid ... } } /* * 4714 B.C. to at least 10000 A.D. * * Although this software can handle dates all the way back to 4714 * B.C., such use may not be meaningful. The Gregorian calendar was * not instituted until October 15, 1582 (or October 5, 1582 in the * Julian calendar). Some countries did not accept it until much * later. For example, Britain converted in 1752, The USSR in 1918 and * Greece in 1923. Most European countries used the Julian calendar * prior to the Gregorian. */ /* * * The Gregorian calendar is a modified version of the Julian calendar. * The only difference being the specification of leap years. The * Julian calendar specifies that every year that is a multiple of 4 * will be a leap year. This leads to a year that is 365.25 days long, * but the current accepted value for the tropical year is 365.242199 * days. * * To correct this error in the length of the year and to bring the * vernal equinox back to March 21, Pope Gregory XIII issued a papal * bull declaring that Thursday October 4, 1582 would be followed by * Friday October 15, 1582 and that centennial years would only be a * leap year if they were a multiple of 400. This shortened the year * by 3 days per 400 years, giving a year of 365.2425 days. * * Another recently proposed change in the leap year rule is to make * years that are multiples of 4000 not a leap year, but this has never * been officially accepted and this rule is not implemented in these * algorithms. * /* * * The calculations are based on three different cycles: a 400 year * cycle of leap years, a 4 year cycle of leap years and a 5 month * cycle of month lengths. * * The 5 month cycle is used to account for the varying lengths of * months. You will notice that the lengths alternate between 30 * and 31 days, except for three anomalies: both July and August * have 31 days, both December and January have 31, and February * is less than 30. Starting with March, the lengths are in a * cycle of 5 months (31, 30, 31, 30, 31): * * Mar 31 days \ * Apr 30 days | * May 31 days > First cycle * Jun 30 days | * Jul 31 days / * * Aug 31 days \ * Sep 30 days | * Oct 31 days > Second cycle * Nov 30 days | * Dec 31 days / * * Jan 31 days \ * Feb 28/9 days | * > Third cycle (incomplete) * * For this reason the calculations (internally) assume that the * year starts with March 1. */ /* * * This algorithm has been tested from the year 4714 B.C. to 10000 * A.D. The source code of the verification program is included in * this package. */ /* * * Conversions Between Calendar Date and Julian Day Number by Robert J. * Tantzen, Communications of the Association for Computing Machinery * August 1963. (Also published in Collected Algorithms from CACM, * algorithm number 199). */ /* // No external calls */ /* Copyright 1993-1995, Scott E. Lee, all rights reserved. Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=553e181388455f0eef17ccd9e2a51c1f3ae8daf0 */ #endif /* MANUAL */ /**************************************************************/ /* $selId: gregor.c,v 2.0 1995/10/24 01:13:06 lees Exp $ * Copyright 1993-1995, Scott E. Lee, all rights reserved. * Permission granted to use, copy, modify, distribute and sell so long as * the above copyright and this permission statement are retained in all * copies. THERE IS NO WARRANTY - USE AT YOUR OWN RISK. */ /************************************************************************** * * These are the externally visible components of this file: * * void * SdnToGregorian( * long int sdn, * int *pYear, * int *pMonth, * int *pDay); * * Convert a SDN to a Gregorian calendar date. If the input SDN is less * than 1, the three output values will all be set to zero, otherwise * *pYear will be >= -4714 and != 0; *pMonth will be in the range 1 to 12 * inclusive; *pDay will be in the range 1 to 31 inclusive. * * long int * GregorianToSdn( * int inputYear, * int inputMonth, * int inputDay); * * Convert a Gregorian calendar date to a SDN. Zero is returned when the * input date is detected as invalid or out of the supported range. The * return value will be > 0 for all valid, supported dates, but there are * some invalid dates that will return a positive value. To verify that a * date is valid, convert it to SDN and then back and compare with the * original. * * char *MonthNameShort[13]; * * Convert a Gregorian month number (1 to 12) to the abbreviated (three * character) name of the Gregorian month (null terminated). An index of * zero will return a zero length string. * * char *MonthNameLong[13]; * * Convert a Gregorian month number (1 to 12) to the name of the Gregorian * month (null terminated). An index of zero will return a zero length * string. * * VALID RANGE * * 4714 B.C. to at least 10000 A.D. * * Although this software can handle dates all the way back to 4714 * B.C., such use may not be meaningful. The Gregorian calendar was * not instituted until October 15, 1582 (or October 5, 1582 in the * Julian calendar). Some countries did not accept it until much * later. For example, Britain converted in 1752, The USSR in 1918 and * Greece in 1923. Most European countries used the Julian calendar * prior to the Gregorian. * * CALENDAR OVERVIEW * * The Gregorian calendar is a modified version of the Julian calendar. * The only difference being the specification of leap years. The * Julian calendar specifies that every year that is a multiple of 4 * will be a leap year. This leads to a year that is 365.25 days long, * but the current accepted value for the tropical year is 365.242199 * days. * * To correct this error in the length of the year and to bring the * vernal equinox back to March 21, Pope Gregory XIII issued a papal * bull declaring that Thursday October 4, 1582 would be followed by * Friday October 15, 1582 and that centennial years would only be a * leap year if they were a multiple of 400. This shortened the year * by 3 days per 400 years, giving a year of 365.2425 days. * * Another recently proposed change in the leap year rule is to make * years that are multiples of 4000 not a leap year, but this has never * been officially accepted and this rule is not implemented in these * algorithms. * * ALGORITHMS * * The calculations are based on three different cycles: a 400 year * cycle of leap years, a 4 year cycle of leap years and a 5 month * cycle of month lengths. * * The 5 month cycle is used to account for the varying lengths of * months. You will notice that the lengths alternate between 30 * and 31 days, except for three anomalies: both July and August * have 31 days, both December and January have 31, and February * is less than 30. Starting with March, the lengths are in a * cycle of 5 months (31, 30, 31, 30, 31): * * Mar 31 days \ * Apr 30 days | * May 31 days > First cycle * Jun 30 days | * Jul 31 days / * * Aug 31 days \ * Sep 30 days | * Oct 31 days > Second cycle * Nov 30 days | * Dec 31 days / * * Jan 31 days \ * Feb 28/9 days | * > Third cycle (incomplete) * * For this reason the calculations (internally) assume that the * year starts with March 1. * * TESTING * * This algorithm has been tested from the year 4714 B.C. to 10000 * A.D. The source code of the verification program is included in * this package. * * REFERENCES * * Conversions Between Calendar Date and Julian Day Number by Robert J. * Tantzen, Communications of the Association for Computing Machinery * August 1963. (Also published in Collected Algorithms from CACM, * algorithm number 199). * **************************************************************************/ #include "sdncal.h" #define SDN_OFFSET 32045 #define DAYS_PER_5_MONTHS 153 #define DAYS_PER_4_YEARS 1461 #define DAYS_PER_400_YEARS 146097 void SdnToGregorian( long int sdn, int *pYear, int *pMonth, int *pDay) { int century; int year; int month; int day; long int temp; int dayOfYear; if (sdn <= 0) { *pYear = 0; *pMonth = 0; *pDay = 0; return; } temp = (sdn + SDN_OFFSET) * 4 - 1; /* Calculate the century (year/100). */ century = temp / DAYS_PER_400_YEARS; /* Calculate the year and day of year (1 <= dayOfYear <= 366). */ temp = ((temp % DAYS_PER_400_YEARS) / 4) * 4 + 3; year = (century * 100) + (temp / DAYS_PER_4_YEARS); dayOfYear = (temp % DAYS_PER_4_YEARS) / 4 + 1; /* Calculate the month and day of month. */ temp = dayOfYear * 5 - 3; month = temp / DAYS_PER_5_MONTHS; day = (temp % DAYS_PER_5_MONTHS) / 5 + 1; /* Convert to the normal beginning of the year. */ if (month < 10) { month += 3; } else { year += 1; month -= 9; } /* Adjust to the B.C./A.D. type numbering. */ year -= 4800; if (year <= 0) year--; *pYear = year; *pMonth = month; *pDay = day; } long int GregorianToSdn( int inputYear, int inputMonth, int inputDay) { int year; int month; /* check for invalid dates */ if (inputYear == 0 || inputYear < -4714 || inputMonth <= 0 || inputMonth > 12 || inputDay <= 0 || inputDay > 31) { return(0); } /* check for dates before SDN 1 (Nov 25, 4714 B.C.) */ if (inputYear == -4714) { if (inputMonth < 11) { return(0); } if (inputMonth == 11 && inputDay < 25) { return(0); } } /* Make year always a positive number. */ if (inputYear < 0) { year = inputYear + 4801; } else { year = inputYear + 4800; } /* Adjust the start of the year. */ if (inputMonth > 2) { month = inputMonth - 3; } else { month = inputMonth + 9; year--; } return( ((year / 100) * DAYS_PER_400_YEARS) / 4 + ((year % 100) * DAYS_PER_4_YEARS) / 4 + (month * DAYS_PER_5_MONTHS + 2) / 5 + inputDay - SDN_OFFSET ); } char *MonthNameShort[13] = { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; char *MonthNameLong[13] = { "", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; /* $Log: gregor.c,v $ Revision 1.4 2002/04/19 14:45:10 forrest@mibsoftware.com rights=#1 Correct license tags. Revision 1.3 2002/02/05 21:08:54 forrest@mibsoftware.com rights=#1 Standardized log Revision 1.2 2002/01/30 16:07:38 forrest@mibsoftware.com rights=#1 Renamed some .h files Revision 1.1 2002/01/28 17:29:10 forrest@mibsoftware.com rights=#1 Initial rights#1 Copyright (c) Forrest J Cavalier III d-b-a Mib Software rights#1 License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 librock_ACQUIRED: 2001-10-30 ftp://ftp.genealogy.org/pub/users/scottlee/sdncal20.zip http://genealogy.org/~scottlee/cal-overview.html License text in librock_LIDESC_HC=553e181388455f0eef17ccd9e2a51c1f3ae8daf0 */