blob: 82d59e98bc3e15d51ceeb3be5adc809399c4b93a [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 Afanasyev19508852014-01-29 01:01:51 -080011#include "../common.hpp"
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080012#include <boost/date_time/posix_time/posix_time.hpp>
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -080013
14namespace ndn {
15
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080016const boost::posix_time::ptime UNIX_EPOCH_TIME =
17 boost::posix_time::ptime (boost::gregorian::date (1970, boost::gregorian::Jan, 1));
Alexander Afanasyevd409d592014-01-28 18:36:38 -080018
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080019/**
20 * @brief Get the current time in milliseconds since 1/1/1970, including fractions of a millisecond
21 */
22inline MillisecondsSince1970
23getNowMilliseconds()
24{
25 return (boost::posix_time::microsec_clock::universal_time() - UNIX_EPOCH_TIME).total_milliseconds();
26}
Alexander Afanasyevd409d592014-01-28 18:36:38 -080027
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080028inline MillisecondsSince1970
29ndn_getNowMilliseconds()
30{
31 return getNowMilliseconds();
32}
33
Alexander Afanasyevd409d592014-01-28 18:36:38 -080034
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -080035/**
36 * Convert to the ISO string representation of the time.
37 * @param time Milliseconds since 1/1/1970.
38 * @return The ISO string.
39 */
40inline std::string
41toIsoString(const MillisecondsSince1970& time)
42{
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080043 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 Afanasyeve2e3ca52014-01-03 13:59:07 -080050}
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 */
57inline MillisecondsSince1970
58fromIsoString(const std::string& isoString)
59{
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080060 boost::posix_time::ptime boostTime = boost::posix_time::from_iso_string(isoString);
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -080061
Alexander Afanasyev1e0a0772014-01-28 20:07:07 -080062 return (boostTime-UNIX_EPOCH_TIME).total_milliseconds();
Alexander Afanasyeve2e3ca52014-01-03 13:59:07 -080063}
64
65} // namespace ndn
66
67#endif // NDN_TIME_HPP