Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "time.hpp" |
| 23 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace time { |
| 27 | |
| 28 | std::string |
| 29 | toIsoString(const system_clock::TimePoint& timePoint) |
| 30 | { |
| 31 | namespace bpt = boost::posix_time; |
| 32 | bpt::ptime ptime = bpt::from_time_t(system_clock::to_time_t(timePoint)); |
| 33 | |
| 34 | uint64_t micro = duration_cast<microseconds>(timePoint - getUnixEpoch()).count() % 1000000; |
| 35 | if (micro > 0) |
| 36 | { |
| 37 | ptime += bpt::microseconds(micro); |
| 38 | return bpt::to_iso_string(ptime); |
| 39 | } |
| 40 | else |
| 41 | return bpt::to_iso_string(ptime); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | system_clock::TimePoint |
| 46 | fromIsoString(const std::string& isoString) |
| 47 | { |
| 48 | namespace bpt = boost::posix_time; |
| 49 | static bpt::ptime posixTimeEpoch = bpt::from_time_t(0); |
| 50 | |
| 51 | bpt::ptime ptime = bpt::from_iso_string(isoString); |
| 52 | |
| 53 | system_clock::TimePoint point = |
| 54 | system_clock::from_time_t((ptime - posixTimeEpoch).total_seconds()); |
| 55 | point += microseconds((ptime - posixTimeEpoch).total_microseconds() % 1000000); |
| 56 | return point; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | std::string |
| 61 | toString(const system_clock::TimePoint& timePoint, |
| 62 | const std::string& format/* = "%Y-%m-%d %H:%M:%S"*/, |
| 63 | const std::locale& locale/* = std::locale("C")*/) |
| 64 | { |
| 65 | namespace bpt = boost::posix_time; |
| 66 | bpt::ptime ptime = bpt::from_time_t(system_clock::to_time_t(timePoint)); |
| 67 | |
| 68 | uint64_t micro = duration_cast<microseconds>(timePoint - getUnixEpoch()).count() % 1000000; |
| 69 | ptime += bpt::microseconds(micro); |
| 70 | |
| 71 | bpt::time_facet* facet = new bpt::time_facet(format.c_str()); |
| 72 | std::ostringstream formattedTimePoint; |
| 73 | formattedTimePoint.imbue(std::locale(locale, facet)); |
| 74 | formattedTimePoint << ptime; |
| 75 | |
| 76 | return formattedTimePoint.str(); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | system_clock::TimePoint |
| 81 | fromString(const std::string& formattedTimePoint, |
| 82 | const std::string& format/* = "%Y-%m-%d %H:%M:%S"*/, |
| 83 | const std::locale& locale/* = std::locale("C")*/) |
| 84 | { |
| 85 | namespace bpt = boost::posix_time; |
| 86 | static bpt::ptime posixTimeEpoch = bpt::from_time_t(0); |
| 87 | |
| 88 | bpt::time_input_facet* facet = new bpt::time_input_facet(format); |
| 89 | std::istringstream is(formattedTimePoint); |
| 90 | |
| 91 | is.imbue(std::locale(locale, facet)); |
| 92 | bpt::ptime ptime; |
| 93 | is >> ptime; |
| 94 | |
| 95 | system_clock::TimePoint point = |
| 96 | system_clock::from_time_t((ptime - posixTimeEpoch).total_seconds()); |
| 97 | point += microseconds((ptime - posixTimeEpoch).total_microseconds() % 1000000); |
| 98 | return point; |
| 99 | } |
| 100 | |
| 101 | } // namespace time |
| 102 | } // namespace ndn |