libtime++: Date and time calculation
testtime++.cc
Go to the documentation of this file.
1 /* this is <testtime++.cc>
2  * ----------------------------------------------------------------------------
3  *
4  * 09/08/2000 by Thomas Forbriger (IfG Stuttgart)
5  *
6  * comprehensive test code for libtime C++ version
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  *
25  * REVISIONS and CHANGES
26  * 09/08/2000 V1.0 Thomas Forbriger
27  * 22/12/2000 V1.1 changed namespace time to libtime
28  * 13/01/2004 V1.2 test constructor for TRelativeTime from double
29  * 15/12/2012 V1.3 test formatted string
30  *
31  * ============================================================================
32  */
33 
34 #include"libtime++.h"
35 #include<iostream>
36 
37 using std::cout;
38 using std::endl;
39 using std::string;
40 
41 using namespace libtime;
42 
43 #define PRINTVALUE( V ) \
44  cout << #V << ":\n " << string(V) << endl;
45 
46 /*
47  * first of all declare some useful functions
48  */
49 template<class X>
50 void init_from_string(const std::string &Initializer)
51 {
52  X Instance(Initializer);
53  std::cout << Instance.timestring()
54  << " initialized from string \""
55  << Initializer << "\"" << endl;
56 }
57 
58 void Ainit_from_value(const long int &year,
59  const long int &month, const long int &day,
60  const long int &hour=0, const long int &minute=0,
61  const long int &second=0, const long int &milsec=0,
62  const long int &micsec=0)
63 {
64  libtime::TAbsoluteTime Instance(year,month,day,hour,minute,
65  second,milsec,micsec);
66  std::cout << Instance.timestring()
67  << " initialized from values: "
68  << year << " " << month << " " << day << " "
69  << hour << " " << minute << " " << second << " "
70  << milsec << " " << micsec << endl;
71 }
72 
73 void Rinit_from_value(const long int &days,
74  const long int &hour=0, const long int &minute=0,
75  const long int &second=0, const long int &milsec=0,
76  const long int &micsec=0)
77 {
78  libtime::TRelativeTime Instance(days,hour,minute,
79  second,milsec,micsec);
80  std::cout << Instance.timestring()
81  << " initialized from values: "
82  << days << " "
83  << hour << " " << minute << " " << second << " "
84  << milsec << " " << micsec << endl;
85 }
86 
87 void init_from_seconds(const double& seconds)
88 {
90  std::cout << value.timestring()
91  << " initialized from "
92  << seconds
93  << " seconds"
94  << endl;
95 }
96 
97 /*
98  * main testing code
99  */
100 
101 int main()
102 {
103  std::cout << "Hello world!\n";
104 
105  std::cout << "\nTesting constructors"
106  << "\n====================" << endl;
107 
108  std::cout << "\nTesting TAbsoluteTime"
109  << "\n---------------------" << endl;
110  init_from_string<libtime::TAbsoluteTime>("0/1/1");
111  init_from_string<libtime::TAbsoluteTime>("70/1/1");
112  init_from_string<libtime::TAbsoluteTime>("60/1/1");
113  init_from_string<libtime::TAbsoluteTime>("160/1/1");
114  init_from_string<libtime::TAbsoluteTime>("1978/1/1");
115  init_from_string<libtime::TAbsoluteTime>("1978/13/1");
116  init_from_string<libtime::TAbsoluteTime>("1978/1/2/3/4/5/6/7/8/9");
117 
118  std::cout << endl;
119  Ainit_from_value(2000,1,1);
120  Ainit_from_value(000,1,50);
121  Ainit_from_value(000,1,50,12,23,34,45,56);
122 
123  std::cout << "\nTesting TRelativeTime"
124  << "\n---------------------" << endl;
125  init_from_string<libtime::TRelativeTime>("0");
126  init_from_string<libtime::TRelativeTime>("8");
127  init_from_string<libtime::TRelativeTime>("123.23.34.45.56789");
128  init_from_string<libtime::TRelativeTime>("123.2345.34567.45678.56789");
129  init_from_seconds(1.e-6);
130  init_from_seconds(1.e-3);
131  init_from_seconds(1.);
132  init_from_seconds(60.);
133  init_from_seconds(3600.);
134  init_from_seconds(86400.);
135  init_from_seconds(123.345763);
136  init_from_seconds(871323.345763);
137 
138  std::cout << endl;
139  Rinit_from_value(0);
140  Rinit_from_value(2000,1,1);
141  Rinit_from_value(0,24,60,60,1000,1000);
142  Rinit_from_value(00,0,0,0,0,99999999L);
143 
144 // junk:
145  libtime::TRelativeTime Q(600,20,10,30,40,10);
146  libtime::TRelativeTime OneSecond(0,0,0,1);
147  libtime::TAbsoluteTime Now(2000,9,12,16,12);
148  libtime::TAbsoluteTime Then(2000,10,2,10,0);
149  cout << string(Q) << endl;
150  cout << string(OneSecond) << endl;
151  cout << string(Q%OneSecond) << endl;
152  long int numbo=Q/OneSecond;
153  cout << Q/OneSecond << endl;
154  cout << string(OneSecond*numbo) << endl;
155  numbo=(Now-Then)/OneSecond;
156  cout << numbo << " seconds from "
157  << string(Now) << endl << " to " << string(Then) << endl;
158  Q=numbo*OneSecond;
159  cout << "these are " << string(Q) << endl << " and lead to "
160  << string(Now+Q) << endl << " from " << string(Now) << endl;
161 
162  std::cout << "current time: " << libtime::now().timestring() << std::endl;
163  std::cout << "current time (UTC): " << libtime::utc().timestring() << std::endl;
164 
165  {
166  std::cout << "hierarchical strings:" << std::endl;
167  libtime::TAbsoluteTime Atime(2000,9,12,16,12,34,56,78);
168  libtime::TRelativeTime Rtime(514,16,12,34,56,78);
169  std::cout << Atime.timestring() << std::endl;
170  std::cout << Atime.hierarchicalstring() << std::endl;
171  std::cout << Rtime.timestring() << std::endl;
172  std::cout << Rtime.hierarchicalstring() << std::endl;
173  }
174 
175  std::cout << "\nSystematically test derived units"
176  << "\n---------------------------------" << endl;
177  PRINTVALUE( Days(25.5) );
178  PRINTVALUE( Hours(25.5) );
179  PRINTVALUE( Minutes(25.5) );
180  PRINTVALUE( Seconds(25.5) );
181  PRINTVALUE( Milliseconds(25.5) );
182  PRINTVALUE( Microseconds(25.5) );
183 
184  PRINTVALUE( Days(6) );
185  PRINTVALUE( Hours(6) );
186  PRINTVALUE( Minutes(6) );
187  PRINTVALUE( Seconds(6) );
188  PRINTVALUE( Milliseconds(6) );
189  PRINTVALUE( Microseconds(6) );
190 
191  PRINTVALUE( Days(180) );
192  PRINTVALUE( Hours(180) );
193  PRINTVALUE( Minutes(180) );
194  PRINTVALUE( Seconds(180) );
195  PRINTVALUE( Milliseconds(180) );
196  PRINTVALUE( Microseconds(180) );
197 
198  PRINTVALUE( Days(22,22,22,22,22,22) );
199  PRINTVALUE( Hours(22,22,22,22,22) );
200  PRINTVALUE( Minutes(22,22,22,22) );
201  PRINTVALUE( Seconds(22,22,22) );
202  PRINTVALUE( Milliseconds(22,22) );
203  PRINTVALUE( Microseconds(22) );
204 
205  PRINTVALUE( Hours(6) );
206  PRINTVALUE( 118*Days()+4*Hours()+10*Minutes()+Milliseconds(25.51) );
207  PRINTVALUE( Minutes(34.123456) );
208  PRINTVALUE( Minutes(34,123,456) );
209  PRINTVALUE( Seconds(34.123456) );
210  PRINTVALUE( Seconds(34,123,456) );
211  PRINTVALUE( Seconds(90072) );
212  PRINTVALUE( 2.5*Days() );
213  PRINTVALUE( Days(2.5) );
214  PRINTVALUE( Seconds(2.5*time2double(Days())) );
215 
217  PRINTVALUE( nowtime.timestring("%Y") );
218  PRINTVALUE( nowtime.timestring("/basedir/subdir/%Y/%m/%Y%m%d.dat") );
219  PRINTVALUE( libtime::now().timestring("%H:%M:%S") );
220  PRINTVALUE( libtime::now().timestring("time is %c") );
221  PRINTVALUE( libtime::utc().timestring("time is %c") );
222 }
223 
224 /* ----- END OF testtime++.cc ----- */
void init_from_string(const std::string &Initializer)
Definition: testtime++.cc:50
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
provide a convenient way to specify time intervals in the order of one microsecond ...
Definition: libtime++.h:598
TRelativeTime double2time(const double &seconds)
convert seconds to relative time
Definition: convert.cc:42
TAbsoluteTime utc()
return system time in UTC
Definition: now.cc:58
provide a convenient way to specify time intervals in the order of one day
Definition: libtime++.h:537
void Ainit_from_value(const long int &year, const long int &month, const long int &day, const long int &hour=0, const long int &minute=0, const long int &second=0, const long int &milsec=0, const long int &micsec=0)
Definition: testtime++.cc:58
class to contain relative times
Definition: libtime++.h:201
std::string timestring() const
Definition: libtime++.h:267
provide a convenient way to specify time intervals in the order of one minute
Definition: libtime++.h:563
std::string hierarchicalstring() const
std::string timestring(const std::string &format) const
return string representation of time.
provide a convenient way to specify time intervals in the order of one millisecond ...
Definition: libtime++.h:587
void Rinit_from_value(const long int &days, const long int &hour=0, const long int &minute=0, const long int &second=0, const long int &milsec=0, const long int &micsec=0)
Definition: testtime++.cc:73
TAbsoluteTime now()
return system time
Definition: now.cc:44
provide a convenient way to specify time intervals in the order of one second
Definition: libtime++.h:575
int main()
Definition: testtime++.cc:101
class to contain absolute times
Definition: libtime++.h:149
void init_from_seconds(const double &seconds)
Definition: testtime++.cc:87
#define PRINTVALUE(V)
Definition: testtime++.cc:43
std::string hierarchicalstring() const