Merge remote-tracking branch 'origin/release-v4.5.2'
[WRF.git] / var / convertor / decode_l2_airs / geth_newdate.c
blob8f0eeef0683e9b669bf0fb94dff298dc1d6615c4
1 #include <stdio.h>
2 #include <stdlib.h>
4 #ifdef NOUNDERSCORE
5 #define geth_newdate geth_newdate
6 #else
7 #define geth_newdate geth_newdate_
8 #endif
10 int nfeb(int year);
11 int geti(char *s, int p, int l);
13 void geth_newdate(char * date, int * increment, char * newdate)
15 char *h, *hh;
16 char *g;
17 char *k;
18 int yrold, moold, dyold, hrold, miold, scold, frold, i, j;
19 int yrnew, monew, dynew, hrnew, minew, scnew, frnew;
20 int idt;
21 int mday[12];
23 int ifrc;
24 int ilen;
25 int jadd;
26 int nday, nhour, nmin, nsec, nfrac;
27 char Continuous, SwapFmt;
29 ilen = 10;
31 mday[0] = 31;
32 mday[1] = 28;
33 mday[2] = 31;
34 mday[3] = 30;
35 mday[4] = 31;
36 mday[5] = 30;
37 mday[6] = 31;
38 mday[7] = 31;
39 mday[8] = 30;
40 mday[9] = 31;
41 mday[10] = 30;
42 mday[11] = 31;
44 date[ilen] = '\0';
46 SwapFmt = 1;
47 idt = *increment;
48 hh = date;
50 yrold = 0;
51 moold = 0;
52 dyold = 0;
53 hrold = 0;
54 miold = 0;
55 scold = 0;
56 frold = 0;
58 /* We have the form YYYYMMDDHH */
59 Continuous = 1;
60 jadd = 0;
61 yrold = geti(hh, 0, 4);
62 moold = geti(hh, 4, 2);
63 jadd ++;
64 dyold = geti(hh, 6, 2);
65 jadd ++;
66 if (ilen > 8) {
67 hrold = geti(hh, 8, 2);
68 jadd ++;
69 if (ilen > 10) {
70 miold = geti(hh, 10,2);
71 jadd ++;
72 if (ilen > 12) {
73 scold = geti(hh, 12,2);
74 jadd ++;
75 if (ilen > 13) {
76 frold = geti(hh, 14,ilen-13);
77 jadd++;
82 ilen += jadd;
84 mday[1] = nfeb(yrold);
86 /* Compute the number of days, hours, minutes, and seconds in idt */
88 if (ilen > 19) /*idt should be in fractions of seconds*/
90 if (ilen == 20) ifrc = 10;
91 if (ilen == 21) ifrc = 100;
92 if (ilen == 22) ifrc = 1000;
93 if (ilen == 23) ifrc = 10000;
94 nday = abs(idt)/(86400*ifrc);
95 nhour = div(abs(idt), 86400*ifrc).rem/(3600*ifrc);
96 nmin = div(abs(idt), 3600*ifrc).rem/(60*ifrc);
97 nsec = div(abs(idt), 60*ifrc).rem/ifrc;
98 nfrac = div(abs(idt), ifrc).rem;
100 else if (ilen > 17) /* idt should be in seconds */
102 ifrc = 1;
103 nday = abs(idt)/86400; /* Integer number of days in delta-time */
104 nhour = div(abs(idt), 86400).rem/3600;
105 nmin = div(abs(idt), 3600).rem/60;
106 nsec = div(abs(idt), 60).rem;
107 nfrac = 0;
109 else if (ilen > 14) /*idt should be in minutes */
111 ifrc = 1;
112 nday = abs(idt)/1440; /* Integer number of days in delta-time */
113 nhour = div(abs(idt), 1440).rem/60;
114 nmin = div(abs(idt), 60).rem;
115 nsec = 0;
116 nfrac = 0;
118 else if (ilen > 11) /* idt should be in hours */
120 ifrc = 1;
121 nday = abs(idt)/24; /* Integer number of days in delta-time */
122 nhour = div(abs(idt),24).rem;
123 nmin = 0;
124 nsec = 0;
125 nfrac = 0;
127 else if (ilen > 8) /* idt should be in days */
129 ifrc = 1;
130 nday = abs(idt); /* Integer number of days in delta-time */
131 nhour = 0;
132 nmin = 0;
133 nsec = 0;
134 nfrac = 0;
136 else
138 printf("Strangeness\n");
141 if (idt == 0) {
142 yrnew = yrold;
143 monew = moold;
144 dynew = dyold;
145 hrnew = hrold;
146 minew = miold;
147 scnew = scold;
148 frnew = frold;
150 else if (idt > 0) {
152 frnew = frold + nfrac;
153 if (frnew >= ifrc) {
154 frnew = frnew - ifrc;
155 nsec = nsec + 1;}
157 scnew = scold + nsec;
158 if (scnew >= 60) {
159 scnew = scnew - 60;
160 nmin = nmin + 1;}
162 minew = miold + nmin;
163 if (minew >= 60) {
164 minew = minew - 60;
165 nhour = nhour + 1;}
167 hrnew = hrold + nhour;
168 if (hrnew >= 24) {
169 hrnew = hrnew - 24;
170 nday = nday + 1; }
172 dynew = dyold;
173 monew = moold;
174 yrnew = yrold;
175 for(i = 1; i <= nday; i++){
176 dynew = dynew + 1;
177 if (dynew > mday[monew-1]) {
178 dynew = dynew - mday[monew-1];
179 monew = monew + 1;
180 if (monew > 12) {
181 monew = 1;
182 yrnew = yrnew + 1;
183 /* If the year changes, recompute the number of days in February */
184 mday[1] = nfeb(yrnew); }
189 else if (idt < 0) {
191 frnew = frold - nfrac;
192 if (frnew < 0) {
193 frnew = frnew + ifrc;
194 nsec = nsec - 1; }
196 scnew = scold - nsec;
197 if (scnew < 00) {
198 scnew = scnew + 60;
199 nmin = nmin + 1; }
201 minew = miold - nmin;
202 if (minew < 00) {
203 minew = minew + 60;
204 nhour = nhour + 1;}
206 hrnew = hrold - nhour;
207 if (hrnew < 00) {
208 hrnew = hrnew + 24;
209 nday = nday + 1; }
211 dynew = dyold;
212 monew = moold;
213 yrnew = yrold;
214 for(i = 1; i <= nday; i++){
215 dynew = dynew - 1;
216 if (dynew == 0) {
217 monew = monew - 1;
218 if (monew == 0) {
219 monew = 12;
220 yrnew = yrnew - 1;
221 /* If the year changes, recompute the number of days in February */
222 mday[1] = nfeb(yrnew);
224 dynew = mday[monew-1];
229 sprintf(newdate, "%4.4i%2.2i%2.2i%2.2i",yrnew, monew, dynew, hrnew);
233 int nfeb(int year)
235 int nfeb;
237 nfeb = 28; /* By default, February has 28 days ... */
238 if (div(year,4).rem == 0){
239 nfeb = 29; /* But every four years, it has 29 days ... */
240 if (div(year,100).rem == 0) {
241 nfeb = 28; /* Except every 100 years, when it has 28 days ... */
242 if (div(year,400).rem == 0){
243 nfeb = 29; /* Except every 400 years, when it has 29 days ... */
244 if (div(year,3600).rem == 0){
245 nfeb = 28; /* Except every 3600 years, when it has 28 days. */
250 return nfeb;
253 int geti(char *s, int p, int l)
255 int i, ival;
256 ival = 0;
257 for (i=0; i<l; i++) {
258 ival = (ival*10) + s[p+i]-'0';
260 return ival;