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 | 1950885 | 2014-01-29 01:01:51 -0800 | [diff] [blame^] | 11 | #include "../common.hpp" |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 12 | #include <boost/date_time/posix_time/posix_time.hpp> |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 13 | |
| 14 | namespace ndn { |
| 15 | |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 16 | const boost::posix_time::ptime UNIX_EPOCH_TIME = |
| 17 | boost::posix_time::ptime (boost::gregorian::date (1970, boost::gregorian::Jan, 1)); |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 18 | |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 19 | /** |
| 20 | * @brief Get the current time in milliseconds since 1/1/1970, including fractions of a millisecond |
| 21 | */ |
| 22 | inline MillisecondsSince1970 |
| 23 | getNowMilliseconds() |
| 24 | { |
| 25 | return (boost::posix_time::microsec_clock::universal_time() - UNIX_EPOCH_TIME).total_milliseconds(); |
| 26 | } |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 28 | inline MillisecondsSince1970 |
| 29 | ndn_getNowMilliseconds() |
| 30 | { |
| 31 | return getNowMilliseconds(); |
| 32 | } |
| 33 | |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame] | 34 | |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 35 | /** |
| 36 | * Convert to the ISO string representation of the time. |
| 37 | * @param time Milliseconds since 1/1/1970. |
| 38 | * @return The ISO string. |
| 39 | */ |
| 40 | inline std::string |
| 41 | toIsoString(const MillisecondsSince1970& time) |
| 42 | { |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 43 | boost::posix_time::ptime boostTime = UNIX_EPOCH_TIME + boost::posix_time::milliseconds(time); |
| 44 | |
| 45 | /// @todo Determine whether this is necessary at all |
| 46 | if ((time % 1000) == 0) |
| 47 | return boost::posix_time::to_iso_string(boostTime) + ".000000"; |
| 48 | else |
| 49 | return boost::posix_time::to_iso_string(boostTime); |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Convert from the ISO string representation to the internal time format. |
| 54 | * @param isoString The ISO time formatted string. |
| 55 | * @return The time in milliseconds since 1/1/1970. |
| 56 | */ |
| 57 | inline MillisecondsSince1970 |
| 58 | fromIsoString(const std::string& isoString) |
| 59 | { |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 60 | boost::posix_time::ptime boostTime = boost::posix_time::from_iso_string(isoString); |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 61 | |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 62 | return (boostTime-UNIX_EPOCH_TIME).total_milliseconds(); |
Alexander Afanasyev | e2e3ca5 | 2014-01-03 13:59:07 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | } // namespace ndn |
| 66 | |
| 67 | #endif // NDN_TIME_HPP |