libtime++: Date and time calculation
example++.cc
Go to the documentation of this file.
1 /* this is <example++.cc>
2  * ----------------------------------------------------------------------------
3  *
4  * Copyright (c) 2000 by Thomas Forbriger (IMGF Frankfurt)
5  *
6  * example for libtime C++ code
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  * 22/12/2000 V1.0 Thomas Forbriger
26  *
27  * ============================================================================
28  */
29 
30 #include"libtime++.h"
31 #include<iostream>
32 
33 using std::cout;
34 using std::endl;
35 
36 using namespace libtime;
37 
38 #define PRINTVALUE( V ) \
39  cout << #V << ":\n " << string(V) << endl;
40 
41 /*----------------------------------------------------------------------*/
42 
43 void example1()
44 {
45  cout << "\n"
46  << "Example 1: How many seconds are there within a day?\n"
47  << "---------­\n"
48  << "\n"
49  << " First we create a relative time span of one day length:\n";
50 
51  libtime::TRelativeTime oneday(1);
52  cout << " " << string(oneday) << endl;
53 
54  cout << "\n"
55  << " Then we create a relative time span of one second length:\n";
56 
57  libtime::TRelativeTime onesecond(0,0,0,1);
58  cout << " " << string(onesecond) << endl;
59 
60  cout << "\n"
61  << " Then we divide the one by the other:\n"
62  << " " << oneday/onesecond << endl;
63 
64  cout << " So there are " << oneday/onesecond
65  << " seconds within one day.\n"
66  << " Too easy?\n";
67 }
68 
69 /*----------------------------------------------------------------------*/
70 
71 void example2()
72 {
73  libtime::TRelativeTime timespan;
74  int nsamples(1300);
75 
76  cout << "\n"
77  << "Example 2: Which is the time of the last sample?\n"
78  << "----------\n"
79  << "\n";
80 
81  cout << " We have a time series that starts with a first sample at:\n";
82  std::string startstring("2000/12/20 22:34:53.241");
83  cout << " " << startstring << endl;
84 
85  cout << "\n"
86  << " We create an absolute time for the first sample:\n";
87  libtime::TAbsoluteTime starttime(startstring);
88  cout << " " << string(starttime) << endl;
89 
90  cout << "\n"
91  << " The sampling interval of the time series is:\n";
92  libtime::TRelativeTime interval(0,0,0,10);
93  cout << " " << string(interval) << endl;
94 
95  cout << "\n"
96  << " The total series has " << nsamples << " samples"
97  << " with " << nsamples-1 << " intervals.\n"
98  << " That is a time span of:\n";
99  timespan=interval*(nsamples-1);
100  cout << " " << string(timespan) << endl;
101 
102  cout << "\n"
103  << " Hence the last sample was taken at:\n"
104  << " " << string(starttime+timespan) << endl;
105 
106  cout << " You'd like a more advanced one?\n";
107 
108 }
109 
110 /*----------------------------------------------------------------------*/
111 
112 void example3sub1(std::string s1, std::string s2,
115 {
116  cout << "\n"
117  << " So " << s1 << " is ";
118  t1 < t2 ? cout << "earlier" : cout << "later";
119  cout << endl;
120  cout << " than " << s2 << "." << endl;;
121  cout << " The time span between them is " << std::string(t2-t1) << endl;
122  cout << " It is ";
123  t2-t1 < i/2 ? cout << "smaller" : cout << "larger";
124  cout << " than half a sampling interval.\n";
125 }
126 
127 void example3()
128 {
129  libtime::TAbsoluteTime first("2000/12/8 9:35:1.2671"),
130  reqstart("2000/12/8 12:37:14"),
131  reqend("2000/12/8 12:43:52"),
132  exstart, exend;
133  libtime::TRelativeTime interval(0,0,0,0,5), reqspan, exspan;
134 
135  cout << "\n"
136  << "Example 3: Serving a data request\n"
137  << "----------\n"
138  << "\n";
139 
140  cout << " Imagine that you might have a database file that starts\n"
141  << " with a first sample at "
142  << std::string(first) << ".\n"
143  << " A client requests a time series that should start at\n"
144  << " " << std::string(reqstart) << " and end at "
145  << " " << std::string(reqend) << ".\n"
146  << " The sampling interval is " << std::string(interval) << endl;
147 
148  long int offset1=(reqstart-first)/interval;
149  long int offset2=(reqend-first)/interval;
150  exstart=first+interval*(offset1);
151  exend=first+interval*(offset2);
152  reqspan=reqend-reqstart;
153  exspan=exend-exstart;
154 
155  cout << " The requested time span is "
156  << std::string(reqspan) << endl;
157 
158  cout << "\n"
159  << " The client will receive a data segment from offset "
160  << offset1 << " to " << offset2 << ".\n";
161 
162  cout << " The segment's first sample is at " << std::string(exstart) << "\n"
163  << " and it's last one is at " << std::string(exend) << endl;
164 
165  example3sub1("the first requested sample",
166  "the first delivered sample",
167  reqstart, exstart, interval);
168 
169  example3sub1("the last requested sample",
170  "the last delivered sample",
171  reqend, exend, interval);
172 
173  example3sub1("the first requested sample",
174  "the sample before the first delivered one",
175  reqstart, exstart-interval, interval);
176 
177  example3sub1("the first requested sample",
178  "the sample after the first delivered one",
179  reqstart, exstart+interval, interval);
180 
181 }
182 
183 /*----------------------------------------------------------------------*/
184 
185 void example4()
186 {
187  libtime::TAbsoluteTime first, second;
188 
189  cout << "\n"
190  << "Example 4: How about leap-years?\n"
191  << "----------\n"
192  << "\n";
193 
194  first="1979/02/15"; second="1979/03/01";
195  cout << "\n"
196  << " The time span between " << string(first) << endl
197  << " and " << string(second) << endl
198  << " is " << string(second-first) << endl;
199 
200  first="1980/02/15"; second="1980/03/01";
201  cout << "\n"
202  << " The time span between " << string(first) << endl
203  << " and " << string(second) << endl
204  << " is " << string(second-first) << endl;
205 
206  first="2000/02/15"; second="2000/03/01";
207  cout << "\n"
208  << " The time span between " << string(first) << endl
209  << " and " << string(second) << endl
210  << " is " << string(second-first) << endl;
211 
212  first="2100/02/15"; second="2100/03/01";
213  cout << "\n"
214  << " The time span between " << string(first) << endl
215  << " and " << string(second) << endl
216  << " is " << string(second-first) << endl;
217 }
218 
219 /*----------------------------------------------------------------------*/
220 
221 void example5()
222 {
223  libtime::TAbsoluteTime exampletime;
224 
225  cout << "\n"
226  << "Example 5: What is a spurious year value?\n"
227  << "----------\n"
228  << "\n";
229 
230  cout << " If you set an absolute time to a year for which I do not\n"
231  << " expect any digital seismological data to be available, I\n"
232  << " will regard this as a spurious year value. However any\n"
233  << " calculations with such values will lead to the expected\n"
234  << " results apart from the annoying warning message.\n";
235 
236  cout << "\n"
237  << " I do not expect digital data for years before 1970:\n";
238 
239  exampletime="1972/1/1";
240  exampletime="1971/1/1";
241  exampletime="1970/1/1";
242  exampletime="1969/1/1";
243  exampletime="1968/1/1";
244 
245 }
246 
247 /*----------------------------------------------------------------------*/
248 
249 void example6sub(std::string exampledate)
250 {
251  cout << " " << exampledate << " means "
252  << std::string(libtime::TAbsoluteTime(exampledate)) << endl;
253 }
254 
255 void example6()
256 {
257  libtime::TAbsoluteTime exampletime;
258 
259  cout << "\n"
260  << "Example 6: How about year value abbreviation?\n"
261  << "----------\n"
262  << "\n";
263 
264  cout << " You may abbreviate year values by two digits. The year\n"
265  << " will be automatically expanded to a full qualified value.\n"
266  << " The initial date is "
267  << std::string(libtime::TAbsoluteTime("1970/1/1")) << ":" << endl;
268 
269  example6sub("72/1/1");
270  example6sub("71/1/1");
271  example6sub("70/1/1");
272  example6sub("69/1/1");
273  example6sub("68/1/1");
274 
275  cout << "\n"
276  << " This means that you can not set the year to values below 100:\n";
277  example6sub("101/1/1");
278  example6sub("100/1/1");
279  example6sub("99/1/1");
280  example6sub("0/1/1");
281 
282 }
283 
284 /*----------------------------------------------------------------------*/
285 
286 void example7()
287 {
288  cout << "\n"
289  << "Example 7: Convenient ways to specify times\n"
290  << "----------\n"
291  << "\n";
292 
293  PRINTVALUE( Hours(6) );
294  PRINTVALUE( 118*Days()+4*Hours()+10*Minutes() );
295  PRINTVALUE( Minutes(34.123456) );
296  PRINTVALUE( Minutes(34,123,456) );
297  PRINTVALUE( Seconds(34.123456) );
298  PRINTVALUE( Seconds(34,123,456) );
299  PRINTVALUE( Seconds(90072) );
300  PRINTVALUE( 2.5*Days() );
301  PRINTVALUE( Days(2.5) );
302  PRINTVALUE( Seconds(2.5*time2double(Days())) );
303 }
304 
305 /*----------------------------------------------------------------------*/
306 
307 int main()
308 {
309  cout << "Example++\n\n"
310  << "This is an example program for the libtime++.a library.\n";
311 
312  cout << "\n"
313  << "Time comes in two different flavours:\n"
314  << "1. absolute time (like today at 10 o'clock)\n"
315  << "2. relative times (like the time span between now and christmas)\n"
316  << "The following examples will show how to use and combine these\n"
317  << "two flavours.\n";
318 
319  example1();
320  example2();
321  example3();
322  example4();
323  example5();
324  example6();
325  example7();
326 
327  cout << "\n"
328  << "That's it... any questions?\n";
329 }
330 
331 /* ----- END OF example++.cc ----- */
int main()
Definition: example++.cc:307
double time2double(const TRelativeTime &rtime)
convert relative time to seconds
Definition: convert.cc:59
provide a convenient way to specify time intervals in the order of one hour
Definition: libtime++.h:550
void example4()
Definition: example++.cc:185
void example1()
Definition: example++.cc:43
provide a convenient way to specify time intervals in the order of one day
Definition: libtime++.h:537
class to contain relative times
Definition: libtime++.h:201
#define PRINTVALUE(V)
Definition: example++.cc:38
void example6sub(std::string exampledate)
Definition: example++.cc:249
void example2()
Definition: example++.cc:71
provide a convenient way to specify time intervals in the order of one minute
Definition: libtime++.h:563
void example5()
Definition: example++.cc:221
provide a convenient way to specify time intervals in the order of one second
Definition: libtime++.h:575
void example6()
Definition: example++.cc:255
class to contain absolute times
Definition: libtime++.h:149
void example3()
Definition: example++.cc:127
void example7()
Definition: example++.cc:286
void example3sub1(std::string s1, std::string s2, libtime::TAbsoluteTime t1, libtime::TAbsoluteTime t2, libtime::TRelativeTime i)
Definition: example++.cc:112