4
#include <boost/date_time/time_formatting_streams.hpp>
5
#include <boost/date_time/local_time/local_date_time.hpp>
6
#include <boost/date_time/local_time/local_time.hpp>
7
#include <boost/date_time/local_time/local_time_io.hpp>
8
#include <boost/date_time/posix_time/posix_time.hpp>
9
#include <boost/date_time/microsec_time_clock.hpp>
10
#include <boost/date_time/c_local_time_adjustor.hpp>
11
#include <boost/date_time/local_time_adjustor.hpp>
14
using namespace boost;
15
using namespace boost::date_time;
16
using namespace boost::local_time;
17
using namespace boost::posix_time;
22
/* Setting locale to read time values in the appropriate format, both
23
time_input_facet and local_time_input_facet are reading the same way.
24
However local_time_input_facet supports time zone information, while
25
time_input_facet ignore that */
27
boost::date_time::time_input_facet<ptime,char> *csv_facet = new boost::date_time::time_input_facet<ptime,char>("%d-%b-%y %H:%M:%S");
28
boost::local_time::local_time_input_facet *local_facet = new boost::local_time::local_time_input_facet("%d-%b-%y %H:%M:%S %ZP");
29
std::locale csv_time_locale(std::locale::classic(), csv_facet);
30
std::locale local_locale(std::locale::classic(), local_facet);
32
stringstream ss("28-Jul-07 13:59:47");
33
ss.exceptions(std::ios_base::failbit); // Throw exception on invalid format
34
ss.imbue(csv_time_locale); // Set the date format
37
/* Creating time zones. Unfortunatelly, there is no way to create
38
current system time zone (no standard)
40
time_zone_ptr is smart pointer (auto_ptr) and will be deallocated
42
time_zone_ptr gmt_zone(new posix_time_zone("GMT"));
43
/* There is actually nothing known about CET, you should prepare
44
special CSV file with zone information, otherwise it would operate
46
time_zone_ptr cet_zone(new posix_time_zone("CET"));
47
/* And this would operate like GMT+1, but the GMT title will be
49
time_zone_ptr gmt1_zone(new posix_time_zone("GMT+1"));
51
/* Using TZ database (included with boost sources)
53
tz_db.load_from_file("date_time_zonespec.csv");
54
boost::shared_ptr<time_zone_base> nyc_tz =
55
tz_db.time_zone_from_region("America/New_York"); */
59
ss >> tm; /* ptime have UTC time (or not associated with time_zone,
60
depending on view. Various functions from boost having
61
different view on this point */
63
local_date_time gmt_tm(tm, gmt_zone); // Just converting to local_date_time
64
local_date_time gmt1_tm(tm, gmt1_zone); // Converting to GMT+1
65
local_date_time cet_tm(tm, cet_zone); // Converting to CET (not really)
67
/* Unfortunatelly it is not possible to get current system time zone
68
in the common case. It is only possible to get current distance to
69
the GMT, but that don't provide any information on time savings.
71
For example, if the current distance to GMT is +1 hours, this could
72
correspond GMT+1 or CET time zones. There GMT+1 is +1 all year round
73
and CET is +2 while day light savings is on */
75
cout << "Time (No association): " << tm << ", " << mktime(&to_tm(tm)) << endl;
76
cout << "Time (GMT associated): " << gmt_tm << ", " << mktime(&to_tm(gmt_tm.utc_time())) << endl;
77
cout << "Time (GMT+1 associated): " << gmt1_tm << ", " << mktime(&to_tm(gmt1_tm.utc_time())) << " (see comments)" << endl;
78
cout << "Time (CET associated): " << cet_tm << ", " << mktime(&to_tm(cet_tm.utc_time())) << " (see comments)" << endl;
80
/* This is a buggy, it will assume current (this moment) difference to
81
GMT performing to_tm conversion, this will lead to different results
82
depending on conversion time (DST is on or off) */
83
// cout << "Time (GMT associated): " << ltm << ", " << mktime(&to_tm(ltm)) << endl;
87
cout << "GMT+1 converting to UTC : " << gmt1_tm.utc_time() << endl;
88
/* This function really stores local time in GMT storage,
89
quite crazy isn't it? */
90
cout << "GMT+1 converting to Local: " << gmt1_tm.local_time() << endl;
93
cout << "Local to GMT : " << local_date_time(gmt1_tm.local_time(), gmt_zone) << endl;
95
/* Reading time with TZ info. The problems with TZ names will
96
prove themselves here as well */
98
stringstream ss2("28-Jul-07 13:59:47 TMG+1");
99
ss2.exceptions(std::ios_base::failbit); // Throw exception on invalid format
100
ss2.imbue(local_locale); // Set the date format
103
stringstream ss3("28-Jul-07 13:59:47 TMG+1");
104
ss3.exceptions(std::ios_base::failbit); // Throw exception on invalid format
105
ss3.imbue(local_locale); // Set the date format
108
cout << "Time+TZ(GMT+1) (Orig. GMT ): " << gmt_tm << ", " << mktime(&to_tm(gmt_tm.utc_time())) << endl;
109
cout << "Time+TZ(GMT+1) (Orig. GMT+1): " << gmt1_tm << ", " << mktime(&to_tm(gmt1_tm.utc_time())) << endl;
112
cout << "Generating: " << endl;
113
cout << " UTC : " << boost::date_time::microsec_clock<ptime>::universal_time() << endl;
114
cout << " Local: " << boost::date_time::microsec_clock<ptime>::local_time() << endl;
117
/* Unfortunately, no way to convert Local Time, back to UTC. However, we can
118
do a trick. We can convert from GMT to LocalTime. So, theoretically, to
119
get GMT from LocalTime we can go the same amount in other direction.
121
However, we can get on the DST changes. Therefore, afterwards we should try
122
to convert back to Local and check if it's OK'ey. If not we can try go up
123
and down for DST interval.*/