Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

PrevUpHomeNext

Class template date

boost::date_time::date — Representation of timepoint at the one day level resolution.

Synopsis

// In header: <boost/date_time/date.hpp>

template<typename T, typename calendar, typename duration_type_> 
class date :
  private boost::less_than_comparable< T, boost::equality_comparable< T > >
{
public:
  // types
  typedef T                          date_type;       
  typedef calendar                   calendar_type;   
  typedef calendar::date_traits_type traits_type;     
  typedef duration_type_             duration_type;   
  typedef calendar::year_type        year_type;       
  typedef calendar::month_type       month_type;      
  typedef calendar::day_type         day_type;        
  typedef calendar::ymd_type         ymd_type;        
  typedef calendar::date_rep_type    date_rep_type;   
  typedef calendar::date_int_type    date_int_type;   
  typedef calendar::day_of_week_type day_of_week_type;

  // construct/copy/destruct
  date(year_type, month_type, day_type);
  date(const ymd_type &);
  explicit date(date_int_type);
  explicit date(date_rep_type);

  // public member functions
  BOOST_CXX14_CONSTEXPR year_type year() const;
  BOOST_CXX14_CONSTEXPR month_type month() const;
  BOOST_CXX14_CONSTEXPR day_type day() const;
  BOOST_CXX14_CONSTEXPR day_of_week_type day_of_week() const;
  BOOST_CXX14_CONSTEXPR ymd_type year_month_day() const;
  BOOST_CONSTEXPR bool operator<(const date_type &) const;
  BOOST_CONSTEXPR bool operator==(const date_type &) const;
  BOOST_CONSTEXPR bool is_special() const;
  BOOST_CONSTEXPR bool is_not_a_date() const;
  BOOST_CONSTEXPR bool is_infinity() const;
  BOOST_CONSTEXPR bool is_pos_infinity() const;
  BOOST_CONSTEXPR bool is_neg_infinity() const;
  BOOST_CXX14_CONSTEXPR special_values as_special() const;
  BOOST_CXX14_CONSTEXPR duration_type operator-(const date_type &) const;
  BOOST_CXX14_CONSTEXPR date_type operator-(const duration_type &) const;
  BOOST_CXX14_CONSTEXPR date_type operator-=(const duration_type &);
  BOOST_CONSTEXPR date_rep_type day_count() const;
  BOOST_CXX14_CONSTEXPR date_type operator+(const duration_type &) const;
  BOOST_CXX14_CONSTEXPR date_type operator+=(const duration_type &);
};

Description

The date template represents an interface shell for a date class that is based on a year-month-day system such as the gregorian or ISO 8601 systems. It provides basic operations to enable calculation and comparisons.

Theory

This date representation fundamentally departs from the C tm struct approach. The goal for this type is to provide efficient date operations (add, subtract) and storage (minimize space to represent) in a concrete class. Thus, the date uses a count internally to represent a particular date. The calendar parameter defines the policies for converting the the year-month-day and internal counted form here. Applications that need to perform heavy formatting of the same date repeatedly will perform better by using the year-month-day representation.

Internally the date uses a day number to represent the date. This is a monotonic time representation. This representation allows for fast comparison as well as simplifying the creation of writing numeric operations. Essentially, the internal day number is like adjusted julian day. The adjustment is determined by the Epoch date which is represented as day 1 of the calendar. Day 0 is reserved for negative infinity so that any actual date is automatically greater than negative infinity. When a date is constructed from a date or formatted for output, the appropriate conversions are applied to create the year, month, day representations.

date public construct/copy/destruct

  1. date(year_type y, month_type m, day_type d);
  2. date(const ymd_type & ymd);
  3. explicit date(date_int_type days);

    This is a private constructor which allows for the creation of new dates. It is not exposed to users since that would require class users to understand the inner workings of the date class.

  4. explicit date(date_rep_type days);

date public member functions

  1. BOOST_CXX14_CONSTEXPR year_type year() const;
  2. BOOST_CXX14_CONSTEXPR month_type month() const;
  3. BOOST_CXX14_CONSTEXPR day_type day() const;
  4. BOOST_CXX14_CONSTEXPR day_of_week_type day_of_week() const;
  5. BOOST_CXX14_CONSTEXPR ymd_type year_month_day() const;
  6. BOOST_CONSTEXPR bool operator<(const date_type & rhs) const;
  7. BOOST_CONSTEXPR bool operator==(const date_type & rhs) const;
  8. BOOST_CONSTEXPR bool is_special() const;
    check to see if date is a special value
  9. BOOST_CONSTEXPR bool is_not_a_date() const;
    check to see if date is not a value
  10. BOOST_CONSTEXPR bool is_infinity() const;
    check to see if date is one of the infinity values
  11. BOOST_CONSTEXPR bool is_pos_infinity() const;
    check to see if date is greater than all possible dates
  12. BOOST_CONSTEXPR bool is_neg_infinity() const;
    check to see if date is greater than all possible dates
  13. BOOST_CXX14_CONSTEXPR special_values as_special() const;
    return as a special value or a not_special if a normal date
  14. BOOST_CXX14_CONSTEXPR duration_type operator-(const date_type & d) const;
  15. BOOST_CXX14_CONSTEXPR date_type operator-(const duration_type & dd) const;
  16. BOOST_CXX14_CONSTEXPR date_type operator-=(const duration_type & dd);
  17. BOOST_CONSTEXPR date_rep_type day_count() const;
  18. BOOST_CXX14_CONSTEXPR date_type operator+(const duration_type & dd) const;
  19. BOOST_CXX14_CONSTEXPR date_type operator+=(const duration_type & dd);

PrevUpHomeNext