source: trunk/FACT++/src/time.cc@ 10252

Last change on this file since 10252 was 10251, checked in by tbretz, 10 years ago
Added some example how to calculate with times.
File size: 3.1 KB
Line 
1#include "Time.h"
2
3#include <iostream>
4
5using namespace std;
6using namespace boost::posix_time;
7
8int main(int, char **)
9{
10 // Print the local time in the default representation of cout
11 cout << endl;
12 cout << "Local Time: " << Time(Time::local) << endl;
13
14 // Print UTC in several different representations
15 Time utc;
16 cout << "Universal CT: " << utc << endl;
17 cout << "User defined: " << Time::fmt("%Y=%m=%d %H=%M=%S.%f") << utc << endl;
18 cout << "SQL-format: " << Time::sql << utc << endl;
19 cout << "ISO-format: " << Time::iso << utc << endl;
20 cout << "Default: " << Time::reset << utc << endl;
21 cout << endl;
22
23 // Copy the UTC into a stringstream and show it on the screen
24 stringstream str;
25 str << "stringstream: " << Time::sql << utc;
26 cout << str.str() << endl;
27 cout << endl;
28
29 // Calculate the corresponsing MJD and shoud MJD and corresponding UTC
30 const double mjd1 = utc.Mjd();
31 cout << "Mjd: " << Time::sql << utc << " (" << mjd1 << ")" << endl;
32
33 // Set utc to the previously calculated MJD
34 utc.Mjd(mjd1);
35
36 // Re-calcualte MJD from this
37 const double mjd2 = utc.Mjd();
38
39 // Show the newly calculated MJD and time and the difference between both
40 cout << "Mjd: " << Time::sql << utc << " (" << mjd2 << ")" << endl;
41 cout << "Diff: " << mjd1 - mjd2 << endl;
42 cout << endl;
43
44 // Instantiate a Time object with an artificial time
45 const Time bd(1974, 9, 9, 21, 59, 42, 123456);
46
47 // Show it in two different representations
48 cout << "Loc default: " << Time::def << bd << endl;
49 cout << "Standard: " << Time::std << bd << endl;
50 cout << endl;
51
52 // Clear the stringstream contents
53 str.str("");
54
55 // Stream the time in its sql representation into the stringstream
56 str << Time::sql << bd;
57
58 // Stream a time from the stringstream considering an sql representation
59 // into a Time object
60 Time tm;
61 str >> Time::sql >> tm;
62
63 // Output stream and interpreted time
64 cout << "Stream: " << str.str() << endl;
65 cout << "Time: " << Time::sql << tm << endl;
66 cout << endl;
67
68 // Print the individual elements of the date and the time
69 cout << "Elements: ";
70 cout << tm.Y() << " " << tm.M() << " " << tm.D() << " " ;
71 cout << tm.h() << " " << tm.m() << " " << tm.s() << " " ;
72 cout << tm.us() << endl;
73 cout << endl;
74
75 const Time t0;
76 Time t1 = t0;
77 cout << "T0 = " << t0 << endl;
78 cout << "T1 = " << t1 << endl;
79 t1 += hours(4242);
80 cout << "T1 += 4242h: " << t1 << endl;
81 t1 += minutes(42);
82 cout << "T1 += 42min: " << t1 << endl;
83 t1 += seconds(42);
84 cout << "T1 += 42sec: " << t1 << endl;
85 cout << endl;
86
87 cout << "T1 - T0 = " << t1-t0 << endl;
88
89 const time_duration diff = t1-t0;
90 cout << "T1 - T0 = " << diff.total_seconds() << "sec" << endl;
91 cout << endl;
92
93 return 0;
94}
95
96// **************************************************************************
97/** @example time.cc
98
99Example for the usage of the class Time
100
101**/
102// **************************************************************************
Note: See TracBrowser for help on using the repository browser.