OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
julian.c
Go to the documentation of this file.
1 /*
2  *----------------------------------------------------------------------
3  * @(#) julian.c 1.0 20 Mar 93 <shc>
4  * Copyright (c) 1993, CSIRO Division of Oceanography.
5  * Copyright (c) 1998, Datron Transco Inc..
6  *----------------------------------------------------------------------
7  *
8  * julian --
9  *
10  * A collection of routines for converting between normal time/date
11  * formats and Julian dates.
12  *
13  * History:
14  * 25 Oct 93 <shc>
15  * Initial version
16  *
17  * 05 Jun 98 <shc>
18  * Divided into julday/caldat and other utility routines.
19  *
20  *----------------------------------------------------------------------
21  */
22 
23 #include <math.h>
24 
25 #include "orbit.h" /* Prototype checking */
26 
27 
28 /*
29  * This routine returns the Julian Day Number which begins at noon of
30  * the calendar date specified by the arguments. Positive year signifies
31  * A.D., negative B.C. Remember that the year after 1 B.C. was 1 A.D.
32  */
33 
34 
35 #define IGREG1 (15L + 31L * (10L + 12L * 1582L))
36 
37 int32_t julday(year, month, day)
38 int year, month, day;
39 {
40  int32_t y, m, adj, date;
41 
42  if (year == 0)
43  return (-1);
44 
45  if (year < 0)
46  year += 1;
47 
48  if (month > 2) {
49  y = year;
50  m = month + 1;
51  } else {
52  y = year - 1;
53  m = month + 13;
54  }
55 
56  date = (int32_t) floor(365.25 * y) + (int32_t) (30.6001 * m) +
57  (int32_t) day + 1720995L;
58 
59  if ((int32_t) day + 31 * ((int32_t) month + 12 * (int32_t) year) > IGREG1) {
60  adj = (int32_t) (0.01 * y);
61  date = date + 2 - adj + (int32_t) (0.25 * adj);
62  }
63 
64  return (date);
65 }
66 
67 
68 /*
69  * Inverse of the function "julday" above.
70  */
71 
72 #define IGREG2 2299161L
73 
74 void
75 caldat(julian, year, month, day)
76 int32_t julian;
77 int *year, *month, *day;
78 {
79  int32_t ja, jb, jc, jd, je, jalpha;
80 
81  if (julian < IGREG2) {
82  ja = julian;
83  } else {
84  jalpha = (int32_t) (((julian - 1867216L) - 0.25) / 36524.25);
85  ja = julian + 1 + jalpha - (int32_t) (0.25 * jalpha);
86  }
87 
88  jb = ja + 1524;
89  jc = (int32_t) (6680.0 + ((jb - 2439870L) - 122.1) / 365.25);
90  jd = 365 * jc + (int32_t) (0.25 * jc);
91  je = (int32_t) ((jb - jd) / 30.6001);
92 
93  *day = jb - jd - (int32_t) (30.6001 * je);
94  *month = je - 1;
95  if (*month > 12)
96  *month = *month - 12;
97  *year = jc - 4715;
98 
99  if (*month > 2)
100  *year = *year - 1;
101  if (*year <= 0)
102  *year = *year - 1;
103 
104  return;
105 }
int32_t day
#define L(lambda, T)
Definition: PreprocessP.h:185
function jd(i, j, k)
Definition: jd.f:2
#define IGREG2
Definition: julian.c:72
int32_t julday(int year, int month, int day)
Definition: julian.c:37
int jb
Definition: atrem_corl1.h:227
Definition: jd.py:1
float ja
Definition: atrem_cor.h:114
integer function julian(DAY, MONTH, YEAR)
Definition: julian.f:2
void caldat(int32_t julian, int *year, int *month, int *day)
Definition: julian.c:75
#define IGREG1
Definition: julian.c:35