libtime++: Date and time calculation
libtime.h
Go to the documentation of this file.
1 /* this is <libtime.h>
2  * ----------------------------------------------------------------------------
3  *
4  * 12/08/97 by Thomas Forbriger (IfG Stuttgart)
5  *
6  * some definitions and prototypes for the C libtime interface
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  * 06/08/00 V2.0 do not include f2c - rather copy relevant definitions
27  * added C linkage convention for C++
28  * use namespace time_kernel
29  * 14/12/07 V2.1 default integer type is different for g77 on 64bit
30  * machines; distinguish explicitely
31  * discarded farray element in union time_Tu
32  * 17/12/07 V2.2 introduced typedef timeint
33  * 13/11/10 V2.3 abort, if f2c.h is read for 64bit CPU compilation
34  *
35  * ============================================================================
36  */
37 
38 #ifndef _TF_LIBTIME_H
39 #define _TF_LIBTIME_H
40 
41 /* #include <f2c.h> */
42 /* #include <stdio.h> */
43 
44 #ifdef __cplusplus
45 namespace time_kernel {
46 extern "C" {
47 #endif
48 
49 /*
50  * all f2c stuff that is needed here
51  * =================================
52  */
53 
54 #ifndef F2C_INCLUDE
55 /* FORTRAN (f2c) types needed by the wrapper functions */
56 
57 /* g77 on 64bit system apparently doesn't use long int */
58 #ifdef __x86_64
59 typedef int integer;
60 typedef double doublereal;
61 typedef int logical;
62 typedef int ftnlen;
63 #else
64 typedef long int integer;
65 typedef double doublereal;
66 typedef long int logical;
67 typedef long int ftnlen;
68 #endif
69 #else
70 #warning "f2c.h is read from somewhere else!"
71 #ifdef __x86_64
72 #error "long int integer type in f2c.h does not work for x86_64 compilation!"
73 #endif
74 #endif
75 
76 /* type of integer that we use externally */
77 typedef integer timeint;
78 
79 /*
80  * a few tf-macros needed by the C specific functions
81  * ==================================================
82  */
83 
84 /* return value of time_read on success */
85 #ifndef EXIT_SUCCESS
86 #define EXIT_SUCCESS 0
87 #endif
88 
89 /* return value of time_read on failure */
90 #ifndef EXIT_FAILURE
91 #define EXIT_FAILURE 1
92 #endif
93 
94 /* error handling macro used within time_read */
95 #ifndef RETURNERROR
96 #define RETURNERROR( EXPR , SUB, STR, CODE )\
97  if ( EXPR ) { fprintf(stderr, "ERROR (%s):\n %s\n", SUB, STR );\
98  return(CODE); }
99 #endif
100 
101 /*S*/
102 /*
103  * some macro constants
104  * ====================
105  */
106 
107 /* value returned by time_isleapyear in case year IS a leap-year */
108 #define TIME_ISLEAP (1)
109 /* value returned by time_isleapyear in case year IS NOT a leap-year */
110 #define TIME_ISNOTLEAP (0)
111 
112 /* length of static string buffer in time_sprint */
113 #define TIME_SLEN (35)
114 
115 /*
116  * time data structure
117  * ===================
118  */
119 
120 /* standard structure to hold date record */
121 typedef struct {
122  integer year; /* year (=0 for relative times) */
123  integer doy; /* day within yaer (may be 0 for relative times */
124  integer hour; /* hour within day */
125  integer minute; /* minute within hour */
126  integer second; /* second within minute */
127  integer milsec; /* millisecond within second */
128  integer micsec; /* microsecond within millisecond */
129 } time_Ts;
130 
131 /*E*/
132 
133 /* this unios is used to convert date records between different
134  * representations and language specific functions */
135 typedef union {
136  integer array[7]; /* linear access in C */
137  time_Ts s; /* the structure that is fed in */
138 } time_Tu;
139 
140 /*S*/
141 /*
142  * wrapper function prototypes
143  * ===========================
144  */
145 
146 /* void time_add(time_Ts date1, time_Ts date2, time_Ts *date3)
147  *
148  * date1: input: any date record
149  * date2: input: date record (may be absolute if date1 is relative)
150  * date3: output: sum of date1 and date2
151  */
152 void time_add(time_Ts, time_Ts, time_Ts *);
153 
154 /* void time_clear(time_Ts *date)
155  *
156  * date: input: any date record
157  * output: zero relative time
158  */
159 void time_clear(time_Ts *);
160 
161 /* long int time_compare(time_Ts date1, time_Ts date2)
162  *
163  * date1: input: any date record
164  * date2: input: any date record (must be relative is date1 is relative,
165  * must be absolute if date1 is absolute)
166  * returns: 1 if date1 > date2
167  * 0 if date1 == date2
168  * -1 if date1 < date2
169  * -2 when mixing relative and absolute date records
170  */
172 
173 /* void time_copy(time_Ts date1, time_Ts *date2)
174  *
175  * date1: input: any date record
176  * date2: output: copy of date1
177  */
178 void time_copy(time_Ts, time_Ts *);
179 
180 /* void time_div(time_Ts date1, time_Ts *date2, long int n, long int *rest)
181  *
182  * date1: input: any relative time
183  * date2: output: n-th fraction of date1
184  * n: input: divisor for date1
185  * rest: output: rest of division in mircoseconds
186  * always: date1 >= (n*date2)
187  */
188 void time_div(time_Ts, time_Ts *, timeint, timeint *);
189 
190 /* void time_finish(time_Ts *date)
191  *
192  * date: input: any date record
193  * output: fully qualified and regularized date record
194  */
195 void time_finish(time_Ts *);
196 
197 /* void time_fullyear(long int *year)
198  *
199  * year: ainput: ny year value (may be a 2-digit abbreviation)
200  * output: a full qualified year value
201  */
202 void time_fullyear(timeint *);
203 
204 /* void time_getdate(long int *day, long int *month, time_Ts date)
205  *
206  * day: output: day within month index of date
207  * month: output: month wihtin year index of date
208  * date: input: any absolute date record
209  */
211 
212 /* long int time_isleapyear(long int year)
213  *
214  * year: input: full qualified year value to be checked
215  * returns: TIME_ISLEAP if argument is a leap-year
216  * TIME_ISNOLEAP if argument is not a leap-year
217  */
219 
220 /* double time_libversion
221  *
222  * returns: version number of library kernel
223  */
224 double time_libversion();
225 
226 /* void time_mul(time_Ts date1, time_Ts *date2, long int n)
227  *
228  * date1: input: any relative date record
229  * date2: output: n times date1
230  * n: input: factor to multiply date1 with
231  */
232 void time_mul(time_Ts, time_Ts *, timeint);
233 
234 /* void time_nfit(time_Ts date1, time_Ts date2, long int *n, time_Ts *full)
235  *
236  * date1: input: any relative time record
237  * date2: input: any relative time record
238  * n: output: number os date2 intervals the fit best into date1
239  * so that abs((n*date2)-date1) <= date2/2
240  * full: output: full time span defined by n and date2 (full=n*date2)
241  */
243 
244 /* void time_norm(time_Ts *date)
245  *
246  * date: input: any date record
247  * output: regularized date record
248  */
249 void time_norm(time_Ts *);
250 
251 /* void time_setdoy(long int day, long int month, time_Ts *date)
252  *
253  * day: input: day index within month
254  * month: input: month index within year
255  * date: input: any date record with year set
256  * output: has correct doy set from day and month
257  */
259 
260 /* void time_sub(time_Ts date1, time_Ts date2, time_Ts date3)
261  *
262  * date1: input: any date record
263  * date2: input: any date record
264  * date3: output: absolute (positive) difference between date1 and date2
265  */
266 void time_sub(time_Ts, time_Ts, time_Ts *);
267 
268 /*
269  * prototypes of pure C functions
270  * ==============================
271  */
272 
273 /* int time_read(time_Ts *date, const char *string)
274  *
275  * string: input: character representation of a time with the fields in the
276  * following order:
277  * year month day hour minute seconds
278  * - the fields must be separated by non-numeric characters
279  * - all fields except the field 'seconds' must be integer
280  * - you may omit any number of trailing fields
281  * - year AND month must be zero to specify a relative time
282  * date: output: full qualified and regularized date record specified by
283  * string
284  * returns: EXIT_SUCCESS on success
285  * EXIT_FAILURE on FAILURE
286  *
287  * NOTICE: time_read is not the direct inverse operation of time_sprint
288  */
289 int time_read(time_Ts *, const char *);
290 
291 /* char *time_sprint(time_Ts date)
292  *
293  * date: input: any date record
294  * returns: a pointer to a static character string of at most TIME_SLEN
295  * characters length containing the ASCII text representation
296  * of date (NOTICE: the next call to time_sprint will overwrite
297  * the static character string)
298  */
299 char *time_sprint(time_Ts);
300 
301 /*E*/
302 
303 #ifdef __cplusplus
304 }}
305 #endif
306 
307 #endif /* _TF_LIBTIME_H */
308 
309 /* ----- END OF libtime.h ----- */
int time_read(time_Ts *, const char *)
Definition: ctime_read.c:37
integer milsec
Definition: libtime.h:127
integer minute
Definition: libtime.h:125
void time_fullyear(timeint *)
integer hour
Definition: libtime.h:124
integer time_compare(time_Ts, time_Ts)
Definition: ctime_compare.c:35
long int logical
Definition: libtime.h:66
integer time_isleapyear(timeint)
double time_libversion()
void time_finish(time_Ts *)
Definition: ctime_finish.c:33
integer doy
Definition: libtime.h:123
void time_copy(time_Ts, time_Ts *)
Definition: ctime_copy.c:33
void time_clear(time_Ts *)
Definition: ctime_clear.c:33
void time_setdoy(timeint, timeint, time_Ts *)
Definition: ctime_setdoy.c:36
void time_norm(time_Ts *)
Definition: ctime_norm.c:33
char * time_sprint(time_Ts)
Definition: ctime_sprint.c:39
void time_mul(time_Ts, time_Ts *, timeint)
Definition: ctime_mul.c:35
double doublereal
Definition: libtime.h:65
integer second
Definition: libtime.h:126
time_Ts s
Definition: libtime.h:137
long int integer
Definition: libtime.h:64
void time_getdate(timeint *, timeint *, time_Ts)
Definition: ctime_getdate.c:36
integer year
Definition: libtime.h:122
void time_div(time_Ts, time_Ts *, timeint, timeint *)
Definition: ctime_div.c:36
integer timeint
Definition: libtime.h:77
void time_nfit(time_Ts, time_Ts, timeint *, time_Ts *)
Definition: ctime_nfit.c:36
long int ftnlen
Definition: libtime.h:67
void time_sub(time_Ts, time_Ts, time_Ts *)
Definition: ctime_sub.c:33
integer micsec
Definition: libtime.h:128
void time_add(time_Ts, time_Ts, time_Ts *)
Definition: ctime_add.c:33