FACT++  1.0
time.cc
Go to the documentation of this file.
1 #include "Time.h"
2 
3 #include <iostream>
4 
5 using namespace std;
6 using namespace boost::posix_time;
7 
8 int 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 // **************************************************************************
112 // **************************************************************************
static const _time_format sql
set to format to the iso standard
Definition: Time.h:42
Adds some functionality to boost::posix_time::ptime for our needs.
Definition: Time.h:30
char str[80]
Definition: test_client.c:7
static const _time_format iso
set to format to the sql format (without the fraction of seconds)
Definition: Time.h:44
STL namespace.
int main(int, char **)
Definition: time.cc:8
static const _time_format reset
Definition: Time.h:39
static const _time_format ssql
set to format to the sql format
Definition: Time.h:43
static const _time_format def
Remove the format description from the stream.
Definition: Time.h:40
void Mjd(double mjd)
Definition: Time.cc:145
static const _time_format fmt(const char *txt=0)
A stream manipulator to set the output/input format.
Definition: Time.cc:415
void SetFromStr(const std::string &str, const char *fmt="%Y-%m-%d %H:%M:%S")
Definition: Time.cc:273
static const _time_format std
set to format to the locale default
Definition: Time.h:41
Initialize with local time.
Definition: Time.h:53
std::string GetAsStr(const char *fmt="%Y-%m-%d %H:%M:%S") const
Definition: Time.cc:240