• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<?xml version="1.0" encoding="utf-8"?>
2<!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN"
3"../../../tools/boostbook/dtd/boostbook.dtd">
4
5<!-- Copyright (c) 2001-2004 CrystalClear Software, Inc.
6     Subject to the Boost Software License, Version 1.0.
7     (See accompanying file LICENSE_1_0.txt or  http://www.boost.org/LICENSE_1_0.txt)
8-->
9
10<section id="date_time.examples.days_between_new_year">
11  <title>Days Between New Years</title>
12
13  <para>
14    Calculate the number of days till new years
15  </para>
16  <programlisting>
17    <![CDATA[
18  /* Provides a simple example of using a date_generator, and simple
19   * mathematical operatorations, to calculate the days since
20   * New Years day of this year, and days until next New Years day.
21   *
22   * Expected results:
23   * Adding together both durations will produce 366 (365 in a leap year).
24   */
25  #include <iostream>
26  #include "boost/date_time/gregorian/gregorian.hpp"
27
28  int
29  main()
30  {
31
32    using namespace boost::gregorian;
33
34    date today = day_clock::local_day();
35    partial_date new_years_day(1,Jan);
36    //Subtract two dates to get a duration
37    days days_since_year_start = today - new_years_day.get_date(today.year());
38    std::cout << "Days since Jan 1: " << days_since_year_start.days()
39              << std::endl;
40
41    days days_until_year_start = new_years_day.get_date(today.year()+1) - today;
42    std::cout << "Days until next Jan 1: " << days_until_year_start.days()
43              << std::endl;
44    return 0;
45  };
46
47    ]]>
48  </programlisting>
49</section>
50