# Time Iterators

Introduction -- Header -- Overview -- Operators

Introduction

Time iterators provide a mechanism for iteration through times. Time iterators are similar to Bidirectional Iterators. However, time_iterators are different than standard iterators in that there is no underlying sequence, just a calculation function. In addition, time_iterators are directly comparable against instances of class ptime. Thus a second iterator for the end point of the iteration is not required, but rather a point in time can be used directly. For example, the following code iterates using a 15 minute iteration interval. The print hours example also illustrates the use of the time_iterator.

    	#include "boost/date_time/posix_time/posix_time.hpp"
    	#include <iostream>

    	int
    	main()
    	{
    	  using namespace boost::gregorian;
    	  using namespace boost::posix_time;
    	  date d(2000,Jan,20);
    	  ptime start(d);
    	  ptime end = start + hours(1);
    	  time_iterator titr(start,minutes(15)); //increment by 15 minutes
    	  //produces 00:00:00, 00:15:00, 00:30:00, 00:45:00
    	  while (titr < end) {
    	    std::cout << to_simple_string(*titr) << std::endl;
    	    ++titr;
    	  }
    	  std::cout << "Now backward" << std::endl;
    	  //produces 01:00:00, 00:45:00, 00:30:00, 00:15:00
    	  while (titr > start) {
    	    std::cout << to_simple_string(*titr) << std::endl;
    	    --titr;
    	  }
    	}

Header

    #include "boost/date_time/posix_time/posix_time.hpp" //include all types plus i/o
    or
    #include "boost/date_time/posix_time/posix_time_types.hpp" //no i/o just types

Overview

Class | Description
--- | ---
Construction Parameters

    time_iterator

Iterate incrementing by the specified duration.

    ptime start_time, time_duration increment

Operators

Syntax | Description
--- | ---
Example

    operator==(const ptime& rhs),
    operator!=(const ptime& rhs),
    operator>, operator<,
    operator>=, operator<=

A full complement of comparison operators

    date d(2002,Jan,1);
    ptime start_time(d, hours(1));
    //increment by 10 minutes
    time_iterator titr(start_time, minutes(10));
    ptime end_time = start_time + hours(2);
    if (titr == end_time) // false
    if (titr != end_time) // true
    if (titr >= end_time) // false
    if (titr <= end_time) // true

    prefix increment

Increment the iterator by the specified duration.

    //increment by 10 milli seconds
    time_iterator titr(start_time, milliseconds(10));
    ++titr; // == start_time + 10 milliseconds

    prefix decrement

Decrement the iterator by the specified time duration.

    time_duration td(1,2,3);
    time_iterator titr(start_time, td);
    --titr; // == start_time - 01:02:03