-
Notifications
You must be signed in to change notification settings - Fork 745
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tim Blechmann <tim@klingt.org>
- Loading branch information
1 parent
04fcbe3
commit 2362cbe
Showing
70 changed files
with
9,651 additions
and
3 deletions.
There are no files selected for viewing
178 changes: 178 additions & 0 deletions
178
external_libraries/boost/boost/date_time/adjust_functors.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
#ifndef _DATE_TIME_ADJUST_FUNCTORS_HPP___ | ||
#define _DATE_TIME_ADJUST_FUNCTORS_HPP___ | ||
|
||
/* Copyright (c) 2002,2003 CrystalClear Software, Inc. | ||
* Use, modification and distribution is subject to the | ||
* Boost Software License, Version 1.0. (See accompanying | ||
* file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) | ||
* Author: Jeff Garland, Bart Garst | ||
* $Date: 2008-02-27 15:00:24 -0500 (Wed, 27 Feb 2008) $ | ||
*/ | ||
|
||
#include "boost/date_time/date.hpp" | ||
#include "boost/date_time/wrapping_int.hpp" | ||
|
||
namespace boost { | ||
namespace date_time { | ||
|
||
|
||
//! Functor to iterate a fixed number of days | ||
template<class date_type> | ||
class day_functor | ||
{ | ||
public: | ||
typedef typename date_type::duration_type duration_type; | ||
day_functor(int f) : f_(f) {} | ||
duration_type get_offset(const date_type& d) const | ||
{ | ||
// why is 'd' a parameter??? | ||
// fix compiler warnings | ||
d.year(); | ||
return duration_type(f_); | ||
} | ||
duration_type get_neg_offset(const date_type& d) const | ||
{ | ||
// fix compiler warnings | ||
d.year(); | ||
return duration_type(-f_); | ||
} | ||
private: | ||
int f_; | ||
}; | ||
|
||
|
||
//! Provides calculation to find next nth month given a date | ||
/*! This adjustment function provides the logic for 'month-based' | ||
* advancement on a ymd based calendar. The policy it uses | ||
* to handle the non existant end of month days is to back | ||
* up to the last day of the month. Also, if the starting | ||
* date is the last day of a month, this functor will attempt | ||
* to adjust to the end of the month. | ||
*/ | ||
template<class date_type> | ||
class month_functor | ||
{ | ||
public: | ||
typedef typename date_type::duration_type duration_type; | ||
typedef typename date_type::calendar_type cal_type; | ||
typedef typename cal_type::ymd_type ymd_type; | ||
typedef typename cal_type::day_type day_type; | ||
|
||
month_functor(int f) : f_(f), origDayOfMonth_(0) {} | ||
duration_type get_offset(const date_type& d) const | ||
{ | ||
ymd_type ymd(d.year_month_day()); | ||
if (origDayOfMonth_ == 0) { | ||
origDayOfMonth_ = ymd.day; | ||
day_type endOfMonthDay(cal_type::end_of_month_day(ymd.year,ymd.month)); | ||
if (endOfMonthDay == ymd.day) { | ||
origDayOfMonth_ = -1; //force the value to the end of month | ||
} | ||
} | ||
typedef date_time::wrapping_int2<short,1,12> wrap_int2; | ||
typedef typename wrap_int2::int_type int_type; | ||
wrap_int2 wi(ymd.month); | ||
//calc the year wrap around, add() returns 0 or 1 if wrapped | ||
int_type year = wi.add(static_cast<int_type>(f_)); | ||
year = static_cast<int_type>(year + ymd.year); //calculate resulting year | ||
// std::cout << "trace wi: " << wi.as_int() << std::endl; | ||
// std::cout << "trace year: " << year << std::endl; | ||
//find the last day for the new month | ||
day_type resultingEndOfMonthDay(cal_type::end_of_month_day(year, wi.as_int())); | ||
//original was the end of month -- force to last day of month | ||
if (origDayOfMonth_ == -1) { | ||
return date_type(year, wi.as_int(), resultingEndOfMonthDay) - d; | ||
} | ||
day_type dayOfMonth = origDayOfMonth_; | ||
if (dayOfMonth > resultingEndOfMonthDay) { | ||
dayOfMonth = resultingEndOfMonthDay; | ||
} | ||
return date_type(year, wi.as_int(), dayOfMonth) - d; | ||
} | ||
//! Returns a negative duration_type | ||
duration_type get_neg_offset(const date_type& d) const | ||
{ | ||
ymd_type ymd(d.year_month_day()); | ||
if (origDayOfMonth_ == 0) { | ||
origDayOfMonth_ = ymd.day; | ||
day_type endOfMonthDay(cal_type::end_of_month_day(ymd.year,ymd.month)); | ||
if (endOfMonthDay == ymd.day) { | ||
origDayOfMonth_ = -1; //force the value to the end of month | ||
} | ||
} | ||
typedef date_time::wrapping_int2<short,1,12> wrap_int2; | ||
typedef typename wrap_int2::int_type int_type; | ||
wrap_int2 wi(ymd.month); | ||
//calc the year wrap around, add() returns 0 or 1 if wrapped | ||
int_type year = wi.subtract(static_cast<int_type>(f_)); | ||
year = static_cast<int_type>(year + ymd.year); //calculate resulting year | ||
//find the last day for the new month | ||
day_type resultingEndOfMonthDay(cal_type::end_of_month_day(year, wi.as_int())); | ||
//original was the end of month -- force to last day of month | ||
if (origDayOfMonth_ == -1) { | ||
return date_type(year, wi.as_int(), resultingEndOfMonthDay) - d; | ||
} | ||
day_type dayOfMonth = origDayOfMonth_; | ||
if (dayOfMonth > resultingEndOfMonthDay) { | ||
dayOfMonth = resultingEndOfMonthDay; | ||
} | ||
return date_type(year, wi.as_int(), dayOfMonth) - d; | ||
} | ||
private: | ||
int f_; | ||
mutable short origDayOfMonth_; | ||
}; | ||
|
||
|
||
//! Functor to iterate a over weeks | ||
template<class date_type> | ||
class week_functor | ||
{ | ||
public: | ||
typedef typename date_type::duration_type duration_type; | ||
typedef typename date_type::calendar_type calendar_type; | ||
week_functor(int f) : f_(f) {} | ||
duration_type get_offset(const date_type& d) const | ||
{ | ||
// why is 'd' a parameter??? | ||
// fix compiler warnings | ||
d.year(); | ||
return duration_type(f_*calendar_type::days_in_week()); | ||
} | ||
duration_type get_neg_offset(const date_type& d) const | ||
{ | ||
// fix compiler warnings | ||
d.year(); | ||
return duration_type(-f_*calendar_type::days_in_week()); | ||
} | ||
private: | ||
int f_; | ||
}; | ||
|
||
//! Functor to iterate by a year adjusting for leap years | ||
template<class date_type> | ||
class year_functor | ||
{ | ||
public: | ||
//typedef typename date_type::year_type year_type; | ||
typedef typename date_type::duration_type duration_type; | ||
year_functor(int f) : _mf(f * 12) {} | ||
duration_type get_offset(const date_type& d) const | ||
{ | ||
return _mf.get_offset(d); | ||
} | ||
duration_type get_neg_offset(const date_type& d) const | ||
{ | ||
return _mf.get_neg_offset(d); | ||
} | ||
private: | ||
month_functor<date_type> _mf; | ||
}; | ||
|
||
|
||
} }//namespace date_time | ||
|
||
|
||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#ifndef DATE_TIME_C_TIME_HPP___ | ||
#define DATE_TIME_C_TIME_HPP___ | ||
|
||
/* Copyright (c) 2002,2003,2005 CrystalClear Software, Inc. | ||
* Use, modification and distribution is subject to the | ||
* Boost Software License, Version 1.0. (See accompanying | ||
* file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) | ||
* Author: Jeff Garland, Bart Garst | ||
* $Date: 2010-01-10 14:17:23 -0500 (Sun, 10 Jan 2010) $ | ||
*/ | ||
|
||
|
||
/*! @file c_time.hpp | ||
Provide workarounds related to the ctime header | ||
*/ | ||
|
||
#include <ctime> | ||
#include <string> // to be able to convert from string literals to exceptions | ||
#include <stdexcept> | ||
#include <boost/throw_exception.hpp> | ||
#include <boost/date_time/compiler_config.hpp> | ||
|
||
//Work around libraries that don't put time_t and time in namespace std | ||
#ifdef BOOST_NO_STDC_NAMESPACE | ||
namespace std { using ::time_t; using ::time; using ::localtime; | ||
using ::tm; using ::gmtime; } | ||
#endif // BOOST_NO_STDC_NAMESPACE | ||
|
||
//The following is used to support high precision time clocks | ||
#ifdef BOOST_HAS_GETTIMEOFDAY | ||
#include <sys/time.h> | ||
#endif | ||
|
||
#ifdef BOOST_HAS_FTIME | ||
#include <time.h> | ||
#endif | ||
|
||
namespace boost { | ||
namespace date_time { | ||
//! Provides a uniform interface to some 'ctime' functions | ||
/*! Provides a uniform interface to some ctime functions and | ||
* their '_r' counterparts. The '_r' functions require a pointer to a | ||
* user created std::tm struct whereas the regular functions use a | ||
* staticly created struct and return a pointer to that. These wrapper | ||
* functions require the user to create a std::tm struct and send in a | ||
* pointer to it. This struct may be used to store the resulting time. | ||
* The returned pointer may or may not point to this struct, however, | ||
* it will point to the result of the corresponding function. | ||
* All functions do proper checking of the C function results and throw | ||
* exceptions on error. Therefore the functions will never return NULL. | ||
*/ | ||
struct c_time { | ||
public: | ||
#if defined(BOOST_DATE_TIME_HAS_REENTRANT_STD_FUNCTIONS) | ||
//! requires a pointer to a user created std::tm struct | ||
inline | ||
static std::tm* localtime(const std::time_t* t, std::tm* result) | ||
{ | ||
// localtime_r() not in namespace std??? | ||
result = localtime_r(t, result); | ||
if (!result) | ||
boost::throw_exception(std::runtime_error("could not convert calendar time to local time")); | ||
return result; | ||
} | ||
//! requires a pointer to a user created std::tm struct | ||
inline | ||
static std::tm* gmtime(const std::time_t* t, std::tm* result) | ||
{ | ||
// gmtime_r() not in namespace std??? | ||
result = gmtime_r(t, result); | ||
if (!result) | ||
boost::throw_exception(std::runtime_error("could not convert calendar time to UTC time")); | ||
return result; | ||
} | ||
#else // BOOST_HAS_THREADS | ||
|
||
#if (defined(_MSC_VER) && (_MSC_VER >= 1400)) | ||
#pragma warning(push) // preserve warning settings | ||
#pragma warning(disable : 4996) // disable depricated localtime/gmtime warning on vc8 | ||
#endif // _MSC_VER >= 1400 | ||
//! requires a pointer to a user created std::tm struct | ||
inline | ||
static std::tm* localtime(const std::time_t* t, std::tm* result) | ||
{ | ||
result = std::localtime(t); | ||
if (!result) | ||
boost::throw_exception(std::runtime_error("could not convert calendar time to local time")); | ||
return result; | ||
} | ||
//! requires a pointer to a user created std::tm struct | ||
inline | ||
static std::tm* gmtime(const std::time_t* t, std::tm* result) | ||
{ | ||
result = std::gmtime(t); | ||
if (!result) | ||
boost::throw_exception(std::runtime_error("could not convert calendar time to UTC time")); | ||
return result; | ||
} | ||
#if (defined(_MSC_VER) && (_MSC_VER >= 1400)) | ||
#pragma warning(pop) // restore warnings to previous state | ||
#endif // _MSC_VER >= 1400 | ||
|
||
#endif // BOOST_HAS_THREADS | ||
}; | ||
}} // namespaces | ||
|
||
#endif // DATE_TIME_C_TIME_HPP___ |
Oops, something went wrong.