all: Refactoring work with time using boost::chrono

Now the library has two clocks: time::steady_clock and
time::system_clock, following (boost|std)::chrono.  In addition to
standard now() method, the library contains several helpers to convert
to/from UnixTimestamp (microsecond resolution) and IsoString (optional
microsecond resolution).  The IsoString conversions use
boost::posix_time routines, since boost::chrono supports extended IO
support only starting boost version 1.52 (Boost Chrono V2).

This commit breaks compatibility with previous API.  All time-related
Data/Interest calls must explicitly use time units to specify
FreshnessPeriod/InterestLifetime.

Brief usage/conversion guide:

- creation of time units does not support double/float types.  If
  necessary to create time unit from double, ``ndn::duration<double>`` (for
  seconds) needs to be used instead.  In some cases, this would also
  require ``ndn::duration_cast``, if the target is not ``ndn::nanoseconds``.
- ndn::getNow, ndn::ndn_getNowMilliseconds, ndn::time::now are all
  removed in favor of the now() method in a specific clock:

    * time::system_clock::now();
    * time::steady_clock::now();

- When necessary to convert system_clock::TimePoint to unix timestamp,
  ``time::toUnixTimestamp`` can be used.  This method return number of
  milliseconds since UNIX epoch as ``ndn::time::milliseconds`` type.
  Use count() method to obtain number as an integral value.

Change-Id: Icd688bc6766e008d60c3d2888173627874526e47
Refs: #1152
diff --git a/src/security/certificate-cache-ttl.cpp b/src/security/certificate-cache-ttl.cpp
index 6f831d0..0997182 100644
--- a/src/security/certificate-cache-ttl.cpp
+++ b/src/security/certificate-cache-ttl.cpp
@@ -17,32 +17,34 @@
 
 namespace ndn {
 
-CertificateCacheTtl::CertificateCacheTtl(shared_ptr<boost::asio::io_service> io, int defaultTtl)
+CertificateCacheTtl::CertificateCacheTtl(shared_ptr<boost::asio::io_service> io, const time::seconds& defaultTtl)
   : m_defaultTtl(defaultTtl)
   , m_scheduler(*io)
 {}
 
 CertificateCacheTtl::~CertificateCacheTtl()
 {}
-  
+
 void
 CertificateCacheTtl::insertCertificate(ptr_lib::shared_ptr<const IdentityCertificate> certificate)
 {
-  time::Duration expire = (certificate->getFreshnessPeriod() >= 0 ? time::milliseconds(certificate->getFreshnessPeriod()) : time::seconds(m_defaultTtl));
+  time::milliseconds expire = (certificate->getFreshnessPeriod() >= time::seconds::zero() ?
+                         certificate->getFreshnessPeriod() : m_defaultTtl);
 
   Name trackerIndex = certificate->getName().getPrefix(-1);
   EventTracker::iterator it = m_tracker.find(trackerIndex);
   if(it != m_tracker.end())
       m_scheduler.cancelEvent(m_tracker[trackerIndex]);
 
-  m_scheduler.scheduleEvent(time::seconds(0), bind(&CertificateCacheTtl::insert, this, certificate));  
-  m_tracker[trackerIndex] = m_scheduler.scheduleEvent(expire, bind(&CertificateCacheTtl::remove, this, certificate->getName()));
+  m_scheduler.scheduleEvent(time::seconds(0), bind(&CertificateCacheTtl::insert, this, certificate));
+  m_tracker[trackerIndex] = m_scheduler.scheduleEvent(expire, bind(&CertificateCacheTtl::remove,
+                                                                   this, certificate->getName()));
 
 }
 
 void
 CertificateCacheTtl::insert(ptr_lib::shared_ptr<const IdentityCertificate> certificate)
-{ 
+{
   Name name = certificate->getName().getPrefix(-1);
   m_cache[name] = certificate;
 }
@@ -56,7 +58,7 @@
     m_cache.erase(it);
 }
 
-ptr_lib::shared_ptr<const IdentityCertificate> 
+ptr_lib::shared_ptr<const IdentityCertificate>
 CertificateCacheTtl::getCertificate(const Name & certificateName)
 {
   Cache::iterator it = m_cache.find(certificateName);
@@ -67,5 +69,3 @@
 }
 
 } // namespace ndn
-
-
diff --git a/src/security/certificate-cache-ttl.hpp b/src/security/certificate-cache-ttl.hpp
index 9ac91d5..1e31e9c 100644
--- a/src/security/certificate-cache-ttl.hpp
+++ b/src/security/certificate-cache-ttl.hpp
@@ -14,25 +14,25 @@
 #include "../util/time.hpp"
 
 namespace ndn {
- 
+
 class CertificateCacheTtl : public CertificateCache
 {
 public:
-  CertificateCacheTtl(shared_ptr<boost::asio::io_service> io, int defaultTtl = 3600);
-  
+  CertificateCacheTtl(shared_ptr<boost::asio::io_service> io, const time::seconds& defaultTtl = time::seconds(3600));
+
   virtual
   ~CertificateCacheTtl();
-    
+
   virtual void
   insertCertificate(ptr_lib::shared_ptr<const IdentityCertificate> certificate);
-  
-  virtual ptr_lib::shared_ptr<const IdentityCertificate> 
+
+  virtual ptr_lib::shared_ptr<const IdentityCertificate>
   getCertificate(const Name & certificateNameWithoutVersion);
 
-private:  
+private:
   void
   insert(ptr_lib::shared_ptr<const IdentityCertificate> certificate);
-  
+
   void
   remove(const Name &certificateName);
 
@@ -40,7 +40,7 @@
   typedef std::map<Name, ptr_lib::shared_ptr<const IdentityCertificate> > Cache;
   typedef std::map<Name, EventId> EventTracker;
 
-  int m_defaultTtl;
+  time::seconds m_defaultTtl;
   Cache m_cache;
   EventTracker m_tracker;
   Scheduler m_scheduler;
diff --git a/src/security/certificate.cpp b/src/security/certificate.cpp
index 0312fd5..931a40e 100644
--- a/src/security/certificate.cpp
+++ b/src/security/certificate.cpp
@@ -26,8 +26,8 @@
 namespace ndn {
 
 Certificate::Certificate()
-  : notBefore_(std::numeric_limits<MillisecondsSince1970>::max())
-  , notAfter_(std::numeric_limits<MillisecondsSince1970>::min())
+  : notBefore_(time::system_clock::TimePoint::max())
+  , notAfter_(time::system_clock::TimePoint::min())
 {}
 
 Certificate::Certificate(const Data& data)
@@ -47,18 +47,16 @@
 bool
 Certificate::isTooEarly()
 {
-  MillisecondsSince1970 now = ndn_getNowMilliseconds();
-  if(now < notBefore_)
+  if(time::system_clock::now() < notBefore_)
     return true;
   else
     return false;
 }
 
-bool 
+bool
 Certificate::isTooLate()
 {
-  MillisecondsSince1970 now = ndn_getNowMilliseconds();
-  if(now > notAfter_)
+  if(time::system_clock::now() > notAfter_)
     return true;
   else
     return false;
@@ -106,7 +104,7 @@
 
   OBufferStream os;
   CryptoPP::FileSink sink(os);
-  
+
   // idCert ::= SEQUENCE {
   //     validity            Validity,
   //     subject             Name,
@@ -126,7 +124,7 @@
 
     // Name ::= CHOICE {
     //     RDNSequence   }
-    // 
+    //
     // RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
     DERSequenceEncoder name(idCert);
     {
@@ -137,7 +135,7 @@
         }
     }
     name.MessageEnd();
-  
+
     // SubjectPublicKeyInfo
     key_.encode(idCert);
 
@@ -151,7 +149,7 @@
       {
         DERSequenceEncoder extensions(idCert);
         {
-          
+
           for(ExtensionList::iterator it = extensionList_.begin();
               it != extensionList_.end(); ++it)
             {
@@ -168,14 +166,14 @@
   setContentType(MetaInfo::TYPE_KEY);
 }
 
-void 
+void
 Certificate::decode()
 {
   using namespace CryptoPP;
 
   OBufferStream os;
   CryptoPP::StringSource source(getContent().value(), getContent().value_size(), true);
-  
+
   // idCert ::= SEQUENCE {
   //     validity            Validity,
   //     subject             Name,
@@ -195,7 +193,7 @@
 
     // Name ::= CHOICE {
     //     RDNSequence   }
-    // 
+    //
     // RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
     subjectDescriptionList_.clear();
     BERSequenceDecoder name(idCert);
@@ -206,7 +204,7 @@
         }
     }
     name.MessageEnd();
-  
+
     // SubjectPublicKeyInfo ::= SEQUENCE {
     //     algorithm           AlgorithmIdentifier
     //     keybits             BIT STRING   }
@@ -235,18 +233,18 @@
   idCert.MessageEnd();
 }
 
-void 
+void
 Certificate::printCertificate(std::ostream &os) const
 {
   os << "Certificate name:" << endl;
   os << "  " << getName() << endl;
   os << "Validity:" << endl;
   {
-    os << "  NotBefore: " << toIsoString(notBefore_) << endl;
-    os << "  NotAfter: "  << toIsoString(notAfter_)  << endl;
+    os << "  NotBefore: " << time::toIsoString(notBefore_) << endl;
+    os << "  NotAfter: "  << time::toIsoString(notAfter_)  << endl;
   }
 
-  os << "Subject Description:" << endl;  
+  os << "Subject Description:" << endl;
   for(SubjectDescriptionList::const_iterator it = subjectDescriptionList_.begin();
       it != subjectDescriptionList_.end(); ++it)
     {
@@ -256,7 +254,7 @@
   os << "Public key bits:" << endl;
   CryptoPP::Base64Encoder encoder(new CryptoPP::FileSink(os), true, 64);
   key_.encode(encoder);
-  
+
   // ndnboost::iostreams::stream<ndnboost::iostreams::array_source> is((const char*)key_.getKeyDer().buf(), key_.getKeyDer().size());
 
   // ptr_lib::shared_ptr<der::DerNode> keyRoot = der::DerNode::parse(reinterpret_cast<der::InputIterator&> (is));
diff --git a/src/security/certificate.hpp b/src/security/certificate.hpp
index 35934d0..d27f7a4 100644
--- a/src/security/certificate.hpp
+++ b/src/security/certificate.hpp
@@ -35,16 +35,16 @@
    * @param data The data packet with the content to decode.
    */
   Certificate(const Data& data);
- 
+
   /**
    * The virtual destructor.
    */
-  virtual 
+  virtual
   ~Certificate();
 
   inline void
   wireDecode(const Block &wire);
-  
+
   /**
    * encode certificate info into content
    */
@@ -55,63 +55,63 @@
    * Add a subject description.
    * @param description The description to be added.
    */
-  void 
+  void
   addSubjectDescription(const CertificateSubjectDescription& description) { subjectDescriptionList_.push_back(description); }
 
-  const SubjectDescriptionList& 
+  const SubjectDescriptionList&
   getSubjectDescriptionList() const { return subjectDescriptionList_; }
-  
-  SubjectDescriptionList& 
+
+  SubjectDescriptionList&
   getSubjectDescriptionList() { return subjectDescriptionList_; }
- 
+
   /**
    * Add a certificate extension.
    * @param extension the extension to be added
    */
-  void 
+  void
   addExtension(const CertificateExtension& extension) { extensionList_.push_back(extension); }
 
   const ExtensionList&
   getExtensionList() const { return extensionList_; }
-  
+
   ExtensionList&
   getExtensionList() { return extensionList_; }
 
-  void 
-  setNotBefore(const MillisecondsSince1970& notBefore) { notBefore_ = notBefore; }
+  void
+  setNotBefore(const time::system_clock::TimePoint& notBefore) { notBefore_ = notBefore; }
 
-  MillisecondsSince1970& 
+  time::system_clock::TimePoint&
   getNotBefore() { return notBefore_; }
-  
-  const MillisecondsSince1970& 
+
+  const time::system_clock::TimePoint&
   getNotBefore() const { return notBefore_; }
 
   void
-  setNotAfter(const MillisecondsSince1970& notAfter) { notAfter_ = notAfter; }
+  setNotAfter(const time::system_clock::TimePoint& notAfter) { notAfter_ = notAfter; }
 
-  MillisecondsSince1970& 
+  time::system_clock::TimePoint&
   getNotAfter() { return notAfter_; }
 
-  const MillisecondsSince1970& 
+  const time::system_clock::TimePoint&
   getNotAfter() const { return notAfter_; }
 
   void
   setPublicKeyInfo(const PublicKey& key) { key_ = key; }
-  
-  PublicKey& 
+
+  PublicKey&
   getPublicKeyInfo() { return key_; }
 
-  const PublicKey& 
+  const PublicKey&
   getPublicKeyInfo() const { return key_; }
 
-  // virtual Name 
+  // virtual Name
   // getPublicKeyName() const = 0;
-  
+
   /**
    * Check if the certificate is valid.
    * @return True if the current time is earlier than notBefore.
    */
-  bool 
+  bool
   isTooEarly();
 
   /**
@@ -121,7 +121,7 @@
   bool
   isTooLate();
 
-  void 
+  void
   printCertificate(std::ostream &os) const;
 
 protected:
@@ -130,8 +130,8 @@
 
 protected:
   SubjectDescriptionList subjectDescriptionList_;
-  MillisecondsSince1970 notBefore_;
-  MillisecondsSince1970 notAfter_;
+  time::system_clock::TimePoint notBefore_;
+  time::system_clock::TimePoint notAfter_;
   PublicKey key_;
   ExtensionList extensionList_;
 };
diff --git a/src/security/key-chain.hpp b/src/security/key-chain.hpp
index 28642ae..e58cbf9 100644
--- a/src/security/key-chain.hpp
+++ b/src/security/key-chain.hpp
@@ -124,8 +124,8 @@
   shared_ptr<IdentityCertificate>
   prepareUnsignedIdentityCertificate(const Name& keyName,
                                      const Name& signingIdentity,
-                                     const MillisecondsSince1970& notBefore,
-                                     const MillisecondsSince1970& notAfter,
+                                     const time::system_clock::TimePoint& notBefore,
+                                     const time::system_clock::TimePoint& notAfter,
                                      const std::vector<CertificateSubjectDescription>& subjectDescription)
 
   {
@@ -360,8 +360,8 @@
     certificateName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion();
 
     certificate->setName(certificateName);
-    certificate->setNotBefore(getNow());
-    certificate->setNotAfter(getNow() + 630720000 /* 20 years*/);
+    certificate->setNotBefore(time::system_clock::now());
+    certificate->setNotAfter(time::system_clock::now() + time::days(7300)/* ~20 years*/);
     certificate->setPublicKeyInfo(*pubKey);
     certificate->addSubjectDescription(CertificateSubjectDescription("2.5.4.41", keyName.toUri()));
     certificate->encode();
diff --git a/src/security/sec-public-info-sqlite3.cpp b/src/security/sec-public-info-sqlite3.cpp
index 16041d3..aeb34dd 100644
--- a/src/security/sec-public-info-sqlite3.cpp
+++ b/src/security/sec-public-info-sqlite3.cpp
@@ -365,9 +365,11 @@
 //   sqlite3_bind_text(statement, 3, identityName, SQLITE_STATIC);
 //   sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
 
-//   // Convert from milliseconds to seconds since 1/1/1970.
-//   sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(certificate.getNotBefore() / 1000));
-//   sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(certificate.getNotAfter() / 1000));
+//   // Convert from time::milliseconds to time::seconds since 1/1/1970.
+// sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(
+//                                    time::toUnixTimestamp(certificate.getNotBefore()).count()));
+// sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(
+//                                    time::toUnixTimestamp(certificate.getNotAfter()).count()));
 
 //   sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_STATIC);
 
@@ -420,9 +422,10 @@
   sqlite3_bind_text(statement, 3, identity.toUri(), SQLITE_TRANSIENT);
   sqlite3_bind_text(statement, 4, keyId, SQLITE_STATIC);
 
-  // Convert from milliseconds to seconds since 1/1/1970.
-  sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(certificate.getNotBefore() / 1000));
-  sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(certificate.getNotAfter() / 1000));
+  sqlite3_bind_int64(statement, 5, static_cast<sqlite3_int64>(
+                                     time::toUnixTimestamp(certificate.getNotBefore()).count()));
+  sqlite3_bind_int64(statement, 6, static_cast<sqlite3_int64>(
+                                     time::toUnixTimestamp(certificate.getNotAfter()).count()));
 
   sqlite3_bind_blob(statement, 7, certificate.wireEncode().wire(), certificate.wireEncode().size(), SQLITE_TRANSIENT);
 
@@ -447,7 +450,8 @@
   if (res == SQLITE_ROW)
     {
       shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
-      certificate->wireDecode(Block((const uint8_t*)sqlite3_column_blob(statement, 0), sqlite3_column_bytes(statement, 0)));
+      certificate->wireDecode(Block((const uint8_t*)sqlite3_column_blob(statement, 0),
+                                    sqlite3_column_bytes(statement, 0)));
       sqlite3_finalize(statement);
       return certificate;
     }
diff --git a/src/security/sec-public-info.hpp b/src/security/sec-public-info.hpp
index e1e0fbe..0861b67 100644
--- a/src/security/sec-public-info.hpp
+++ b/src/security/sec-public-info.hpp
@@ -402,7 +402,7 @@
   else
     oss << "dsk-";
 
-  oss << getNow();
+  oss << time::toUnixTimestamp(time::system_clock::now()).count();
   
   Name keyName = Name(identityName).append(oss.str());