Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 1 | /** |
| 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 | |
| 11 | #include <stdexcept> |
| 12 | #include <ndn-cpp/c/util/time.h> |
| 13 | |
| 14 | namespace ndn { |
| 15 | |
| 16 | /** |
| 17 | * Convert to the ISO string representation of the time. |
| 18 | * @param time Milliseconds since 1/1/1970. |
| 19 | * @return The ISO string. |
| 20 | */ |
| 21 | inline std::string |
| 22 | toIsoString(const MillisecondsSince1970& time) |
| 23 | { |
| 24 | char isoString[25]; |
| 25 | ndn_Error error; |
| 26 | if ((error = ndn_toIsoString(time, isoString))) |
| 27 | throw std::runtime_error(ndn_getErrorString(error)); |
| 28 | |
| 29 | return isoString; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Convert from the ISO string representation to the internal time format. |
| 34 | * @param isoString The ISO time formatted string. |
| 35 | * @return The time in milliseconds since 1/1/1970. |
| 36 | */ |
| 37 | inline MillisecondsSince1970 |
| 38 | fromIsoString(const std::string& isoString) |
| 39 | { |
| 40 | MillisecondsSince1970 milliseconds; |
| 41 | ndn_Error error; |
| 42 | if ((error = ndn_fromIsoString(isoString.c_str(), &milliseconds))) |
| 43 | throw std::runtime_error(ndn_getErrorString(error)); |
| 44 | |
| 45 | return milliseconds; |
| 46 | } |
| 47 | |
| 48 | } // namespace ndn |
| 49 | |
| 50 | #endif // NDN_TIME_HPP |