timeutil.c 5.9 KB
Newer Older
1
/* this is <timeutil.c>
2
3
 * 
 * $id:$
4
5
6
7
8
9
 *
 * for manipulating absolute data times
 *
 * 25/04/97   Thomas Forbriger (IfG Stuttgart)
 * 27/05/97   timeutil_norm did ignore carry to doy
 *
10
11
12
13
 * This is a pure C pre-version of libtime.f
 * By now (5/8/2000) it is still included in libtime.a but provides no extra
 * functionality. It may be removed in the future.
 *
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
 */

#include <stdio.h>
#include <stddef.h>
#include <timeutil.h>
#include <tfmacros.h>

/*
 * some constants we need internally
 * =================================
 */

/* days in each month */
static int timeutil_DaysInYear[13] \
    ={0,31,28,31,30,31,30,31,31,30,31,30,31};
static int timeutil_DaysInLeapYear[13] \
    ={0,31,29,31,30,31,30,31,31,30,31,30,31};

/* value limits */
static long int timeutil_limits[TIMEUTIL_N_ELEMENTS] \
  = {-1L, -1L, -1L, -1L, 24L, 60L, 60L, 1000L, 1000L};

/*
 * clear time structure
 * ====================
 */
void timeutil_clear(td)
timeutil_Ttime *td;
{
  long int *ptr;
  int i;

  ptr=(long int *)td;
  for (i=0; i<TIMEUTIL_N_ELEMENTS; i++) {
    ptr[i]=0;
  }
} /* timeutil_clear */

/*
 * write time to string
 * ====================
 */
char *timeutil_print(td)
timeutil_Ttime td;
{
  static char string[35];
  timeutil_finish(&td);
  sprintf(string, "%3d %2.2d/%2.2d/%4.4d %2.2d:%2.2d:%2.2d.%3.3d%3.3d",
    td.doy, td.day, td.month, td.year,
    td.hour, td.min, td.sec, td.msec, td.usec);
  return string;
} /* timeutil_print */

/*
 * finish preset time structure
 * ============================
 *
 * this is used to set the doy-value and the full year
 */
void timeutil_finish(td)
timeutil_Ttime *td;
{
  if (td->year<70) td->year=td->year+2000;
  if (td->year<100) td->year=td->year+1900;
  td->doy=timeutil_doy(*td);
  timeutil_norm(td);
} /* timeutil_finish */

/* 
 * calculate day of year (doy)
 * ===========================
 */
long int timeutil_doy(td)
timeutil_Ttime td;
{
  int *days;
  long int doy;
  int i;
  
  days=timeutil_DaysInYear;
  if (timeutil_is_leap(td.year)==1) { days=timeutil_DaysInLeapYear; }

  doy=0;
  i=1;
  while (i<td.month) {
    doy=doy+days[i];
    i=i+1;
    CHECKERROR((i>13),"timeutil_doy","month value out of range")
  }
  doy=doy+td.day;
  return doy;
} /* timeutil_doy */

/*
 * check wheter year in data is leap-year or not
 * =============================================
 * 
 * return code
 * 0:  no leap-year
 * 1:  is leap-year
 */
int timeutil_is_leap(year)
long int year;
{
  int res1, res2, res3;

  if (year<70) { year = year + 2000; }
  if (year<100) { year = year + 1900; }

  res1=(int)(year-(int)(year/4)*4);
  res2=(int)(year-(int)(year/100)*100);
  res3=(int)(year-(int)(year/400)*400);

  if (((res1==0) && (res2!=0)) || (res3==0))
  {
    return 1;
  } else {
    return 0;
  }
} /* timeutil_is_leap */

/* 
 * calculate date from day of year (doy)
 * =====================================
 */
void timeutil_date(td, doy)
timeutil_Ttime *td;
long int doy;
{
  int *days;

  td->doy=doy;

  days=timeutil_DaysInYear;
  if (timeutil_is_leap(td->year)==1) { days=timeutil_DaysInLeapYear; }

  td->day=doy;
  td->month=1;
  while(td->day>days[td->month]) {
    td->day=td->day-days[td->month];
    td->month=td->month+1;
    CHECKERROR((td->month>13), "timeutil_date", "doy value out of range")
  }
} /* timeutil_date */

/*
 * normalize values in time structure to allowed ranges
 * ====================================================
 *
 * the date is taken from the doy value in this case
 */
void timeutil_norm(td)
timeutil_Ttime *td;
{
  int i;
  long int carry, diy;
  long int *ptr;

  ptr=(long int *)td;

  /* set correct values up to hours            */
  /* carry to doy values is worked out         */
  /* but doy value may still be incorrect      */
  for (i=TIMEUTIL_N_ELEMENTS-1; i>=TIMEUTIL_FIRST_LINEAR-1; i--) {
    carry=(long int)(ptr[i]/timeutil_limits[i]);
    if (ptr[i]<0) carry=carry-1;
    ptr[i]=ptr[i]-carry*timeutil_limits[i];
    ptr[i-1]=ptr[i-1]+carry;
  }

  /* catch day carry for doy */
  td->doy=td->doy+carry;

  /* work on doy and year                      */
  if (td->year<70) td->year=td->year+2000;
  if (td->year<100) td->year=td->year+1900;
  if (td->doy>0) {
    diy=365;
    if (timeutil_is_leap(td->year)==1) diy=366;
    while (td->doy>diy) {
      td->doy=td->doy-diy;
      td->year=td->year+1;
      diy=365;
      if (timeutil_is_leap(td->year)==1) diy=366;
    }
  } else {
    while (td->doy<1) {
      td->year=td->year-1;
      diy=365;
      if (timeutil_is_leap(td->year)==1) diy=366;
      td->doy=td->doy+diy;
    }
  }
  timeutil_date(td, td->doy);
} /* timeutil_norm */

/*
 * add to time records
 * ===================
 */
void timeutil_add(sum, td1, td2)
timeutil_Ttime *sum;
timeutil_Ttime td1;
timeutil_Ttime td2;
{
  sum->usec  =td1.usec   +td2.usec;
  sum->msec  =td1.msec   +td2.msec;
  sum->sec   =td1.sec    +td2.sec;
  sum->min   =td1.min    +td2.min;
  sum->hour  =td1.hour   +td2.hour;
  sum->doy   =td1.doy    +td2.doy;
  sum->year  =td1.year   +td2.year;
  timeutil_norm(sum);
} /* timeutil_add */

/*
 * compare two data times
 * ======================
 *
 * returnvalue:
 *   0:  both are equal
 *   1:  time1>time2
 *  -1:  time1<time2
 */
int timeutil_compare(time1, time2)
timeutil_Ttime time1;
timeutil_Ttime time2;
{
  if (time1.year  >time2.year)   return 1;
  if (time1.year  <time2.year)   return -1;
  if (time1.month >time2.month)  return 1;
  if (time1.month <time2.month)  return -1;
  if (time1.day   >time2.day)    return 1;
  if (time1.day   <time2.day)    return -1;
  if (time1.hour  >time2.hour)   return 1;
  if (time1.hour  <time2.hour)   return -1;
  if (time1.min   >time2.min)    return 1;
  if (time1.min   <time2.min)    return -1;
  if (time1.sec   >time2.sec)    return 1;
  if (time1.sec   <time2.sec)    return -1;
  if (time1.msec  >time2.msec)   return 1;
  if (time1.msec  <time2.msec)   return -1;
  if (time1.usec  >time2.usec)   return 1;
  if (time1.usec  <time2.usec)   return -1;
  return 0;
} /* timeutil_compare */

/***** END OF timeutil.c *****/