source: trunk/MagicSoft/slalib/clyd.c@ 13408

Last change on this file since 13408 was 731, checked in by tbretz, 24 years ago
*** empty log message ***
  • Property svn:executable set to *
File size: 2.7 KB
Line 
1#include "slalib.h"
2#include "slamac.h"
3void slaClyd ( int iy, int im, int id, int *ny, int *nd, int *jstat )
4/*
5** - - - - - - - -
6** s l a C l y d
7** - - - - - - - -
8**
9** Gregorian calendar to year and day in year (in a Julian calendar
10** aligned to the 20th/21st century Gregorian calendar).
11**
12** Given:
13** iy,im,id int year, month, day in Gregorian calendar
14**
15** Returned:
16** ny int year (re-aligned Julian calendar)
17** nd int day in year (1 = January 1st)
18** jstat int status:
19** 0 = OK
20** 1 = bad year (before -4711)
21** 2 = bad month
22** 3 = bad day (but conversion performed)
23**
24** Notes:
25**
26** 1 This routine exists to support the low-precision routines
27** slaEarth, slaMoon and slaEcor.
28**
29** 2 Between 1900 March 1 and 2100 February 28 it returns answers
30** which are consistent with the ordinary Gregorian calendar.
31** Outside this range there will be a discrepancy which increases
32** by one day for every non-leap century year.
33**
34** 3 The essence of the algorithm is first to express the Gregorian
35** date as a Julian Day Number and then to convert this back to
36** a Julian calendar date, with day-in-year instead of month and
37** day. See 12.92-1 and 12.95-1 in the reference.
38**
39** Reference: Explanatory Supplement to the Astronomical Almanac,
40** ed P.K.Seidelmann, University Science Books (1992),
41** p604-606.
42**
43** Last revision: 26 November 1994
44**
45** Copyright P.T.Wallace. All rights reserved.
46*/
47{
48 long i, j, k, l, n, iyL, imL;
49
50/* Month lengths in days */
51 static int mtab[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
52
53
54
55/* Validate year */
56 if ( iy < -4711 ) { *jstat = 1; return; }
57
58/* Validate month */
59 if ( ( im < 1 ) || ( im > 12 ) ) { *jstat = 2; return; }
60
61/* Allow for (Gregorian) leap year */
62 mtab[1] = ( ( ( iy % 4 ) == 0 ) &&
63 ( ( ( iy % 100 ) != 0 ) || ( ( iy % 400 ) == 0 ) ) ) ?
64 29 : 28;
65
66/* Validate day */
67 *jstat = ( id < 1 || id > mtab[im-1] ) ? 3 : 0;
68
69/* Perform the conversion */
70 iyL = (long) iy;
71 imL = (long) im;
72 i = ( 14 - imL ) /12L;
73 k = iyL - i;
74 j = ( 1461L * ( k + 4800L ) ) / 4L
75 + ( 367L * ( imL - 2L + 12L * i ) ) / 12L
76 - ( 3L * ( ( k + 4900L ) / 100L ) ) / 4L + (long) id - 30660L;
77 k = ( j - 1L ) / 1461L;
78 l = j - 1461L * k;
79 n = ( l - 1L ) / 365L - l / 1461L;
80 j = ( ( 80L * ( l - 365L * n + 30L ) ) / 2447L ) / 11L;
81 i = n + j;
82 *nd = 59 + (int) ( l -365L * i + ( ( 4L - n ) / 4L ) * ( 1L - j ) );
83 *ny = (int) ( 4L * k + i ) - 4716;
84}
Note: See TracBrowser for help on using the repository browser.