libtime++: Date and time calculation
ctlibtime.c
Go to the documentation of this file.
1 /* this is <ctlibtime.c>
2  * ----------------------------------------------------------------------------
3  *
4  * 12/08/97 by Thomas Forbriger (IfG Stuttgart)
5  *
6  * C version of libtime test program
7  *
8  * ----
9  * libtime is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  * ----
23  *
24  * REVISIONS and CHANGES
25  * 12/08/97 V1.0 Thomas Forbriger
26  * 13/12/07 V1.1 started migration to 64bit
27  * 17/12/07 V1.2 migrated code works
28  *
29  * ============================================================================
30  */
31 
32 #include <libtime.h>
33 #include <stdio.h>
34 #include <string.h>
35 
36 void subaddtest(date1,date2)
37 time_Ts date1;
38 time_Ts date2;
39 {
40  time_Ts date3, date4;
41  char string[TIME_SLEN];
42  strcpy(string,time_sprint(date1));
43  printf("*** date1: %s\n",string);
44  strcpy(string,time_sprint(date2));
45  printf(" date2: %s\n",string);
46  time_sub(date1, date2, &date3);
47  strcpy(string,time_sprint(date3));
48  printf(" date1-date2=date3: %s\n",string);
49  time_add(date3, date2, &date4);
50  strcpy(string,time_sprint(date4));
51  printf(" date3+date2=date4: %s\n",string);
52 } /* subaddtest */
53 
54 void head(routine)
55 char *routine;
56 {
57  printf("\nTEST %s\n\n",routine);
58 } /* head */
59 
60 void subhead(routine)
61 char *routine;
62 {
63  printf("\n**** %s\n\n",routine);
64 } /* subhead */
65 
66 void arout(Date)
67 time_Ts Date;
68 {
69  int i;
70  time_Tu *u;
71  u=(time_Tu *)&Date;
72  printf("date-array ");
73  for (i=0; i<7; i++) { printf("%6d ",u->array[i]); }
74  printf("\n");
75 } /* arout */
76 
77 void cmptest(date1, date2)
78 time_Ts date1;
79 time_Ts date2;
80 {
81  char string1[TIME_SLEN];
82  char string2[TIME_SLEN];
83  strcpy(string1, time_sprint(date1));
84  strcpy(string2, time_sprint(date2));
85  printf("%s ?? %s : %d\n",
86  string1,string2,time_compare(date1, date2));
87  printf("%s ?? %s : %d\n",
88  string2,string1,time_compare(date2, date1));
89 } /* cmptest */
90 
91 void divmultest(date1, n, date2)
92 time_Ts date1;
93 long int n;
94 time_Ts date2;
95 {
96  timeint nfit, rest;
97  time_Ts date3, date4, full;
98  char string1[TIME_SLEN];
99  char string2[TIME_SLEN];
100  char string3[TIME_SLEN];
101  char string4[TIME_SLEN];
102  char stringf[TIME_SLEN];
103  printf("****\n");
104  strcpy(string1,time_sprint(date1));
105  strcpy(string2,time_sprint(date2));
106  printf("date1 is %s\n",string1);
107  printf("date2 is %s\n",string2);
108  time_mul(date1, &date3, n);
109  strcpy(string3,time_sprint(date3));
110  printf("%s * %d -> %s\n",string1, n, string3);
111  time_nfit(date3, date1, &nfit, &full);
112  strcpy(stringf,time_sprint(full));
113  printf("fitting: %d to %s leads to %s\n",nfit,string3,stringf);
114  time_div(date3, &date4, n, &rest);
115  strcpy(string4,time_sprint(date4));
116  printf("%s / %d -> %s rest %d\n",string3,n,string4,rest);
117  time_add(date3, date2, &date4);
118  time_div(date4, &date3, n, &rest);
119  strcpy(string4,time_sprint(date4));
120  strcpy(string3,time_sprint(date3));
121  printf("%s / %d -> %s rest %d\n",string4,n,string3,rest);
122  time_nfit(date4, date1, &nfit, &full);
123  strcpy(stringf,time_sprint(full));
124  printf("fitting: %d to %s leads to %s\n",nfit,string4,stringf);
125  time_add(date4, date1, &date3);
126  strcpy(string3,time_sprint(date3));
127  time_nfit(date3, date1, &nfit, &full);
128  strcpy(stringf,time_sprint(full));
129  printf("fitting: %d to %s leads to %s\n",nfit,string3,stringf);
130 } /* divmultest */
131 
132 /*
133  * Main program
134  */
135 
136 int main()
137 {
138  time_Ts date1, date2;
139  timeint day, month, year;
140 
141  date1.year=97;
142  date1.doy=150;
143  date1.hour=12;
144  date1.minute=10;
145  date1.second=9;
146  date1.milsec=123;
147  date1.micsec=456;
148 
149  head("time_libversion");
150  printf("libversion: %f\n",time_libversion());
151 
152  head("time_isleapyear");
153  printf("leap 2000 %d\n",time_isleapyear(2000));
154  printf("leap 1996 %d\n",time_isleapyear(1996));
155  printf("leap 1997 %d\n",time_isleapyear(1997));
156  printf("leap 92 %d\n",time_isleapyear( 92));
157  printf("leap 0 %d\n",time_isleapyear( 0));
158  printf("leap 1900 %d\n",time_isleapyear(1900));
159 
160  head("time_fullyear");
161  year=0;
162  time_fullyear(&year);
163  printf("year 0: %d\n",year);
164  year=15;
165  time_fullyear(&year);
166  printf("year 15: %d\n",year);
167  year=97;
168  time_fullyear(&year);
169  printf("year 97: %d\n",year);
170  year=70;
171  time_fullyear(&year);
172  printf("year 70: %d\n",year);
173  year=100;
174  time_fullyear(&year);
175  printf("year 100: %d\n",year);
176  year=69;
177  time_fullyear(&year);
178  printf("year 69: %d\n",year);
179  year=99 ;
180  time_fullyear(&year);
181  printf("year 99: %d\n",year);
182  year=1831;
183  time_fullyear(&year);
184  printf("year 1831: %d\n",year);
185  year=2061;
186  time_fullyear(&year);
187  printf("year 2061: %d\n",year);
188  time_fullyear(&date1.year);
189  printf("full year: %s\n", time_sprint(date1));
190  date1.year=50;
191  time_fullyear(&date1.year);
192  printf("full year 50: %s\n", time_sprint(date1));
193 
194  head("time_sprint (time_getdate is implicit)");
195  date1.year=97;
196  date1.doy=150;
197  date1.hour=12;
198  date1.minute=10;
199  date1.second=9;
200  date1.milsec=123;
201  date1.micsec=456;
202  arout(date1);
203  printf("date: %s\n", time_sprint(date1));
204  date1.year=96;
205  printf("other year: %s\n", time_sprint(date1));
206  date1.doy=366;
207  printf("last doy (set by doy): %s\n", time_sprint(date1));
208  date1.year=0;
209  date1.doy=12345;
210  printf("relative time value: %s\n", time_sprint(date1));
211 
212  head("time_setdoy");
213  date1.year=97;
214  date1.doy=150;
215  date1.hour=12;
216  date1.minute=10;
217  date1.second=9;
218  date1.milsec=123;
219  date1.micsec=456;
220  time_setdoy(1, 1, &date1);
221  printf("jan first: %s\n", time_sprint(date1));
222  time_setdoy(31, 12, &date1);
223  printf("dec last: %s\n", time_sprint(date1));
224 
225  head("time_clear");
226  time_clear(&date1);
227  arout(date1);
228  printf("after clear: %s\n", time_sprint(date1));
229 
230  head("time_norm");
231  date1.year=96;
232  date1.doy=1;
233  date1.hour=24;
234  date1.minute=60;
235  date1.second=60;
236  date1.milsec=1000;
237  date1.micsec=1000;
238  arout(date1);
239  time_norm(&date1);
240  arout(date1);
241  printf("after norm: %s\n", time_sprint(date1));
242  date1.year=96;
243  date1.doy=1;
244  date1.hour=0;
245  date1.minute=0;
246  date1.second=0;
247  date1.milsec=0;
248  date1.micsec=-1;
249  arout(date1);
250  time_norm(&date1);
251  arout(date1);
252  printf("after norm: %s\n", time_sprint(date1));
253  date1.year=0;
254  date1.doy=5003;
255  date1.hour=24;
256  date1.minute=60;
257  date1.second=60;
258  date1.milsec=1000;
259  date1.micsec=1000;
260  arout(date1);
261  time_norm(&date1);
262  arout(date1);
263  printf("after norm: %s\n", time_sprint(date1));
264  date1.year=0;
265  date1.doy=123;
266  date1.hour=0;
267  date1.minute=0;
268  date1.second=0;
269  date1.milsec=0;
270  date1.micsec=-1;
271  arout(date1);
272  time_norm(&date1);
273  arout(date1);
274  printf("after norm: %s\n", time_sprint(date1));
275 
276  head("time_getdate after time_clear");
277  time_clear(&date1);
278  arout(date1);
279  time_getdate(&day, &month, date1);
280  printf("date after time_clear %d %d\n",day,month);
281 
282  head("time_add and time_sub");
283  date1.year=1997;
284  date1.doy=150;
285  date1.hour=12;
286  date1.minute=34;
287  date1.second=56;
288  date1.milsec=123;
289  date1.micsec=456;
290 
291  subhead("absolute & absolute");
292  time_copy(date1, &date2);
293  date2.year=1999;
294  subaddtest(date1, date2);
295  subaddtest(date2, date1);
296  time_copy(date1, &date2);
297  date2.hour=11;
298  date2.minute=33;
299  date2.second=55;
300  date2.milsec=122;
301  date2.micsec=455;
302  subaddtest(date2, date1);
303  date2.micsec=127;
304  subaddtest(date2, date1);
305 
306  subhead("absolute & relative");
307  time_clear(&date2);
308  date2.doy=12;
309  date2.hour=40;
310  date2.minute=40;
311  date2.second=20;
312  date2.milsec=20;
313  date2.micsec=20;
314  subaddtest(date2, date1);
315  subaddtest(date1, date2);
316 
317  subhead("relative & relative");
318  date1.year=0;
319  date1.doy=1;
320  date1.hour=11;
321  date1.minute=12;
322  date1.second=13;
323  date1.milsec=14;
324  date1.micsec=15;
325  subaddtest(date2, date1);
326  subaddtest(date1, date2);
327 
328  head("time_copy");
329  date1.year=1997;
330  date1.doy=150;
331  date1.hour=12;
332  date1.minute=34;
333  date1.second=56;
334  date1.milsec=123;
335  date1.micsec=456;
336  time_clear(&date2);
337  printf("date1: %s\n", time_sprint(date1));
338  printf("date2 after clear: %s\n", time_sprint(date2));
339  time_copy(date1, &date2);
340  printf("date2 after copy: %s\n", time_sprint(date2));
341 
342  head("time_finish");
343  date1.year=57;
344  date1.doy=450;
345  date1.hour=12;
346  date1.minute=0;
347  date1.second=4781;
348  date1.milsec=0;
349  date1.micsec=45456;
350  arout(date1);
351  time_finish(&date1);
352  arout(date1);
353 
354  head("time_compare");
355  date1.year=1997;
356  date1.doy=150;
357  date1.hour=12;
358  date1.minute=34;
359  date1.second=56;
360  date1.milsec=123;
361  date1.micsec=456;
362  time_copy(date1, &date2);
363  cmptest(date1, date2);
364  date1.doy=146;
365  cmptest(date1, date2);
366  date1.year=0;
367  date1.doy=5667;
368  time_copy(date1, &date2);
369  date2.minute=12;
370  date2.second=45;
371  cmptest(date1, date2);
372 
373  head("time_mul and time_div and time_nfit");
374  date1.year=0;
375  date1.doy=0;
376  date1.hour=0;
377  date1.minute=0;
378  date1.second=1;
379  date1.milsec=2;
380  date1.micsec=3;
381  time_copy(date1, &date2);
382  date1.second=0;
383  date1.milsec=1;
384  date1.micsec=500;
385  divmultest(date1, 5, date2);
386  divmultest(date1, 8567, date2);
387  divmultest(date1, 2658567, date2);
388 } /* main */
389 
390 
391 /* ----- END OF ctlibtime.c ----- */
integer time_isleapyear(timeint year)
void time_div(time_Ts Date1, time_Ts *Pdate2, timeint n, timeint *rest)
Definition: ctime_div.c:36
integer milsec
Definition: libtime.h:127
char * time_sprint(time_Ts Date)
Definition: ctime_sprint.c:39
integer minute
Definition: libtime.h:125
integer hour
Definition: libtime.h:124
integer array[7]
Definition: libtime.h:136
void head(char *routine)
Definition: ctlibtime.c:54
void divmultest(time_Ts date1, long int n, time_Ts date2)
Definition: ctlibtime.c:91
void time_add(time_Ts Date1, time_Ts Date2, time_Ts *Pdate3)
Definition: ctime_add.c:33
integer doy
Definition: libtime.h:123
void subhead(char *routine)
Definition: ctlibtime.c:60
void time_getdate(timeint *day, timeint *month, time_Ts Date)
Definition: ctime_getdate.c:36
void subaddtest(time_Ts date1, time_Ts date2)
Definition: ctlibtime.c:36
double time_libversion()
void time_setdoy(timeint day, timeint month, time_Ts *Pdate)
Definition: ctime_setdoy.c:36
void time_clear(time_Ts *Pdate)
Definition: ctime_clear.c:33
void time_copy(time_Ts Date1, time_Ts *Pdate2)
Definition: ctime_copy.c:33
void time_norm(time_Ts *Pdate)
Definition: ctime_norm.c:33
void time_fullyear(timeint *year)
integer second
Definition: libtime.h:126
int main()
Definition: ctlibtime.c:136
void time_sub(time_Ts Date1, time_Ts Date2, time_Ts *Pdate3)
Definition: ctime_sub.c:33
void arout(time_Ts Date)
Definition: ctlibtime.c:66
integer year
Definition: libtime.h:122
integer timeint
Definition: libtime.h:77
void time_nfit(time_Ts Date1, time_Ts Date2, timeint *n, time_Ts *Pfull)
Definition: ctime_nfit.c:36
void time_mul(time_Ts Date1, time_Ts *Pdate2, integer n)
Definition: ctime_mul.c:35
void time_finish(time_Ts *Pdate)
Definition: ctime_finish.c:33
integer micsec
Definition: libtime.h:128
integer time_compare(time_Ts Date1, time_Ts Date2)
Definition: ctime_compare.c:35
void cmptest(time_Ts date1, time_Ts date2)
Definition: ctlibtime.c:77
#define TIME_SLEN
Definition: libtime.h:113