| Home | Libraries | People | FAQ | More

## General Usage Examples

The following provides some sample usage of dates. See Date Programming for more details.

    using namespace boost::gregorian;
        date week_start(2002,Feb,1);
        date week_finish  = week_start + week(1);
        date d2 = d1 + days(5);
        date today = day_clock::local_day();
        if (d2 >= today) {} //date comparison operators

        date_period thisWeek(d1,d2);
        if (thisWeek.contains(today)) {}//do something

        //iterate and print the week
        day_iterator itr(week_start);
        while (itr <= week_finish) {
         std::cout << (*itr) << std::endl;
         ++itr;
        }
        //input streaming
        std::stringstream ss("2004-Jan-1");
        ss >> d3;

        //date generator functions
        date d5 = next_weekday(d4, Sunday); //calculate Sunday following d4

        //US labor day is first Monday in Sept
        nth_day_of_the_week_in_month labor_day(nth_dow::first,Monday, Sep);
        //calculate a specific date for 2004 from functor
        date d6 = labor_day.get_date(2004);

The following provides some example code using times. See Time Programming for more details.

    using namespace boost::posix_time;
        date d(2002,Feb,1); //an arbitrary date
        ptime t1(d, hours(5)+nanosec(100)); //date + time of day offset
        ptime t2 = t1 - minutes(4)+seconds(2);
        ptime now = second_clock::local_time(); //use the clock
        date today = now.date(); //Get the date part out of the time
        date tomorrow = today + date_duration(1);
        ptime tomorrow_start(tomorrow); //midnight

        //input streaming
        std::stringstream ss("2004-Jan-1 05:21:33.20");
        ss >> t2;

        //starting at current time iterator adds by one hour
        time_iterator titr(now,hours(1));
        for (; titr < tomorrow_start; ++titr) {
         std::cout << (*titr) << std::endl;
        }

The following provides some example code using times. See Local Time Programming for more details.

        using namespace boost::local_time;
        //setup some timezones for creating and adjusting times
        //first time zone uses the time zone file for regional timezone definitions
        tz_database tz_db;
        tz_db.load_from_file("date_time_zonespec.csv");
        time_zone_ptr nyc_tz = tz_db.time_zone_from_region("America/New_York");
        //This timezone uses a posix time zone string definition to create a time zone
        time_zone_ptr phx_tz(new posix_time_zone("MST-07:00:00"));

        //local departure time in phoenix is 11 pm on April 2 2005
        // Note that New York changes to daylight savings on Apr 3 at 2 am)
        local_date_time phx_departure(date(2005, Apr, 2), hours(23), phx_tz,
                                      local_date_time::NOT_DATE_TIME_ON_ERROR);

        time_duration flight_length = hours(4) + minutes(30);
        local_date_time phx_arrival = phx_departure + flight_length;
        //convert the phx time to a nyz time
        local_date_time nyc_arrival = phx_arrival.local_time_in(nyc_tz);

        //2005-Apr-03 06:30:00 EDT
        std::cout << nyc_arrival << std::endl;

| Copyright © 2001-2005 CrystalClear Software, Inc