blob: 5e5dc7cd2109bf5ae74d410dd28acdc22cbdab72 [file] [log] [blame]
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -08001/**
2 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
4 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
5 * See COPYING for copyright and distribution information.
6 */
7
8#ifndef NDN_TIME_HPP
9#define NDN_TIME_HPP
10
Alexander Afanasyevd409d592014-01-28 18:36:38 -080011#include "ndn-cpp-dev/common.hpp"
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -080012
13namespace ndn {
14
Alexander Afanasyevd409d592014-01-28 18:36:38 -080015MillisecondsSince1970
16ndn_getNowMilliseconds();
17
18int
19ndn_toIsoString(MillisecondsSince1970 milliseconds, char *isoString);
20
21int
22ndn_fromIsoString(const char* isoString, MillisecondsSince1970 *milliseconds);
23
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -080024/**
25 * Convert to the ISO string representation of the time.
26 * @param time Milliseconds since 1/1/1970.
27 * @return The ISO string.
28 */
29inline std::string
30toIsoString(const MillisecondsSince1970& time)
31{
32 char isoString[25];
Alexander Afanasyevd409d592014-01-28 18:36:38 -080033 int error;
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -080034 if ((error = ndn_toIsoString(time, isoString)))
Alexander Afanasyevd409d592014-01-28 18:36:38 -080035 throw std::runtime_error("toIsoString");
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -080036
37 return isoString;
38}
39
40/**
41 * Convert from the ISO string representation to the internal time format.
42 * @param isoString The ISO time formatted string.
43 * @return The time in milliseconds since 1/1/1970.
44 */
45inline MillisecondsSince1970
46fromIsoString(const std::string& isoString)
47{
48 MillisecondsSince1970 milliseconds;
Alexander Afanasyevd409d592014-01-28 18:36:38 -080049 int error;
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -080050 if ((error = ndn_fromIsoString(isoString.c_str(), &milliseconds)))
Alexander Afanasyevd409d592014-01-28 18:36:38 -080051 throw std::runtime_error("fromIsoString");
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -080052
53 return milliseconds;
54}
55
56} // namespace ndn
57
58#endif // NDN_TIME_HPP