blob: 3dacbc65e45743e31f79ffda48625d389eb5df87 [file] [log] [blame]
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 */
12
13#include "time.hpp"
14#include <boost/date_time/posix_time/posix_time.hpp>
15
16namespace ndn {
17namespace time {
18
19std::string
20toIsoString(const system_clock::TimePoint& timePoint)
21{
22 namespace bpt = boost::posix_time;
23 bpt::ptime ptime = bpt::from_time_t(system_clock::to_time_t(timePoint));
24
25 uint64_t micro = duration_cast<microseconds>(timePoint - getUnixEpoch()).count() % 1000000;
26 if (micro > 0)
27 {
28 ptime += bpt::microseconds(micro);
29 return bpt::to_iso_string(ptime);
30 }
31 else
32 return bpt::to_iso_string(ptime);
33}
34
35
36system_clock::TimePoint
37fromIsoString(const std::string& isoString)
38{
39 namespace bpt = boost::posix_time;
40 static bpt::ptime posixTimeEpoch = bpt::from_time_t(0);
41
42 bpt::ptime ptime = bpt::from_iso_string(isoString);
43
44 system_clock::TimePoint point =
45 system_clock::from_time_t((ptime - posixTimeEpoch).total_seconds());
46 point += microseconds((ptime - posixTimeEpoch).total_microseconds() % 1000000);
47 return point;
48}
49
50
51std::string
52toString(const system_clock::TimePoint& timePoint,
53 const std::string& format/* = "%Y-%m-%d %H:%M:%S"*/,
54 const std::locale& locale/* = std::locale("C")*/)
55{
56 namespace bpt = boost::posix_time;
57 bpt::ptime ptime = bpt::from_time_t(system_clock::to_time_t(timePoint));
58
59 uint64_t micro = duration_cast<microseconds>(timePoint - getUnixEpoch()).count() % 1000000;
60 ptime += bpt::microseconds(micro);
61
62 bpt::time_facet* facet = new bpt::time_facet(format.c_str());
63 std::ostringstream formattedTimePoint;
64 formattedTimePoint.imbue(std::locale(locale, facet));
65 formattedTimePoint << ptime;
66
67 return formattedTimePoint.str();
68}
69
70
71system_clock::TimePoint
72fromString(const std::string& formattedTimePoint,
73 const std::string& format/* = "%Y-%m-%d %H:%M:%S"*/,
74 const std::locale& locale/* = std::locale("C")*/)
75{
76 namespace bpt = boost::posix_time;
77 static bpt::ptime posixTimeEpoch = bpt::from_time_t(0);
78
79 bpt::time_input_facet* facet = new bpt::time_input_facet(format);
80 std::istringstream is(formattedTimePoint);
81
82 is.imbue(std::locale(locale, facet));
83 bpt::ptime ptime;
84 is >> ptime;
85
86 system_clock::TimePoint point =
87 system_clock::from_time_t((ptime - posixTimeEpoch).total_seconds());
88 point += microseconds((ptime - posixTimeEpoch).total_microseconds() % 1000000);
89 return point;
90}
91
92} // namespace time
93} // namespace ndn