example++.cc 9.97 KB
Newer Older
thomas.forbriger's avatar
thomas.forbriger committed
1
2
3
/* this is <example++.cc>
 * ----------------------------------------------------------------------------
 *
thomas.forbriger's avatar
thomas.forbriger committed
4
 * $Id$
thomas.forbriger's avatar
thomas.forbriger committed
5
6
7
8
9
 *
 * Copyright (c) 2000 by Thomas Forbriger (IMGF Frankfurt)
 *
 * example for libtime C++ code
 *
thomas.forbriger's avatar
thomas.forbriger committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 * ----
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * ----
 *
thomas.forbriger's avatar
thomas.forbriger committed
26
27
28
29
30
31
32
33
34
 * REVISIONS and CHANGES
 *    22/12/2000   V1.0   Thomas Forbriger
 *
 * ============================================================================
 */
 
#include"libtime++.h"
#include<iostream>

35
36
37
using std::cout;
using std::endl;

38
39
40
41
42
using namespace libtime;

#define PRINTVALUE( V ) \
  cout << #V << ":\n  " << string(V) << endl;

thomas.forbriger's avatar
thomas.forbriger committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*----------------------------------------------------------------------*/

void example1()
{
  cout << "\n"
       << "Example 1: How many seconds are there within a day?\n"
       << "---------­\n"
       << "\n"
       << "  First we create a relative time span of one day length:\n";

  libtime::TRelativeTime oneday(1);
  cout << "    " << string(oneday) << endl;

  cout << "\n"
       << "  Then we create a relative time span of one second length:\n";

  libtime::TRelativeTime onesecond(0,0,0,1);
  cout << "    " << string(onesecond) << endl;

  cout << "\n"
thomas.forbriger's avatar
thomas.forbriger committed
63
       << "  Then we divide the one by the other:\n"
thomas.forbriger's avatar
thomas.forbriger committed
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
       << "    " << oneday/onesecond << endl;

  cout << "  So there are " << oneday/onesecond 
                            << " seconds within one day.\n"
       << "  Too easy?\n";
}

/*----------------------------------------------------------------------*/

void example2()
{
  libtime::TRelativeTime timespan;
  int nsamples(1300);

  cout << "\n"
       << "Example 2: Which is the time of the last sample?\n"
       << "----------\n"
       << "\n";

  cout << "  We have a time series that starts with a first sample at:\n";
  std::string startstring("2000/12/20 22:34:53.241");
  cout << "  " << startstring << endl;

  cout << "\n"
       << "  We create an absolute time for the first sample:\n";
  libtime::TAbsoluteTime starttime(startstring);
  cout << "  " << string(starttime) << endl;

  cout << "\n"
       << "  The sampling interval of the time series is:\n";
  libtime::TRelativeTime interval(0,0,0,10);
  cout << "  " << string(interval) << endl;

  cout << "\n"
       << "  The total series has " << nsamples << " samples"
               << " with " << nsamples-1 << " intervals.\n"
       << "  That is a time span of:\n";
  timespan=interval*(nsamples-1);
  cout << "  " << string(timespan) << endl;

  cout << "\n"
       << "  Hence the last sample was taken at:\n"
       << "  " << string(starttime+timespan) << endl;

  cout << "  You'd like a more advanced one?\n";

}

/*----------------------------------------------------------------------*/

void example3sub1(std::string s1, std::string s2,
                  libtime::TAbsoluteTime t1, libtime::TAbsoluteTime t2,
                  libtime::TRelativeTime i)
{
  cout << "\n"
       << "  So " << s1 << " is ";
  t1 < t2 ? cout << "earlier" : cout << "later";
  cout << endl;
  cout << "  than " << s2 << "." << endl;;
  cout << "  The time span between them is " << std::string(t2-t1) << endl;
  cout << "  It is ";
  t2-t1 < i/2 ? cout << "smaller" : cout << "larger";
  cout << " than half a sampling interval.\n";
}

void example3()
{
  libtime::TAbsoluteTime first("2000/12/8 9:35:1.2671"),
                      reqstart("2000/12/8 12:37:14"),
                        reqend("2000/12/8 12:43:52"),
                        exstart, exend;
  libtime::TRelativeTime interval(0,0,0,0,5), reqspan, exspan;

  cout << "\n"
       << "Example 3: Serving a data request\n"
       << "----------\n"
       << "\n";

  cout << "  Imagine that you might have a database file that starts\n"
       << "  with a first sample at "
       << std::string(first) << ".\n"
       << "  A client requests a time series that should start at\n"
       << "  " << std::string(reqstart) << " and end at "
       << "  " << std::string(reqend) << ".\n"
       << "  The sampling interval is " << std::string(interval) << endl;

  long int offset1=(reqstart-first)/interval;
  long int offset2=(reqend-first)/interval;
  exstart=first+interval*(offset1);
  exend=first+interval*(offset2);
  reqspan=reqend-reqstart;
  exspan=exend-exstart;
  
  cout << "  The requested time span is "
       << std::string(reqspan) << endl;

  cout << "\n"
       << "  The client will receive a data segment from offset "
       << offset1 << " to " << offset2 << ".\n";

thomas.forbriger's avatar
thomas.forbriger committed
164
165
  cout << "  The segment's first sample is at " << std::string(exstart) << "\n"
       << "  and it's last one is at " << std::string(exend) << endl;
thomas.forbriger's avatar
thomas.forbriger committed
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

  example3sub1("the first requested sample",
               "the first delivered sample",
               reqstart, exstart, interval);

  example3sub1("the last requested sample",
               "the last delivered sample",
               reqend, exend, interval);

  example3sub1("the first requested sample",
               "the sample before the first delivered one",
               reqstart, exstart-interval, interval);

  example3sub1("the first requested sample",
               "the sample after the first delivered one",
               reqstart, exstart+interval, interval);

}

/*----------------------------------------------------------------------*/

void example4()
{
  libtime::TAbsoluteTime first, second;

  cout << "\n"
       << "Example 4: How about leap-years?\n"
       << "----------\n"
       << "\n";

  first="1979/02/15"; second="1979/03/01";
  cout << "\n"
       << "  The time span between " << string(first) << endl
       << "                    and " << string(second) << endl
       << "                     is " << string(second-first) << endl;

  first="1980/02/15"; second="1980/03/01";
  cout << "\n"
       << "  The time span between " << string(first) << endl
       << "                    and " << string(second) << endl
       << "                     is " << string(second-first) << endl;

  first="2000/02/15"; second="2000/03/01";
  cout << "\n"
       << "  The time span between " << string(first) << endl
       << "                    and " << string(second) << endl
       << "                     is " << string(second-first) << endl;

  first="2100/02/15"; second="2100/03/01";
  cout << "\n"
       << "  The time span between " << string(first) << endl
       << "                    and " << string(second) << endl
       << "                     is " << string(second-first) << endl;
}

/*----------------------------------------------------------------------*/

void example5()
{
  libtime::TAbsoluteTime exampletime;

  cout << "\n"
       << "Example 5: What is a spurious year value?\n"
       << "----------\n"
       << "\n";

  cout << "  If you set an absolute time to a year for which I do not\n"
       << "  expect any digital seismological data to be available, I\n"
thomas.forbriger's avatar
thomas.forbriger committed
234
235
       << "  will regard this as a spurious year value. However any\n"
       << "  calculations with such values will lead to the expected\n"
thomas.forbriger's avatar
thomas.forbriger committed
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
       << "  results apart from the annoying warning message.\n";

  cout << "\n"
       << "  I do not expect digital data for years before 1970:\n";

  exampletime="1972/1/1";
  exampletime="1971/1/1";
  exampletime="1970/1/1";
  exampletime="1969/1/1";
  exampletime="1968/1/1";

}

/*----------------------------------------------------------------------*/

void example6sub(std::string exampledate)
{
  cout << "  " << exampledate << " means " 
       << std::string(libtime::TAbsoluteTime(exampledate)) << endl;
}

void example6()
{
  libtime::TAbsoluteTime exampletime;

  cout << "\n"
262
       << "Example 6: How about year value abbreviation?\n"
thomas.forbriger's avatar
thomas.forbriger committed
263
264
265
266
       << "----------\n"
       << "\n";

  cout << "  You may abbreviate year values by two digits. The year\n"
thomas.forbriger's avatar
thomas.forbriger committed
267
       << "  will be automatically expanded to a full qualified value.\n"
thomas.forbriger's avatar
thomas.forbriger committed
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
       << "  The initial date is "
       << std::string(libtime::TAbsoluteTime("1970/1/1")) << ":" << endl;

  example6sub("72/1/1");
  example6sub("71/1/1");
  example6sub("70/1/1");
  example6sub("69/1/1");
  example6sub("68/1/1");

  cout << "\n"
       << "  This means that you can not set the year to values below 100:\n";
  example6sub("101/1/1");
  example6sub("100/1/1");
  example6sub("99/1/1");
  example6sub("0/1/1");

}

/*----------------------------------------------------------------------*/

288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
void example7()
{
  cout << "\n"
       << "Example 7: Convenient ways to specify times\n"
       << "----------\n"
       << "\n";

  PRINTVALUE( Hours(6) );
  PRINTVALUE( 118*Days()+4*Hours()+10*Minutes() );
  PRINTVALUE( Minutes(34.123456) );
  PRINTVALUE( Minutes(34,123,456) );
  PRINTVALUE( Seconds(34.123456) );
  PRINTVALUE( Seconds(34,123,456) );
  PRINTVALUE( Seconds(90072) );
  PRINTVALUE( 2.5*Days() );
  PRINTVALUE( Days(2.5) );
  PRINTVALUE( Seconds(2.5*time2double(Days())) );
}

/*----------------------------------------------------------------------*/

thomas.forbriger's avatar
thomas.forbriger committed
309
310
int main()
{
thomas.forbriger's avatar
thomas.forbriger committed
311
  cout << "Example++ ($Id$)\n\n"
thomas.forbriger's avatar
thomas.forbriger committed
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
       << "This is an example program for the libtime++.a library.\n";

  cout << "\n"
       << "Time comes in two different flavours:\n"
       << "1. absolute time (like today at 10 o'clock)\n"
       << "2. relative times (like the time span between now and christmas)\n"
       << "The following examples will show how to use and combine these\n"
       << "two flavours.\n";

  example1();
  example2();
  example3();
  example4();
  example5();
  example6();
327
  example7();
thomas.forbriger's avatar
thomas.forbriger committed
328
329
330
331
332
333

  cout << "\n"
       << "That's it... any questions?\n";
}
 
/* ----- END OF example++.cc ----- */