blob: 8f110a96531665104980e8bb5390df161c240311 [file] [log] [blame]
/**
* Copyright (C) 2013 Regents of the University of California.
* @author: Jeff Thompson <jefft0@remap.ucla.edu>
* @author: Yingdi Yu <yingdi@cs.ucla.edu>
* See COPYING for copyright and distribution information.
*/
#ifndef NDN_TIME_HPP
#define NDN_TIME_HPP
#include "common.hpp"
#include <boost/date_time/posix_time/posix_time.hpp>
namespace ndn {
const boost::posix_time::ptime UNIX_EPOCH_TIME =
boost::posix_time::ptime (boost::gregorian::date (1970, boost::gregorian::Jan, 1));
/**
* @brief Get the current time in milliseconds since 1/1/1970, including fractions of a millisecond
*/
inline MillisecondsSince1970
getNowMilliseconds()
{
return (boost::posix_time::microsec_clock::universal_time() - UNIX_EPOCH_TIME).total_milliseconds();
}
inline MillisecondsSince1970
ndn_getNowMilliseconds()
{
return getNowMilliseconds();
}
/**
* Convert to the ISO string representation of the time.
* @param time Milliseconds since 1/1/1970.
* @return The ISO string.
*/
inline std::string
toIsoString(const MillisecondsSince1970& time)
{
boost::posix_time::ptime boostTime = UNIX_EPOCH_TIME + boost::posix_time::milliseconds(time);
/// @todo Determine whether this is necessary at all
if ((time % 1000) == 0)
return boost::posix_time::to_iso_string(boostTime) + ".000000";
else
return boost::posix_time::to_iso_string(boostTime);
}
/**
* Convert from the ISO string representation to the internal time format.
* @param isoString The ISO time formatted string.
* @return The time in milliseconds since 1/1/1970.
*/
inline MillisecondsSince1970
fromIsoString(const std::string& isoString)
{
boost::posix_time::ptime boostTime = boost::posix_time::from_iso_string(isoString);
return (boostTime-UNIX_EPOCH_TIME).total_milliseconds();
}
} // namespace ndn
#endif // NDN_TIME_HPP