Adding schedule related classes: Schedule, RepetitiveInterval and Interval

Change-Id: Ia3fa750270f49c1b9e3bc4c6f67281c527910c55
Refs: #3118
diff --git a/src/interval.cpp b/src/interval.cpp
new file mode 100644
index 0000000..f5738a1
--- /dev/null
+++ b/src/interval.cpp
@@ -0,0 +1,114 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2015,  Regents of the University of California
+ *
+ * This file is part of ndn-group-encrypt (Group-based Encryption Protocol for NDN).
+ * See AUTHORS.md for complete list of ndn-group-encrypt authors and contributors.
+ *
+ * ndn-group-encrypt is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-group-encrypt is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndn-group-encrypt, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author Zhiyi Zhang <dreamerbarrychang@gmail.com>
+ */
+
+#include "interval.hpp"
+
+namespace ndn {
+namespace gep {
+
+static const TimeStamp DEFAULT_TIME = boost::posix_time::from_iso_string("14000101T000000");
+
+Interval::Interval(bool isValid)
+  : m_startTime(DEFAULT_TIME)
+  , m_endTime(DEFAULT_TIME)
+  , m_isValid(isValid)
+{
+}
+
+Interval::Interval(const TimeStamp& startTime,
+                   const TimeStamp& endTime)
+  : m_startTime(startTime)
+  , m_endTime(endTime)
+  , m_isValid(true)
+{
+  BOOST_ASSERT(startTime < endTime);
+}
+
+bool
+Interval::covers(const TimeStamp& tp) const
+{
+  BOOST_ASSERT(isValid());
+
+  if (isEmpty())
+    return false;
+  return (m_startTime <= tp && tp < m_endTime);
+}
+
+Interval&
+Interval::operator &&(const Interval& interval)
+{
+  BOOST_ASSERT(isValid() && interval.isValid());
+
+  // if one is empty, result is empty
+  if (isEmpty() || interval.isEmpty()) {
+    m_startTime = m_endTime;
+    return *this;
+  }
+  // two intervals do not have intersection
+  if (m_startTime >= interval.getEndTime() || m_endTime <= interval.getStartTime()) {
+    m_startTime = m_endTime;
+    return *this;
+  }
+
+  // get the start time
+  if (m_startTime <= interval.getStartTime())
+    m_startTime = interval.getStartTime();
+
+  // get the end time
+  if (m_endTime > interval.getEndTime())
+    m_endTime = interval.getEndTime();
+
+  return *this;
+}
+
+Interval&
+Interval::operator ||(const Interval& interval)
+{
+  BOOST_ASSERT(this->isValid() && interval.isValid());
+
+  if (isEmpty()) {
+    // left interval is empty, return left one
+    m_startTime = interval.getStartTime();
+    m_endTime = interval.getEndTime();
+    return *this;
+  }
+  if (interval.isEmpty()) {
+    // right interval is empty, return right one
+    return *this;
+  }
+  if (m_startTime >= interval.getEndTime() || m_endTime <= interval.getStartTime()) {
+    // two intervals do not have intersection
+    BOOST_THROW_EXCEPTION(Error("cannot generate a union interval when there's no intersection"));
+  }
+
+  // get the start time
+  if (m_startTime > interval.getStartTime())
+    m_startTime = interval.getStartTime();
+
+  // get the end time
+  if (m_endTime <= interval.getEndTime())
+    m_endTime = interval.getEndTime();
+
+  return *this;
+}
+
+} // namespace gep
+} // namespace ndn
diff --git a/src/interval.hpp b/src/interval.hpp
new file mode 100644
index 0000000..5764454
--- /dev/null
+++ b/src/interval.hpp
@@ -0,0 +1,124 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2015,  Regents of the University of California
+ *
+ * This file is part of ndn-group-encrypt (Group-based Encryption Protocol for NDN).
+ * See AUTHORS.md for complete list of ndn-group-encrypt authors and contributors.
+ *
+ * ndn-group-encrypt is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-group-encrypt is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndn-group-encrypt, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author Zhiyi Zhang <dreamerbarrychang@gmail.com>
+ */
+
+#ifndef NDN_GEP_INTERVAL_HPP
+#define NDN_GEP_INTERVAL_HPP
+
+#include "common.hpp"
+
+#include <boost/date_time/posix_time/posix_time.hpp>
+
+namespace ndn {
+namespace gep {
+
+typedef boost::posix_time::ptime TimeStamp;
+
+///@brief Interval define a time duration which contains a start timestamp and an end timestamp
+class Interval
+{
+public:
+  class Error : public std::runtime_error
+  {
+  public:
+    explicit
+    Error(const std::string& what)
+      : std::runtime_error(what)
+    {
+    }
+  };
+
+public:
+  /**
+   * @brief Construction to create an object
+   *
+   * @parameter isValid If isValid is true, the created interval is an empty interval
+   */
+  explicit
+  Interval(bool isValid = false);
+
+  Interval(const TimeStamp& startTime,
+           const TimeStamp& endTime);
+
+  /**
+   * @brief Check if the timestamp tp is in the interval
+   * @pre this->isValid() == true
+   *
+   * @parameter tp A timestamp
+   */
+  bool
+  covers(const TimeStamp& tp) const;
+
+  /**
+   * @brief Get the intersection interval of two intervals
+   * @pre this->isValid() == true && interval.isValid() == true
+   *
+   * Two intervals should all be valid but they can be empty
+   */
+  Interval&
+  operator &&(const Interval& interval);
+
+  /**
+   * @brief Get the union set interval of two intervals
+   * @pre this->isValid() == true && interval.isValid() == true
+   *
+   * Two intervals should all be valid but they can be empty
+   */
+  Interval&
+  operator ||(const Interval& interval);
+
+  const TimeStamp&
+  getStartTime() const
+  {
+    BOOST_ASSERT(isValid());
+    return m_startTime;
+  }
+
+  const TimeStamp&
+  getEndTime() const
+  {
+    BOOST_ASSERT(isValid());
+    return m_endTime;
+  }
+
+  bool
+  isValid() const
+  {
+    return m_isValid;
+  }
+
+  bool
+  isEmpty() const
+  {
+    BOOST_ASSERT(isValid());
+    return m_startTime == m_endTime;
+  }
+
+private:
+  TimeStamp m_startTime;
+  TimeStamp m_endTime;
+
+  bool m_isValid;
+};
+
+} // namespace gep
+} // namespace ndn
+
+#endif // NDN_GEP_INTERVAL_HPP
diff --git a/src/repetitive-interval.cpp b/src/repetitive-interval.cpp
new file mode 100644
index 0000000..2a441b5
--- /dev/null
+++ b/src/repetitive-interval.cpp
@@ -0,0 +1,275 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2015,  Regents of the University of California
+ *
+ * This file is part of ndn-group-encrypt (Group-based Encryption Protocol for NDN).
+ * See AUTHORS.md for complete list of ndn-group-encrypt authors and contributors.
+ *
+ * ndn-group-encrypt is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-group-encrypt is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndn-group-encrypt, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author Zhiyi Zhang <dreamerbarrychang@gmail.com>
+ */
+
+#include "repetitive-interval.hpp"
+#include "tlv.hpp"
+
+#include <ndn-cxx/encoding/block-helpers.hpp>
+#include <ndn-cxx/util/concepts.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
+
+namespace ndn {
+namespace gep {
+
+static const TimeStamp DEFAULT_TIME = boost::posix_time::from_iso_string("14000101T000000");
+
+BOOST_CONCEPT_ASSERT((WireEncodable<RepetitiveInterval>));
+BOOST_CONCEPT_ASSERT((WireDecodable<RepetitiveInterval>));
+
+RepetitiveInterval::RepetitiveInterval()
+  : m_startDate(DEFAULT_TIME)
+  , m_endDate(DEFAULT_TIME)
+  , m_intervalStartHour(0)
+  , m_intervalEndHour(24)
+  , m_nRepeats(0)
+  , m_unit(RepeatUnit::NONE)
+{
+}
+
+RepetitiveInterval::RepetitiveInterval(const Block& block)
+{
+  wireDecode(block);
+}
+
+RepetitiveInterval::RepetitiveInterval(const TimeStamp& startDate,
+                                       const TimeStamp& endDate,
+                                       size_t intervalStartHour,
+                                       size_t intervalEndHour,
+                                       size_t nRepeats,
+                                       RepeatUnit unit)
+  : m_startDate(startDate)
+  , m_endDate(endDate)
+  , m_intervalStartHour(intervalStartHour)
+  , m_intervalEndHour(intervalEndHour)
+  , m_nRepeats(nRepeats)
+  , m_unit(unit)
+{
+  BOOST_ASSERT(m_intervalStartHour < m_intervalEndHour);
+  BOOST_ASSERT(m_startDate.date() <= m_endDate.date());
+  BOOST_ASSERT(m_intervalEndHour <= 24);
+  if (unit == RepeatUnit::NONE)
+    BOOST_ASSERT(m_startDate.date() == m_endDate.date());
+}
+
+template<encoding::Tag TAG>
+size_t
+RepetitiveInterval::wireEncode(EncodingImpl<TAG>& encoder) const
+{
+  using namespace boost::posix_time;
+
+  size_t totalLength = 0;
+
+  // RepeatUnit
+  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::RepeatUnit,
+                                                  static_cast<size_t>(m_unit));
+  // NRepeat
+  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::NRepeats, m_nRepeats);
+  // IntervalEndHour
+  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::IntervalEndHour, m_intervalEndHour);
+  // IntervalStartHour
+  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::IntervalStartHour,
+                                                m_intervalStartHour);
+  // EndDate
+  totalLength += prependStringBlock(encoder, tlv::EndDate, to_iso_string(m_endDate));
+  // StartDate
+  totalLength += prependStringBlock(encoder, tlv::StartDate, to_iso_string(m_startDate));
+
+  totalLength += encoder.prependVarNumber(totalLength);
+  totalLength += encoder.prependVarNumber(tlv::RepetitiveInterval);
+
+  return totalLength;
+}
+
+const Block&
+RepetitiveInterval::wireEncode() const
+{
+  if (m_wire.hasWire())
+    return m_wire;
+
+  EncodingEstimator estimator;
+  size_t estimatedSize = wireEncode(estimator);
+
+  EncodingBuffer buffer(estimatedSize, 0);
+  wireEncode(buffer);
+
+  this->m_wire = buffer.block();
+  return m_wire;
+}
+
+void
+RepetitiveInterval::wireDecode(const Block& wire)
+{
+  using namespace boost::posix_time;
+
+  if (wire.type() != tlv::RepetitiveInterval)
+    BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding RepetitiveInterval"));
+
+  m_wire = wire;
+  m_wire.parse();
+
+  if (m_wire.elements_size() != 6)
+    BOOST_THROW_EXCEPTION(tlv::Error("RepetitiveInterval tlv does not have six sub-TLVs"));
+
+  Block::element_const_iterator it = m_wire.elements_begin();
+  // StartDate
+  if (it->type() == tlv::StartDate) {
+    m_startDate = ptime(from_iso_string(readString(*it)));
+    it++;
+  }
+  else
+    BOOST_THROW_EXCEPTION(tlv::Error("First element must be StartDate"));
+
+  // EndDate
+  if (it->type() == tlv::EndDate) {
+    m_endDate = ptime(from_iso_string(readString(*it)));
+    it++;
+  }
+  else
+    BOOST_THROW_EXCEPTION(tlv::Error("Second element must be EndDate"));
+
+  // IntervalStartHour
+  if (it->type() == tlv::IntervalStartHour) {
+    m_intervalStartHour = readNonNegativeInteger(*it);
+    it++;
+  }
+  else
+    BOOST_THROW_EXCEPTION(tlv::Error("Third element must be IntervalStartHour"));
+
+  // IntervalEndHour
+  if (it->type() == tlv::IntervalEndHour) {
+    m_intervalEndHour = readNonNegativeInteger(*it);
+    it++;
+  }
+  else
+    BOOST_THROW_EXCEPTION(tlv::Error("Fourth element must be IntervalEndHour"));
+
+  // NRepeats
+  if (it->type() == tlv::NRepeats) {
+    m_nRepeats = readNonNegativeInteger(*it);
+    it++;
+  }
+  else
+    BOOST_THROW_EXCEPTION(tlv::Error("Fifth element must be NRepeats"));
+
+  // RepeatUnit
+  if (it->type() == tlv::RepeatUnit) {
+    m_unit = static_cast<RepeatUnit>(readNonNegativeInteger(*it));
+    it++;
+  }
+  else
+    BOOST_THROW_EXCEPTION(tlv::Error("The last element must be RepeatUnit"));
+}
+
+std::tuple<bool, Interval>
+RepetitiveInterval::getInterval(const TimeStamp& tp) const
+{
+  TimeStamp startTime;
+  TimeStamp endTime;
+  bool isPositive;
+
+  if (!this->hasIntervalOnDate(tp)) {
+    // there is no interval on the date of tp
+    startTime = TimeStamp(tp.date(), boost::posix_time::hours(0));
+    endTime = TimeStamp(tp.date(), boost::posix_time::hours(24));
+    isPositive = false;
+  }
+  else {
+    // there is an interval on the date of tp
+    startTime = TimeStamp(tp.date(), boost::posix_time::hours(m_intervalStartHour));
+    endTime = TimeStamp(tp.date(), boost::posix_time::hours(m_intervalEndHour));
+
+    // check if in the time duration
+    if (tp < startTime) {
+      endTime = startTime;
+      startTime = TimeStamp(tp.date(), boost::posix_time::hours(0));
+      isPositive = false;
+    }
+    else if (tp > endTime) {
+      startTime = endTime;
+      endTime = TimeStamp(tp.date(), boost::posix_time::hours(24));
+      isPositive = false;
+    }
+    else {
+      isPositive = true;
+    }
+  }
+  return std::make_tuple(isPositive, Interval(startTime, endTime));
+}
+
+bool
+RepetitiveInterval::hasIntervalOnDate(const TimeStamp& tp) const
+{
+  namespace bg = boost::gregorian;
+
+  // check if in the bound of the interval
+  if (tp.date() < m_startDate.date() || tp.date() > m_endDate.date()) {
+    return false;
+  }
+
+  if (m_unit == RepeatUnit::NONE) {
+    return true;
+  }
+
+  // check if in the matching date
+  bg::date dateA = tp.date();
+  bg::date dateB = m_startDate.date();
+  if (m_unit == RepeatUnit::DAY) {
+    bg::date_duration duration = dateA - dateB;
+    if (static_cast<size_t>(duration.days()) % m_nRepeats == 0)
+      return true;
+  }
+  else if (m_unit == RepeatUnit::MONTH && dateA.day() == dateB.day()) {
+    size_t yearDiff = static_cast<size_t>(dateA.year() - dateB.year());
+    size_t monthDiff = 12 * yearDiff + dateA.month().as_number() - dateB.month().as_number();
+    if (monthDiff % m_nRepeats == 0)
+      return true;
+  }
+  else if (m_unit == RepeatUnit::YEAR &&
+           dateA.day().as_number() == dateB.day().as_number() &&
+           dateA.month().as_number() == dateB.month().as_number()) {
+    size_t diff = static_cast<size_t>(dateA.year() - dateB.year());
+    if (diff % m_nRepeats == 0)
+      return true;
+  }
+
+  return false;
+}
+
+bool
+RepetitiveInterval::operator<(const RepetitiveInterval& interval) const
+{
+  if (m_startDate < interval.getStartDate())
+    return true;
+  if (m_endDate < interval.getEndDate())
+    return true;
+  if (m_intervalStartHour < interval.getIntervalStartHour())
+    return true;
+  if (m_intervalEndHour < interval.getIntervalEndHour())
+    return true;
+  if (m_nRepeats < interval.getNRepeats())
+    return true;
+  if (static_cast<size_t>(m_unit) < static_cast<size_t>(interval.getRepeatUnit()))
+    return true;
+  return false;
+}
+
+} // namespace gep
+} // namespace ndn
diff --git a/src/repetitive-interval.hpp b/src/repetitive-interval.hpp
new file mode 100644
index 0000000..1e8127f
--- /dev/null
+++ b/src/repetitive-interval.hpp
@@ -0,0 +1,145 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2015,  Regents of the University of California
+ *
+ * This file is part of ndn-group-encrypt (Group-based Encryption Protocol for NDN).
+ * See AUTHORS.md for complete list of ndn-group-encrypt authors and contributors.
+ *
+ * ndn-group-encrypt is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-group-encrypt is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndn-group-encrypt, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author Zhiyi Zhang <dreamerbarrychang@gmail.com>
+ */
+
+#ifndef NDN_GEP_REPETITIVE_INTERVAL_HPP
+#define NDN_GEP_REPETITIVE_INTERVAL_HPP
+
+#include "common.hpp"
+#include "interval.hpp"
+
+namespace ndn {
+namespace gep {
+
+///@brief An advanced interval which can have a repeat pattern and repeat unit
+class RepetitiveInterval
+{
+public:
+  enum class RepeatUnit{
+    NONE = 0,
+    DAY = 1,
+    MONTH = 2,
+    YEAR = 3
+  };
+
+public:
+  RepetitiveInterval();
+
+  explicit
+  RepetitiveInterval(const Block& block);
+
+  /**
+   * @brief Construction to create an object
+   * @pre @p startDate <= @p endDate
+   * @pre @p intervalStartHour and @p intervalEndHour can be [0, 24]
+   * @pre @p intervalStartHour < @p intervalEndHour
+   * @pre when @p unit = NONE, then @p startDate == @p endDate
+   */
+  RepetitiveInterval(const TimeStamp& startDate,
+                     const TimeStamp& endDate,
+                     size_t intervalStartHour,
+                     size_t intervalEndHour,
+                     size_t nRepeats = 0,
+                     RepeatUnit unit = RepeatUnit::NONE);
+
+  template<encoding::Tag TAG>
+  size_t
+  wireEncode(EncodingImpl<TAG>& encoder) const;
+
+  const Block&
+  wireEncode() const;
+
+  void
+  wireDecode(const Block& wire);
+
+  /**
+   * @brief Get get an interval that @p tp falls in
+   *
+   * @parameter tp A timestamp
+   *
+   * @return bool If the repetitive interval covers the @p tp, return true, otherwise false
+   * @return Interval Return the interval which @tp falls in
+   */
+  std::tuple<bool, Interval>
+  getInterval(const TimeStamp& tp) const;
+
+  /**
+   * @brief To store in std::set, class have to implement operator <
+   *
+   * @parameter interval Interval which will be compared with
+   */
+  bool
+  operator<(const RepetitiveInterval& interval) const;
+
+  const TimeStamp&
+  getStartDate() const
+  {
+    return m_startDate;
+  }
+
+  const TimeStamp&
+  getEndDate() const
+  {
+    return m_endDate;
+  }
+
+  size_t
+  getIntervalStartHour() const
+  {
+    return m_intervalStartHour;
+  }
+
+  size_t
+  getIntervalEndHour() const
+  {
+    return m_intervalEndHour;
+  }
+
+  size_t
+  getNRepeats() const
+  {
+    return m_nRepeats;
+  }
+
+  RepeatUnit
+  getRepeatUnit() const
+  {
+    return m_unit;
+  }
+
+private:
+  ///@brief Check if there is any interval in the date of timestamp
+  bool
+  hasIntervalOnDate(const TimeStamp& tp) const;
+
+  TimeStamp m_startDate;
+  TimeStamp m_endDate;
+  size_t m_intervalStartHour;
+  size_t m_intervalEndHour;
+  size_t m_nRepeats;
+  RepeatUnit m_unit;
+
+  mutable Block m_wire;
+};
+
+} // namespace gep
+} // namespace ndn
+
+#endif // NDN_GEP_REPETITIVE_INTERVAL_HPP
diff --git a/src/schedule.cpp b/src/schedule.cpp
new file mode 100644
index 0000000..301e0fe
--- /dev/null
+++ b/src/schedule.cpp
@@ -0,0 +1,176 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2015,  Regents of the University of California
+ *
+ * This file is part of ndn-group-encrypt (Group-based Encryption Protocol for NDN).
+ * See AUTHORS.md for complete list of ndn-group-encrypt authors and contributors.
+ *
+ * ndn-group-encrypt is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-group-encrypt is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndn-group-encrypt, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author Zhiyi Zhang <dreamerbarrychang@gmail.com>
+ */
+
+#include "schedule.hpp"
+#include "tlv.hpp"
+
+#include <ndn-cxx/encoding/block-helpers.hpp>
+#include <ndn-cxx/util/concepts.hpp>
+
+namespace ndn {
+namespace gep {
+
+BOOST_CONCEPT_ASSERT((WireEncodable<Schedule>));
+BOOST_CONCEPT_ASSERT((WireDecodable<Schedule>));
+
+Schedule::Schedule() = default;
+
+Schedule::Schedule(const Block& block)
+{
+  wireDecode(block);
+}
+
+template<encoding::Tag TAG>
+size_t
+Schedule::wireEncode(EncodingImpl<TAG>& encoder) const
+{
+  size_t totalLength = 0;
+  size_t blackLength = 0;
+  size_t whiteLength = 0;
+
+  // encode the blackIntervalList as an embed TLV structure
+  for (const RepetitiveInterval& element : m_blackIntervalList) {
+    blackLength += encoder.prependBlock(element.wireEncode());
+  }
+  blackLength += encoder.prependVarNumber(blackLength);
+  blackLength += encoder.prependVarNumber(tlv::BlackIntervalList);
+
+  // encode the whiteIntervalList as an embed TLV structure
+  for (const RepetitiveInterval& element : m_whiteIntervalList) {
+    whiteLength += encoder.prependBlock(element.wireEncode());
+  }
+  whiteLength += encoder.prependVarNumber(whiteLength);
+  whiteLength += encoder.prependVarNumber(tlv::WhiteIntervalList);
+
+  totalLength = whiteLength + blackLength;
+  totalLength += encoder.prependVarNumber(totalLength);
+  totalLength += encoder.prependVarNumber(tlv::Schedule);
+
+  return totalLength;
+}
+
+const Block&
+Schedule::wireEncode() const
+{
+  if (m_wire.hasWire())
+    return m_wire;
+
+  EncodingEstimator estimator;
+  size_t estimatedSize = wireEncode(estimator);
+
+  EncodingBuffer buffer(estimatedSize, 0);
+  wireEncode(buffer);
+
+  this->m_wire = buffer.block();
+  return m_wire;
+}
+
+void
+Schedule::wireDecode(const Block& wire)
+{
+  if (wire.type() != tlv::Schedule)
+    BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding RepetitiveInterval"));
+
+  m_wire = wire;
+  m_wire.parse();
+
+  if (m_wire.elements_size() != 2)
+    BOOST_THROW_EXCEPTION(tlv::Error("RepetitiveInterval tlv does not have two sub-TLVs"));
+
+  Block::element_const_iterator it = m_wire.elements_begin();
+
+  if (it != m_wire.elements_end() && it->type() == tlv::WhiteIntervalList) {
+    it->parse();
+    Block::element_const_iterator tempIt = it->elements_begin();
+    while (tempIt != it->elements_end() && tempIt->type() == tlv::RepetitiveInterval) {
+      m_whiteIntervalList.insert(RepetitiveInterval(*tempIt));
+      tempIt++;
+    }
+    it++;
+  }
+  else
+    BOOST_THROW_EXCEPTION(tlv::Error("The first element must be WhiteIntervalList"));
+
+  if (it != m_wire.elements_end() && it->type() == tlv::BlackIntervalList) {
+    it->parse();
+    Block::element_const_iterator tempIt = it->elements_begin();
+    while (tempIt != it->elements_end() && tempIt->type() == tlv::RepetitiveInterval) {
+      m_blackIntervalList.insert(RepetitiveInterval(*tempIt));
+      tempIt++;
+    }
+    it++;
+  }
+  else
+    BOOST_THROW_EXCEPTION(tlv::Error("The second element must be BlackIntervalList"));
+}
+
+Schedule&
+Schedule::addWhiteInterval(const RepetitiveInterval& repetitiveInterval)
+{
+  m_whiteIntervalList.insert(repetitiveInterval);
+  return *this;
+}
+
+Schedule&
+Schedule::addBlackInterval(const RepetitiveInterval& repetitiveInterval)
+{
+  m_blackIntervalList.insert(repetitiveInterval);
+  return *this;
+}
+
+Interval
+Schedule::getCoveringInterval(const TimeStamp& tp) const
+{
+  Interval blackResult;
+  Interval whiteResult(true);
+  Interval tempInterval;
+  bool isPositive;
+
+  // get the blackResult
+  for (const RepetitiveInterval& element : m_blackIntervalList) {
+    std::tie(isPositive, tempInterval) = element.getInterval(tp);
+    if (isPositive == true) {
+      // tempInterval is a black repetitive interval covering the time stamp, return empty interval
+      return Interval(true);
+    }
+    else {
+      // tempInterval is not covering the time stamp, && the tempInterval to the blackResult
+      if (!blackResult.isValid())
+        blackResult = tempInterval;
+      else
+        blackResult && tempInterval;
+    }
+  }
+
+  // get the whiteResult
+  for (const RepetitiveInterval& element : m_whiteIntervalList) {
+    std::tie(isPositive, tempInterval) = element.getInterval(tp);
+    if (isPositive == true) {
+      // tempInterval is a white repetitive interval covering the time stamp, || to the white result
+      whiteResult || tempInterval;
+    }
+  }
+
+  return whiteResult && blackResult;
+}
+
+} // namespace gep
+} // namespace ndn
diff --git a/src/schedule.hpp b/src/schedule.hpp
new file mode 100644
index 0000000..80ecb32
--- /dev/null
+++ b/src/schedule.hpp
@@ -0,0 +1,83 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2015,  Regents of the University of California
+ *
+ * This file is part of ndn-group-encrypt (Group-based Encryption Protocol for NDN).
+ * See AUTHORS.md for complete list of ndn-group-encrypt authors and contributors.
+ *
+ * ndn-group-encrypt is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-group-encrypt is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndn-group-encrypt, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author Zhiyi Zhang <dreamerbarrychang@gmail.com>
+ */
+
+#ifndef NDN_GEP_SECHEDULE_HPP
+#define NDN_GEP_SECHEDULE_HPP
+
+#include "common.hpp"
+#include "repetitive-interval.hpp"
+
+namespace ndn {
+namespace gep {
+
+/**
+ * @brief Schedule is used to manage the time, which contains two sets of RepetitiveIntervals
+ *
+ * whiteIntervalList is used to define the time allowing member's access to data
+ * blackIntervalList is used to define the time not allowing member's access to data
+ */
+class Schedule
+{
+public:
+  Schedule();
+
+  explicit
+  Schedule(const Block& block);
+
+public:
+  template<encoding::Tag TAG>
+  size_t
+  wireEncode(EncodingImpl<TAG>& encoder) const;
+
+  const Block&
+  wireEncode() const;
+
+  void
+  wireDecode(const Block& wire);
+
+  ///@brief Add an RepetitiveInterval @p repetitiveInterval into white list
+  Schedule&
+  addWhiteInterval(const RepetitiveInterval& repetitiveInterval);
+
+  ///@brief Add the RepetitiveInterval into black list
+  Schedule&
+  addBlackInterval(const RepetitiveInterval& repetitiveInterval);
+
+  /**
+   * @brief Get the Interval that covers the @p ts
+   *
+   * Function iterates two repetitive interval sets and find out
+   * the shortest interval that allows group member to have the access to the data
+   */
+  Interval
+  getCoveringInterval(const TimeStamp& ts) const;
+
+private:
+  std::set<RepetitiveInterval> m_whiteIntervalList;
+  std::set<RepetitiveInterval> m_blackIntervalList;
+
+  mutable Block m_wire;
+};
+
+} // namespace gep
+} // namespace ndn
+
+#endif // NDN_GROUP_ENCRYPT_SECHEDULE_HPP
diff --git a/src/tlv.hpp b/src/tlv.hpp
index a94af47..5c0c6ef 100644
--- a/src/tlv.hpp
+++ b/src/tlv.hpp
@@ -30,7 +30,21 @@
   EncryptedContent = 130,
   EncryptionAlgorithm = 131,
   EncryptedPayload = 132,
-  InitialVector = 133
+  InitialVector = 133,
+
+  // for repetitive interval
+  StartDate = 134,
+  EndDate = 135,
+  IntervalStartHour = 136,
+  IntervalEndHour = 137,
+  NRepeats = 138,
+  RepeatUnit = 139,
+  RepetitiveInterval = 140,
+
+  // for schedule
+  WhiteIntervalList = 141,
+  BlackIntervalList = 142,
+  Schedule = 143
 };
 
 enum AlgorithmTypeValue {