calendar.c 7.22 KB
Newer Older
1
#include <limits.h>
2
#include <stdio.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
3

4
#include "cdi.h"  		/* CALENDAR_ */
5
#include "calendar.h"
6
#include "error.h"
7
#include "timebase.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
8
9


10
11
12
static const int month_360[12] = {30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30};
static const int month_365[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static const int month_366[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
13
14


15
16
int calendar_dpy(int calendar)
{
17
  int daysperyear = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
18

19
20
21
  if      ( calendar == CALENDAR_360DAYS ) daysperyear = 360;
  else if ( calendar == CALENDAR_365DAYS ) daysperyear = 365;
  else if ( calendar == CALENDAR_366DAYS ) daysperyear = 366;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
22

23
  return daysperyear;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
24
25
26
}


27
int days_per_month(int calendar, int year, int month)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
28
{
29
  int daysperyear = calendar_dpy(calendar);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
30

31
  const int *dpm;
32
33
34
  if      ( daysperyear == 360 ) dpm = month_360;
  else if ( daysperyear == 365 ) dpm = month_365;
  else                           dpm = month_366;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
35

36
  int dayspermonth = 0;
37
38
  if ( month >= 1 && month <= 12 )
    dayspermonth = dpm[month-1];
39
  /*
40
41
  else
    fprintf(stderr, "days_per_month: month %d out of range\n", month);
42
  */
43
  if ( daysperyear == 0 && month == 2 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
44
    {
45
46
47
48
      if ( (year%4 == 0 && year%100 != 0) || year%400 == 0 )
	dayspermonth = 29;
      else
	dayspermonth = 28;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
49
50
    }

51
  return dayspermonth;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
52
53
54
}


55
int days_per_year(int calendar, int year)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
56
{
57
  int daysperyear = calendar_dpy(calendar);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
58

59
  if ( daysperyear == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
60
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
61
      if ( year == 1582 && (calendar == CALENDAR_STANDARD || calendar == CALENDAR_GREGORIAN) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
62
63
64
        daysperyear = 355;
      else if ( (year%4 == 0 && year%100 != 0) || year%400 == 0 )
        daysperyear = 366;
65
      else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
66
        daysperyear = 365;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
67
68
    }

69
  return daysperyear;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
70
71
72
}


73
static void decode_day(int dpy, int days, int *year, int *month, int *day)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
74
{
75
  int i = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
76

77
78
  *year = (days-1) / dpy;
  days -= (*year*dpy);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
79

80
  const int *dpm = NULL;
81
82
83
  if      ( dpy == 360 ) dpm = month_360;
  else if ( dpy == 365 ) dpm = month_365;
  else if ( dpy == 366 ) dpm = month_366;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
84

85
86
87
88
89
90
  if ( dpm )
    for ( i = 0; i < 12; i++ )
      {
	if ( days > dpm[i] ) days -= dpm[i];
	else break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
91

92
93
  *month = i + 1;
  *day   = days;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
94
95
96
}


97
static int encode_day(int dpy, int year, int month, int day)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
98
{
99
  long rval = (long)dpy * year + day;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
100

101
  const int *dpm = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
102
103
104
  if      ( dpy == 360 ) dpm = month_360;
  else if ( dpy == 365 ) dpm = month_365;
  else if ( dpy == 366 ) dpm = month_366;
105

106
  if ( dpm ) for ( int i = 0; i < month-1; i++ ) rval += dpm[i];
107
  if ( rval > INT_MAX || rval < INT_MIN )
108
    Error("Unhandled date: %ld", rval);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
109

110
  return (int)rval;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
111
112
113
}


114
115
void encode_caldaysec(int calendar, int year, int month, int day, int hour, int minute, int second,
		      int *julday, int *secofday)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
116
{
117
  int dpy = calendar_dpy(calendar);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
118

119
  if ( dpy == 360 || dpy == 365 || dpy == 366 )
120
    *julday = encode_day(dpy, year, month, day);
121
  else
122
    *julday = encode_julday(calendar, year, month, day);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
123

124
  *secofday = hour*3600 + minute*60 + second;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
125
126
127
}


128
void decode_caldaysec(int calendar, int julday, int secofday,
129
		      int *year, int *month, int *day, int *hour, int *minute, int *second)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
130
{
131
  int dpy = calendar_dpy(calendar);
132
133

  if ( dpy == 360 || dpy == 365 || dpy == 366 )
134
    decode_day(dpy, julday, year, month, day);
135
  else
136
    decode_julday(calendar, julday, year, month, day);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
137

138
139
140
  *hour   = secofday/3600;
  *minute = secofday/60 - *hour*60;
  *second = secofday - *hour*3600 - *minute*60;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
141
142
143
}


144
145
#ifdef TEST
static int date_to_calday(int calendar, int date)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
146
{
147
  int dpy = calendar_dpy(calendar);
148

149
  int year, month, day;
150
151
  cdiDecodeDate(date, &year, &month, &day);

152
  int calday;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
153
  if ( dpy == 360 || dpy == 365 || dpy == 366 )
154
    calday = encode_day(dpy, year, month, day);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
155
  else
156
    calday = encode_julday(calendar, year, month, day);
157

158
  return calday;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
159
160
161
}


162
static int calday_to_date(int calendar, int calday)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
163
{
164
  int year, month, day;
165
  int dpy = calendar_dpy(calendar);
166

Uwe Schulzweida's avatar
Uwe Schulzweida committed
167
  if ( dpy == 360 || dpy == 365 || dpy == 366 )
168
    decode_day(dpy, calday, &year, &month, &day);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
169
  else
170
    decode_julday(calendar, calday, &year, &month, &day);
171

172
  int date = cdiEncodeDate(year, month, day);
173

174
  return date;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
175
176
}

177

Uwe Schulzweida's avatar
Uwe Schulzweida committed
178
179
int main(void)
{
180
181
182
183
184
185
  int calendar = CALENDAR_STANDARD;
  int nmin;
  int vdate0, vtime0;
  int vdate, vtime;
  int ijulinc;
  int i, j = 0;
186
  int year, mon, day, hour, minute, second;
187
188
189
190
191
192
  int calday, secofday;

  /* 1 - Check valid range of years */

  nmin = 11000;
  vdate0 = -80001201;
193
  vtime0 = 120500;
194
195
196
197
198

  printf("start time: %8d %4d\n", vdate0, vtime0);

  for ( i = 0; i < nmin; i++ )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
199
200
      cdiDecodeDate(vdate0, &year, &mon, &day);
      cdiDecodeTime(vtime0, &hour, &minute, &second);
201
202
203
204
205
206
207
208
209
210
211
212

      calday  = date_to_calday(calendar, vdate0);
      secofday = time_to_sec(vtime0);

      vdate = calday_to_date(calendar, calday);
      vtime = sec_to_time(secofday);

      if ( vdate0 != vdate || vtime0 != vtime )
	printf("%4d %8d %4d %8d %4d %9d %9d\n",
	       ++j, vdate0, vtime0, vdate, vtime, calday, secofday);

      year++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
213
214
      vdate0 = cdiEncodeDate(year, mon, day);
      vtime0 = cdiEncodeTime(hour, minute, second);
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
    }

  printf("stop time: %8d %4d\n", vdate0, vtime0);

  /* 2 - Check time increment of one minute */

  nmin = 120000;
  ijulinc = 60;
  vdate0 = 20001201;
  vtime0 = 0;

  printf("start time: %8d %4d\n", vdate0, vtime0);

  calday = date_to_calday(calendar, vdate0);
  secofday = time_to_sec(vtime0);
  for ( i = 0; i < nmin; i++ )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
232
233
      cdiDecodeDate(vdate0, &year, &mon, &day);
      cdiDecodeTime(vtime0, &hour, &minute, &second);
234

235
      if ( ++minute >= 60 )
236
	{
237
	  minute = 0;
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
	  if ( ++hour >= 24 )
	    {
	      hour = 0;
	      if ( ++day >= 32 )
		{
		  day = 1;
		  if ( ++mon >= 13 )
		    {
		      mon = 1;
		      year++;
		    }
		}
	    }
	}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
253
254
      vdate0 = cdiEncodeDate(year, mon, day);
      vtime0 = cdiEncodeTime(hour, minute, second);
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277

      julday_add_seconds(ijulinc, &calday, &secofday);

      vdate = calday_to_date(calendar, calday);
      vtime = sec_to_time(secofday);
      if ( vdate0 != vdate || vtime0 != vtime )
	printf("%4d %8d %4d %8d %4d %9d %9d\n",
	       ++j, vdate0, vtime0, vdate, vtime, calday, secofday);
    }

  printf("stop time: %8d %4d\n", vdate0, vtime0);

  return (0);
}
#endif


#ifdef TEST2
int main(void)
{
  int calendar = CALENDAR_STANDARD;
  int i;
  int calday, secofday;
278
  int year, month, day, hour, minute, second;
279
280
  int value = 30;
  int factor = 86400;
281

282
  calendar = CALENDAR_360DAYS;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
283

284
  year=1979; month=1; day=15; hour=12; minute=30; second = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
285

286
  printf("calendar = %d\n", calendar);
287
  printf("%d/%02d/%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
288

289
  encode_caldaysec(calendar, year, month, day, hour, minute, second, &calday, &secofday);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
290

291
292
  decode_caldaysec(calendar, calday, secofday, &year, &month, &day, &hour, &minute, &second);
  printf("%d/%02d/%02d %02d:%02d:%02d   %d %d\n", year, month, day, hour, minute, second, calday, secofday);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
293
294
295

  for ( i = 0; i < 420; i++ )
    {
296

297
298
      decode_caldaysec(calendar, calday, secofday, &year, &month, &day, &hour, &minute, &second);
      printf("%2d %d/%02d/%02d %02d:%02d:%02d\n", i, year, month, day, hour, minute, second);
299
      julday_add_seconds(value*factor, &calday, &secofday);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
300
301
302
303
    }

  return (0);
}
304
#endif
305
306
307
308
309
310
311
312
313
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */