Leosac  0.8.0
Open Source Access Control
DateTimeConverter.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
22 #include "boost/date_time/posix_time/posix_time.hpp"
23 #include <chrono>
24 
25 template <typename Duration>
26 boost::posix_time::ptime time_point_ptime(
27  const std::chrono::time_point<std::chrono::system_clock, Duration> &from)
28 {
29  typedef std::chrono::nanoseconds duration_t;
30  typedef duration_t::rep rep_t;
31  rep_t d =
32  std::chrono::duration_cast<duration_t>(from.time_since_epoch()).count();
33  rep_t sec = d / 1000000000;
34  rep_t nsec = d % 1000000000;
35  return boost::posix_time::from_time_t(0) +
36  boost::posix_time::seconds(static_cast<long>(sec)) +
37 #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
38  boost::posix_time::nanoseconds(nsec);
39 #else
40  boost::posix_time::microseconds((nsec + 500) / 1000);
41 #endif
42 }
time_point_ptime
boost::posix_time::ptime time_point_ptime(const std::chrono::time_point< std::chrono::system_clock, Duration > &from)
Definition: DateTimeConverter.hpp:26