ProteoWizard
DateTime.hpp
Go to the documentation of this file.
1//
2// $Id$
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
28namespace bdt = boost::date_time;
29namespace bpt = boost::posix_time;
30namespace blt = boost::local_time;
31
32using bpt::from_time_t;
33
34#ifdef WIN32
35using bpt::from_ftime;
36#endif
37
38using boost::gregorian::date;
39
40
41namespace boost {
42namespace 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 */
49template<class time_type>
50inline
51time_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 secondOffset = round(fraction);
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
76namespace pwiz {
77namespace util {
78
79
80/// formats a boost ptime according to a custom format string
81inline 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
92inline 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
103inline 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
114inline 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)
130inline 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)
140inline 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
148inline 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_
time_type time_from_OADATE(double oa_date)
Create a time object from an OLE automation date value.
Definition DateTime.hpp:51
std::string encode_xml_datetime(const bpt::ptime &t)
returns a string representation suitable for an xsd:datetime attribute; input is assumed to be UTC ti...
Definition DateTime.hpp:130
blt::local_date_time decode_xml_datetime(const std::string &t)
converts an xsd:datetime attribute to a local_date_time
Definition DateTime.hpp:148
std::string format_date_time(const std::string &format, const bpt::ptime &t)
formats a boost ptime according to a custom format string
Definition DateTime.hpp:81
blt::local_date_time parse_date_time(const std::string &format, const std::string &t)
converts a custom formatted datetime string to a boost local_date_time
Definition DateTime.hpp:114