aboutsummaryrefslogtreecommitdiff
path: root/math/slalib/clyd.f
blob: 957a6ec8cc58791c9eff73a0006234b699ed0b1d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
SUBROUTINE slCLYD (IY, IM, ID, NY, ND, JSTAT)
*+
*     - - - - -
*      C L Y D
*     - - - - -
*
*  Gregorian calendar to year and day in year (in a Julian calendar
*  aligned to the 20th/21st century Gregorian calendar).
*
*  Given:
*     IY,IM,ID   i    year, month, day in Gregorian calendar
*
*  Returned:
*     NY         i    year (re-aligned Julian calendar)
*     ND         i    day in year (1 = January 1st)
*     JSTAT      i    status:
*                       0 = OK
*                       1 = bad year (before -4711)
*                       2 = bad month
*                       3 = bad day (but conversion performed)
*
*  Notes:
*
*  1  This routine exists to support the low-precision routines
*     slERTH, slMOON and slECOR.
*
*  2  Between 1900 March 1 and 2100 February 28 it returns answers
*     which are consistent with the ordinary Gregorian calendar.
*     Outside this range there will be a discrepancy which increases
*     by one day for every non-leap century year.
*
*  3  The essence of the algorithm is first to express the Gregorian
*     date as a Julian Day Number and then to convert this back to
*     a Julian calendar date, with day-in-year instead of month and
*     day.  See 12.92-1 and 12.95-1 in the reference.
*
*  Reference:  Explanatory Supplement to the Astronomical Almanac,
*              ed P.K.Seidelmann, University Science Books (1992),
*              p604-606.
*
*  P.T.Wallace   Starlink   26 November 1994
*
*  Copyright (C) 1995 Rutherford Appleton Laboratory
*
*  License:
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program (see SLA_CONDITIONS); if not, write to the
*    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
*    Boston, MA  02110-1301  USA
*
*  Copyright (C) 1995 Association of Universities for Research in Astronomy Inc.
*-

      IMPLICIT NONE

      INTEGER IY,IM,ID,NY,ND,JSTAT

      INTEGER I,J,K,L,N

*  Month lengths in days
      INTEGER MTAB(12)
      DATA MTAB/31,28,31,30,31,30,31,31,30,31,30,31/



*  Preset status
      JSTAT=0

*  Validate year
      IF (IY.GE.-4711) THEN

*     Validate month
         IF (IM.GE.1.AND.IM.LE.12) THEN

*        Allow for (Gregorian) leap year
            IF (MOD(IY,4).EQ.0.AND.
     :         (MOD(IY,100).NE.0.OR.MOD(IY,400).EQ.0)) THEN
               MTAB(2)=29
            ELSE
               MTAB(2)=28
            END IF

*        Validate day
            IF (ID.LT.1.OR.ID.GT.MTAB(IM)) JSTAT=3

*        Perform the conversion
            I=(14-IM)/12
            K=IY-I
            J=(1461*(K+4800))/4+(367*(IM-2+12*I))/12
     :        -(3*((K+4900)/100))/4+ID-30660
            K=(J-1)/1461
            L=J-1461*K
            N=(L-1)/365-L/1461
            J=((80*(L-365*N+30))/2447)/11
            I=N+J
            ND=59+L-365*I+((4-N)/4)*(1-J)
            NY=4*K+I-4716

*        Bad month
         ELSE
            JSTAT=2
         END IF
      ELSE

*     Bad year
         JSTAT=1
      END IF

      END