1 | #ifndef BOOST_DATE_TIME_LOCAL_TIME_IO_HPP__
|
---|
2 | #define BOOST_DATE_TIME_LOCAL_TIME_IO_HPP__
|
---|
3 |
|
---|
4 | /* Copyright (c) 2003-2004 CrystalClear Software, Inc.
|
---|
5 | * Subject to the Boost Software License, Version 1.0.
|
---|
6 | * (See accompanying file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
---|
7 | * Author: Jeff Garland, Bart Garst
|
---|
8 | * $Date: 2005/06/28 13:11:45 $
|
---|
9 | */
|
---|
10 |
|
---|
11 | #include <iostream>
|
---|
12 | #include "boost/date_time/local_time/local_date_time.hpp"
|
---|
13 | #include "boost/date_time/local_time/posix_time_zone.hpp"
|
---|
14 | #include "boost/date_time/time_facet.hpp"
|
---|
15 | #include "boost/date_time/string_convert.hpp"
|
---|
16 | #include "boost/io/ios_state.hpp"
|
---|
17 |
|
---|
18 | namespace boost {
|
---|
19 | namespace local_time {
|
---|
20 |
|
---|
21 | typedef boost::date_time::time_facet<local_date_time, wchar_t> wlocal_time_facet;
|
---|
22 | typedef boost::date_time::time_facet<local_date_time, char> local_time_facet;
|
---|
23 |
|
---|
24 | typedef boost::date_time::time_input_facet<local_date_time::utc_time_type,wchar_t> wlocal_time_input_facet;
|
---|
25 | typedef boost::date_time::time_input_facet<local_date_time::utc_time_type,char> local_time_input_facet;
|
---|
26 |
|
---|
27 | //! operator<< for local_date_time - see local_time docs for formatting details
|
---|
28 | template<class CharT, class TraitsT>
|
---|
29 | inline
|
---|
30 | std::basic_ostream<CharT, TraitsT>&
|
---|
31 | operator<<(std::basic_ostream<CharT, TraitsT>& os, const local_date_time& ldt)
|
---|
32 | {
|
---|
33 | boost::io::ios_flags_saver iflags(os);
|
---|
34 | typedef local_date_time time_type;//::utc_time_type typename
|
---|
35 | typedef date_time::time_facet<time_type, CharT> custom_time_facet;
|
---|
36 | typedef std::time_put<CharT> std_time_facet;
|
---|
37 | std::ostreambuf_iterator<CharT> oitr(os);
|
---|
38 |
|
---|
39 | if(std::has_facet<custom_time_facet>(os.getloc())) {
|
---|
40 | std::use_facet<custom_time_facet>(os.getloc()).put(oitr,
|
---|
41 | os,
|
---|
42 | os.fill(),
|
---|
43 | ldt);
|
---|
44 | }
|
---|
45 | else {
|
---|
46 | custom_time_facet* f = new custom_time_facet();
|
---|
47 | std::locale l = std::locale(os.getloc(), f);
|
---|
48 | os.imbue(l);
|
---|
49 | f->put(oitr, os, os.fill(), ldt);
|
---|
50 | }
|
---|
51 |
|
---|
52 | return os;
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | //! input operator for local_date_time
|
---|
57 | template <class CharT, class Traits>
|
---|
58 | inline
|
---|
59 | std::basic_istream<CharT, Traits>&
|
---|
60 | operator>>(std::basic_istream<CharT, Traits>& is, local_date_time& ldt)
|
---|
61 | {
|
---|
62 | boost::io::ios_flags_saver iflags(is);
|
---|
63 | typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
|
---|
64 | if (strm_sentry) {
|
---|
65 | try {
|
---|
66 | typedef typename local_date_time::utc_time_type utc_time_type;
|
---|
67 | typedef typename date_time::time_input_facet<utc_time_type, CharT> time_input_facet;
|
---|
68 |
|
---|
69 | // intermediate objects
|
---|
70 | std::basic_string<CharT> tz_str;
|
---|
71 | utc_time_type pt(not_a_date_time);
|
---|
72 |
|
---|
73 | std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
|
---|
74 | if(std::has_facet<time_input_facet>(is.getloc())) {
|
---|
75 | std::use_facet<time_input_facet>(is.getloc()).get_local_time(sit, str_end, is, pt, tz_str);
|
---|
76 | }
|
---|
77 | else {
|
---|
78 | time_input_facet* f = new time_input_facet();
|
---|
79 | std::locale l = std::locale(is.getloc(), f);
|
---|
80 | is.imbue(l);
|
---|
81 | f->get_local_time(sit, str_end, is, pt, tz_str);
|
---|
82 | }
|
---|
83 | if(tz_str.empty()) {
|
---|
84 | time_zone_ptr null_ptr;
|
---|
85 | // a null time_zone_ptr creates a local_date_time that is UTC
|
---|
86 | ldt = local_date_time(pt, null_ptr);
|
---|
87 | }
|
---|
88 | else {
|
---|
89 | time_zone_ptr tz_ptr(new posix_time_zone(date_time::convert_string_type<CharT,char>(tz_str)));
|
---|
90 | // the "date & time" constructor expects the time label to *not* be utc.
|
---|
91 | // a posix_tz_string also expects the time label to *not* be utc.
|
---|
92 | ldt = local_date_time(pt.date(), pt.time_of_day(), tz_ptr, local_date_time::EXCEPTION_ON_ERROR);
|
---|
93 | }
|
---|
94 | }
|
---|
95 | catch(...) {
|
---|
96 | // mask tells us what exceptions are turned on
|
---|
97 | std::ios_base::iostate exception_mask = is.exceptions();
|
---|
98 | // if the user wants exceptions on failbit, we'll rethrow our
|
---|
99 | // date_time exception & set the failbit
|
---|
100 | if(std::ios_base::failbit & exception_mask) {
|
---|
101 | try { is.setstate(std::ios_base::failbit); }
|
---|
102 | catch(std::ios_base::failure&) {} // ignore this one
|
---|
103 | throw; // rethrow original exception
|
---|
104 | }
|
---|
105 | else {
|
---|
106 | // if the user want's to fail quietly, we simply set the failbit
|
---|
107 | is.setstate(std::ios_base::failbit);
|
---|
108 | }
|
---|
109 |
|
---|
110 | }
|
---|
111 | }
|
---|
112 | return is;
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | } } // namespaces
|
---|
117 |
|
---|
118 | #endif // BOOST_DATE_TIME_LOCAL_TIME_IO_HPP__
|
---|