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

Last change on this file since 16784 was 10253, checked in by tbretz, 14 years ago
Added exaple for SetFrom Str and GetAsStr, remove fractional seconds from example where is was not working.
File size: 3.3 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::ssql << bd;
57
58 // Stream a time from the stringstream considering an sql representation
59 // into a Time object
60 Time tm;
61 str >> Time::ssql >> tm;
62
63 // Output stream and interpreted time
64 cout << "Stream: " << str.str() << endl;
65 cout << "Time: " << Time::ssql << 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 // Set and get a Time from a string
76 const string s = "2042-12-24 12:42:42";
77
78 Time tstr;
79 tstr.SetFromStr(s);
80 cout << "String: " << s << endl;
81 cout << "TimeFromStr: " << tstr.GetAsStr() << endl;
82 cout << endl;
83
84 // Calculate with times
85 const Time t0;
86 Time t1 = t0;
87 cout << "T0 = " << t0 << endl;
88 cout << "T1 = " << t1 << endl;
89 t1 += hours(4242);
90 cout << "T1 += 4242h: " << t1 << endl;
91 t1 += minutes(42);
92 cout << "T1 += 42min: " << t1 << endl;
93 t1 += seconds(42);
94 cout << "T1 += 42sec: " << t1 << endl;
95 cout << endl;
96
97 cout << "T1 - T0 = " << t1-t0 << endl;
98
99 const time_duration diff = t1-t0;
100 cout << "T1 - T0 = " << diff.total_seconds() << "sec" << endl;
101 cout << endl;
102
103 return 0;
104}
105
106// **************************************************************************
107/** @example time.cc
108
109Example for the usage of the class Time
110
111**/
112// **************************************************************************
Note: See TracBrowser for help on using the repository browser.