ProteoWizard
DateTime.hpp
Go to the documentation of this file.
1 //
2 // $Id: DateTime.hpp 2616 2011-04-07 22:26:33Z chambm $
3 //
4 //
5 // Original author: Matt Chambers <matt.chambers .@. vanderbilt.edu>
6 //
7 // Copyright 2008 Vanderbilt University - Nashville, TN 37232
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // Unless required by applicable law or agreed to in writing, software
16 // distributed under the License is distributed on an "AS IS" BASIS,
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 // See the License for the specific language governing permissions and
19 // limitations under the License.
20 //
21 
22 #ifndef _DATETIME_HPP_
23 #define _DATETIME_HPP_
24 
25 #include <boost/date_time/local_time/local_time.hpp>
26 #include <boost/math/special_functions/modf.hpp>
27 
28 namespace bdt = boost::date_time;
29 namespace bpt = boost::posix_time;
30 namespace blt = boost::local_time;
31 
32 using bpt::from_time_t;
33 
34 #ifdef WIN32
35 using bpt::from_ftime;
36 #endif
37 
38 using boost::gregorian::date;
39 
40 
41 namespace boost {
42 namespace date_time {
43 
44 //! Create a time object from an OLE automation date value.
45 /*! Create a time object from an OLE automation date value.
46  * An OLE automation date is implemented as a floating-point number *
47  * whose value is the number of days from midnight, 30 December 1899.
48  */
49 template<class time_type>
50 inline
51 time_type time_from_OADATE(double oa_date)
52 {
53  typedef typename time_type::date_type date_type;
54  typedef typename time_type::date_duration_type date_duration_type;
55  typedef typename time_type::time_duration_type time_duration_type;
56  using boost::math::modf;
57 
58  static const date_type base_date(1899, Dec, 30);
59  static const time_type base_time(base_date, time_duration_type(0,0,0));
60 
61  int dayOffset, hourOffset, minuteOffset, secondOffset;
62  double fraction = fabs(modf(oa_date, &dayOffset)) * 24; // fraction = hours
63  fraction = modf(fraction, &hourOffset) * 60; // fraction = minutes
64  fraction = modf(fraction, &minuteOffset) * 60; // fraction = seconds
65  modf(fraction, &secondOffset);
66  time_type t(base_time);
67  t += time_duration_type(hourOffset, minuteOffset, secondOffset);
68  t += date_duration_type(dayOffset);
69  return t;
70 }
71 
72 }
73 }
74 
75 
76 namespace pwiz {
77 namespace util {
78 
79 
80 /// formats a boost ptime according to a custom format string
81 inline std::string format_date_time(const std::string& format, const bpt::ptime& t)
82 {
83  bpt::time_facet* output_facet = new bpt::time_facet;
84  output_facet->format(format.c_str());
85  std::ostringstream ss;
86  ss.imbue(std::locale(std::locale::classic(), output_facet));
87  return static_cast<std::ostringstream&>(ss << t).str();
88 }
89 
90 
91 /// formats a boost local_date_time according to a custom format string
92 inline std::string format_date_time(const std::string& format, const blt::local_date_time& t)
93 {
94  blt::local_time_facet* output_facet = new blt::local_time_facet;
95  output_facet->format(format.c_str());
96  std::ostringstream ss;
97  ss.imbue(std::locale(std::locale::classic(), output_facet));
98  return static_cast<std::ostringstream&>(ss << t).str();
99 }
100 
101 
102 /// formats a boost time duration according to a custom format string
103 inline std::string format_date_time(const std::string& format, const bpt::time_duration& t)
104 {
105  bpt::time_facet* output_facet = new bpt::time_facet;
106  output_facet->format(format.c_str());
107  std::ostringstream ss;
108  ss.imbue(std::locale(std::locale::classic(), output_facet));
109  return static_cast<std::ostringstream&>(ss << t).str();
110 }
111 
112 
113 /// converts a custom formatted datetime string to a boost local_date_time
114 inline blt::local_date_time parse_date_time(const std::string& format, const std::string& t)
115 {
116  blt::local_time_input_facet* input_facet = new blt::local_time_input_facet;
117  input_facet->format(format.c_str());
118  std::istringstream ss(t);
119  ss.imbue(std::locale(std::locale::classic(), input_facet));
120 
121  blt::local_date_time result(bdt::not_a_date_time);
122  ss >> result;
123  return result;
124 }
125 
126 
127 /// returns a string representation suitable for an xsd:datetime attribute;
128 /// input is assumed to be UTC time;
129 /// output string is UTC time (as denoted by the 'Z' suffix)
130 inline std::string encode_xml_datetime(const bpt::ptime& t)
131 {
132  // 2007-06-27T15:23:45Z
133  return format_date_time("%Y-%m-%dT%H:%M:%SZ", t);
134 }
135 
136 
137 /// returns a string representation suitable for an xsd:datetime attribute;
138 /// time zone is assumed to be correct;
139 /// output string is UTC time (as denoted by the 'Z' suffix)
140 inline std::string encode_xml_datetime(const blt::local_date_time& t)
141 {
142  // 2007-06-27T15:23:45Z
143  return encode_xml_datetime(t.utc_time());
144 }
145 
146 
147 /// converts an xsd:datetime attribute to a local_date_time
148 inline blt::local_date_time decode_xml_datetime(const std::string& t)
149 {
150  blt::local_time_input_facet* input_facet = new blt::local_time_input_facet;
151  input_facet->format("%Y-%m-%dT%H:%M:%SZ");
152  std::stringstream ss(t);
153  ss.imbue(std::locale(std::locale::classic(), input_facet));
154  blt::local_date_time result(bdt::not_a_date_time);
155  ss >> result;
156  return blt::local_date_time(result.utc_time(), blt::time_zone_ptr());
157 }
158 
159 
160 } // namespace util
161 } // namespace pwiz
162 
163 
164 #endif // _DATETIME_HPP_