OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
time_utl.c
Go to the documentation of this file.
1 #include <sys/types.h>
2 #include <time.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "time_utl.h"
6 
7 /*
8  NOTE that the $SWFAPP/QCdisp/time_utl.c has the fortran code for
9  jddate
10  jdate
11  jd
12  from which the algorithms for the c code was taken
13  W. Robinson, SAIC, 23 Sep 2005
14 
15  */
16 int jd_to_yydoy(int jd, int *yy, int *doy)
17 /*******************************************************************
18 
19  jd_to_yydoy
20 
21  purpose: convert julian day to year and day of year
22 
23  Returns type: int - 0
24 
25  Parameters: (in calling order)
26  Type Name I/O Description
27  ---- ---- --- -----------
28  int jd I julian day (ref to 4xxx BC)
29  int * yy O 4 digit year
30  int * doy O day of the year
31 
32  Modification history:
33  Programmer Date Description of change
34  ---------- ---- ---------------------
35  W. Robinson, SAIC 23-Sep-2005 development from ftn code
36 
37  *******************************************************************/ {
38  int day1900;
39  /*
40  * Compute days since January 0, 1900 and years since 1900
41  */
42  day1900 = jd - 2415020;
43 
44  *yy = 4 * day1900 / 1461;
45  /*
46  * Compute day-of-year and Add first two digits of year
47  */
48  *doy = day1900 - 1461 * (*yy - 1) / 4 - 365;
49  *yy = *yy + 1900;
50  /*
51  * and end
52  */
53  return 0;
54 }
55 
56 int jd_to_ydoy(int jd, int *y, int *doy)
57 /*******************************************************************
58 
59  jd_to_yydoy
60 
61  purpose: convert julian day to year and day of year
62 
63  Returns type: int - 0
64 
65  Parameters: (in calling order)
66  Type Name I/O Description
67  ---- ---- --- -----------
68  int jd I julian day (ref to 4xxx BC)
69  int * y O 2 digit year
70  int * doy O day of the year
71 
72  Modification history:
73  Programmer Date Description of change
74  ---------- ---- ---------------------
75  W. Robinson, SAIC 23-Sep-2005 development from ftn code
76 
77  *******************************************************************/ {
78  int yy;
79  jd_to_yydoy(jd, &yy, doy);
80  *y = yy - 1900;
81  return 0;
82 }
83 
84 int yymd_to_jd(int yy, int m, int d)
85 /*******************************************************************
86 
87  yymd_to_jd
88 
89  purpose: convert year, month, day to julian day
90 
91  Returns type: int - julian day (ref 4xxx BC)
92 
93  Parameters: (in calling order)
94  Type Name I/O Description
95  ---- ---- --- -----------
96  int yy I 4 digit year
97  int m I month 1 - 12
98  int d I day of month 1 - 31
99 
100  Modification history:
101  Programmer Date Description of change
102  ---------- ---- ---------------------
103  W. Robinson, SAIC 23-Sep-2005 development from ftn version
104 
105  *******************************************************************/ {
106  return ( 367 * yy -
107  7 * (yy + (m + 9) / 12) / 4 +
108  275 * m / 9 +
109  d + 1721014);
110 }
111 
112 int yydoy_to_md70(int yy, int doy, int *m, int *d)
113 /*******************************************************************
114 
115  yydoy_to_md70
116 
117  purpose: convert year and day of year into month and day
118  NOTE *** only good for dates post 1970
119  see yydoy_to_md for more conventional but universal conversion
120 
121  Returns type: int - 0 no meaning
122 
123  Parameters: (in calling order)
124  Type Name I/O Description
125  ---- ---- --- -----------
126  int yy I 4 digit year
127  int doy I day of year
128  int * m O month
129  int * d O day of month
130 
131  *******************************************************************/ {
132  struct tm mytm, *out_tm;
133  time_t mytime;
134 
135  /*
136  * place the day of year in as day of month and set month to 0,
137  * mktime and localtime will set the month and day of month properly
138  */
139  mytm.tm_year = yy - 1900; /* Years after 1900 */
140  mytm.tm_mon = 0;
141  mytm.tm_mday = doy;
142  mytm.tm_sec = 0;
143  mytm.tm_min = 0;
144  mytm.tm_hour = 0;
145  mytm.tm_isdst = 0;
146 
147  mytime = mktime(&mytm);
148  out_tm = localtime(&mytime);
149 
150  *m = out_tm->tm_mon + 1;
151  *d = out_tm->tm_mday;
152  return 0;
153 }
154 
155 int yydoy_to_jd(int yy, int doy)
156 /*******************************************************************
157 
158  yydoy_to_jd
159 
160  purpose: convert 4 digit year, and day of year to julian day
161 
162  Returns type: int - julian day
163 
164  Parameters: (in calling order)
165  Type Name I/O Description
166  ---- ---- --- -----------
167  int yy I 2 digit year in 1900 - 1999
168  int doy I day of year
169 
170  *******************************************************************/ {
171  int m, d;
172  yydoy_to_md(yy, doy, &m, &d);
173  return yymd_to_jd(yy, m, d);
174 }
175 
176 int ydoy_to_jd(int y, int doy)
177 /*******************************************************************
178 
179  ydoy_to_jd
180 
181  purpose: convert 2 digit year, and day of year to julian day
182 
183  Returns type: int - julian day
184 
185  Parameters: (in calling order)
186  Type Name I/O Description
187  ---- ---- --- -----------
188  int y I 4 digit year
189  int doy I day of year
190 
191  *******************************************************************/ {
192  int yy;
193 
194  yy = y + 1900;
195  return yydoy_to_jd(yy, doy);
196 }
197 
198 int yydoy_to_md(int yy, int doy, int *m, int *d)
199 /*******************************************************************
200 
201  yydoy_to_md
202 
203  purpose: convert year and day of year into month and day
204 
205  Returns type: int - 0 no meaning
206 
207  Parameters: (in calling order)
208  Type Name I/O Description
209  ---- ---- --- -----------
210  int yy I 4 digit year
211  int doy I day of year
212  int * m O month
213  int * d O day of month
214 
215  Modification history:
216  Programmer Date Description of change
217  ---------- ---- ---------------------
218  W. Robinson, SAIC 7-Nov-2005 development from ftn gregor.f
219 
220  *******************************************************************/ {
221  int daymon[2][12] = {
222  { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
223  { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
224  };
225  int ly;
226 
227  ly = leap(yy);
228  *m = 11;
229  while (doy <= daymon[ly][*m])
230  *m -= 1;
231  *d = doy - daymon[ly][*m];
232  *m += 1;
233  return 0;
234 }
235 
236 int leap(int yy)
237 /*******************************************************************
238 
239  leap
240 
241  purpose: find if the year is a leap year
242 
243  Returns type: int - 0 if not leap year, 1 if leap year
244 
245  Parameters: (in calling order)
246  Type Name I/O Description
247  ---- ---- --- -----------
248  int yy I 4 digit year
249 
250  Modification history:
251  Programmer Date Description of change
252  ---------- ---- ---------------------
253  W. Robinson, SAIC 7-Nov-2005 development from leap.f
254 
255  *******************************************************************/ {
256  return ( (yy % 400 == 0) ||
257  ((yy % 4 == 0) && (yy % 100 != 0))) ? 1 : 0;
258 }
int ydoy_to_jd(int y, int doy)
Definition: time_utl.c:176
int yydoy_to_md(int yy, int doy, int *m, int *d)
Definition: time_utl.c:198
int yydoy_to_md70(int yy, int doy, int *m, int *d)
Definition: time_utl.c:112
int yymd_to_jd(int yy, int m, int d)
Definition: time_utl.c:84
int jd_to_yydoy(int jd, int *yy, int *doy)
Definition: time_utl.c:16
int yydoy_to_jd(int yy, int doy)
Definition: time_utl.c:155
float tm[MODELMAX]
int jd_to_ydoy(int jd, int *y, int *doy)
Definition: time_utl.c:56
int leap(int yy)
Definition: time_utl.c:236
integer mytime
Definition: names.f90:9
Definition: jd.py:1