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 | |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 11 | #include "ndn-cpp-dev/common.hpp" |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 12 | |
| 13 | namespace ndn { |
| 14 | |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 15 | MillisecondsSince1970 |
| 16 | ndn_getNowMilliseconds(); |
| 17 | |
| 18 | int |
| 19 | ndn_toIsoString(MillisecondsSince1970 milliseconds, char *isoString); |
| 20 | |
| 21 | int |
| 22 | ndn_fromIsoString(const char* isoString, MillisecondsSince1970 *milliseconds); |
| 23 | |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 24 | /** |
| 25 | * Convert to the ISO string representation of the time. |
| 26 | * @param time Milliseconds since 1/1/1970. |
| 27 | * @return The ISO string. |
| 28 | */ |
| 29 | inline std::string |
| 30 | toIsoString(const MillisecondsSince1970& time) |
| 31 | { |
| 32 | char isoString[25]; |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 33 | int error; |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 34 | if ((error = ndn_toIsoString(time, isoString))) |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 35 | throw std::runtime_error("toIsoString"); |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 36 | |
| 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 | */ |
| 45 | inline MillisecondsSince1970 |
| 46 | fromIsoString(const std::string& isoString) |
| 47 | { |
| 48 | MillisecondsSince1970 milliseconds; |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 49 | int error; |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 50 | if ((error = ndn_fromIsoString(isoString.c_str(), &milliseconds))) |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 51 | throw std::runtime_error("fromIsoString"); |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 52 | |
| 53 | return milliseconds; |
| 54 | } |
| 55 | |
| 56 | } // namespace ndn |
| 57 | |
| 58 | #endif // NDN_TIME_HPP |