From d36019fd08eece646830beee608c0139d5a2b5bc Mon Sep 17 00:00:00 2001 From: Jeff Garland Date: Tue, 13 Aug 2002 23:07:56 +0000 Subject: [PATCH] first version of library [SVN r14827] --- include/boost/gdtl/adjust_functors.hpp | 135 +++++++++ include/boost/gdtl/c_local_time_adjustor.hpp | 72 +++++ include/boost/gdtl/c_time.hpp | 35 +++ include/boost/gdtl/constrained_value.hpp | 104 +++++++ include/boost/gdtl/date.hpp | 178 ++++++++++++ include/boost/gdtl/date_clock_device.hpp | 83 ++++++ include/boost/gdtl/date_defs.hpp | 33 +++ include/boost/gdtl/date_duration.hpp | 77 +++++ include/boost/gdtl/date_format_simple.hpp | 95 ++++++ include/boost/gdtl/date_formatting.hpp | 127 ++++++++ .../boost/gdtl/date_formatting_locales.hpp | 179 ++++++++++++ include/boost/gdtl/date_generators.hpp | 270 ++++++++++++++++++ include/boost/gdtl/date_iterator.hpp | 98 +++++++ include/boost/gdtl/date_names_put.hpp | 230 +++++++++++++++ include/boost/gdtl/date_parsing.hpp | 103 +++++++ include/boost/gdtl/dst_rules.hpp | 266 +++++++++++++++++ include/boost/gdtl/gregorian/formatters.hpp | 111 +++++++ .../boost/gdtl/gregorian/greg_calendar.hpp | 52 ++++ include/boost/gdtl/gregorian/greg_date.hpp | 90 ++++++ include/boost/gdtl/gregorian/greg_day.hpp | 59 ++++ .../boost/gdtl/gregorian/greg_day_of_year.hpp | 44 +++ .../boost/gdtl/gregorian/greg_duration.hpp | 41 +++ include/boost/gdtl/gregorian/greg_facet.hpp | 76 +++++ include/boost/gdtl/gregorian/greg_month.hpp | 62 ++++ include/boost/gdtl/gregorian/greg_weekday.hpp | 64 +++++ include/boost/gdtl/gregorian/greg_year.hpp | 59 ++++ include/boost/gdtl/gregorian/greg_ymd.hpp | 40 +++ include/boost/gdtl/gregorian/gregorian.hpp | 32 +++ .../boost/gdtl/gregorian/gregorian_types.hpp | 77 +++++ include/boost/gdtl/gregorian/parsers.hpp | 62 ++++ include/boost/gdtl/gregorian_calendar.hpp | 63 ++++ include/boost/gdtl/gregorian_calendar.ipp | 140 +++++++++ include/boost/gdtl/int_adapter.hpp | 212 ++++++++++++++ include/boost/gdtl/iso_format.hpp | 177 ++++++++++++ include/boost/gdtl/local_time_adjustor.hpp | 222 ++++++++++++++ include/boost/gdtl/parse_format_base.hpp | 34 +++ include/boost/gdtl/period.hpp | 253 ++++++++++++++++ include/boost/gdtl/posix_time/posix_time.hpp | 30 ++ .../gdtl/posix_time/posix_time_config.hpp | 144 ++++++++++ .../gdtl/posix_time/posix_time_duration.hpp | 100 +++++++ .../gdtl/posix_time/posix_time_system.hpp | 72 +++++ .../gdtl/posix_time/posix_time_types.hpp | 56 ++++ include/boost/gdtl/posix_time/ptime.hpp | 55 ++++ .../boost/gdtl/posix_time/time_formatters.hpp | 129 +++++++++ .../boost/gdtl/posix_time/time_parsers.hpp | 42 +++ include/boost/gdtl/posix_time/time_period.hpp | 37 +++ include/boost/gdtl/special_defs.hpp | 32 +++ include/boost/gdtl/testfrmwk.hpp | 74 +++++ include/boost/gdtl/time.hpp | 156 ++++++++++ include/boost/gdtl/time_clock.hpp | 76 +++++ include/boost/gdtl/time_defs.hpp | 49 ++++ include/boost/gdtl/time_duration.hpp | 151 ++++++++++ include/boost/gdtl/time_iterator.hpp | 54 ++++ include/boost/gdtl/time_parsing.hpp | 109 +++++++ include/boost/gdtl/time_resolution_traits.hpp | 76 +++++ include/boost/gdtl/time_system_counted.hpp | 149 ++++++++++ include/boost/gdtl/time_system_split.hpp | 124 ++++++++ include/boost/gdtl/wrapping_int.hpp | 137 +++++++++ include/boost/gdtl/year_month_day.hpp | 52 ++++ 59 files changed, 5959 insertions(+) create mode 100644 include/boost/gdtl/adjust_functors.hpp create mode 100644 include/boost/gdtl/c_local_time_adjustor.hpp create mode 100644 include/boost/gdtl/c_time.hpp create mode 100644 include/boost/gdtl/constrained_value.hpp create mode 100644 include/boost/gdtl/date.hpp create mode 100644 include/boost/gdtl/date_clock_device.hpp create mode 100644 include/boost/gdtl/date_defs.hpp create mode 100644 include/boost/gdtl/date_duration.hpp create mode 100644 include/boost/gdtl/date_format_simple.hpp create mode 100644 include/boost/gdtl/date_formatting.hpp create mode 100644 include/boost/gdtl/date_formatting_locales.hpp create mode 100644 include/boost/gdtl/date_generators.hpp create mode 100644 include/boost/gdtl/date_iterator.hpp create mode 100644 include/boost/gdtl/date_names_put.hpp create mode 100644 include/boost/gdtl/date_parsing.hpp create mode 100644 include/boost/gdtl/dst_rules.hpp create mode 100644 include/boost/gdtl/gregorian/formatters.hpp create mode 100644 include/boost/gdtl/gregorian/greg_calendar.hpp create mode 100644 include/boost/gdtl/gregorian/greg_date.hpp create mode 100644 include/boost/gdtl/gregorian/greg_day.hpp create mode 100644 include/boost/gdtl/gregorian/greg_day_of_year.hpp create mode 100644 include/boost/gdtl/gregorian/greg_duration.hpp create mode 100644 include/boost/gdtl/gregorian/greg_facet.hpp create mode 100644 include/boost/gdtl/gregorian/greg_month.hpp create mode 100644 include/boost/gdtl/gregorian/greg_weekday.hpp create mode 100644 include/boost/gdtl/gregorian/greg_year.hpp create mode 100644 include/boost/gdtl/gregorian/greg_ymd.hpp create mode 100644 include/boost/gdtl/gregorian/gregorian.hpp create mode 100644 include/boost/gdtl/gregorian/gregorian_types.hpp create mode 100644 include/boost/gdtl/gregorian/parsers.hpp create mode 100644 include/boost/gdtl/gregorian_calendar.hpp create mode 100644 include/boost/gdtl/gregorian_calendar.ipp create mode 100644 include/boost/gdtl/int_adapter.hpp create mode 100644 include/boost/gdtl/iso_format.hpp create mode 100644 include/boost/gdtl/local_time_adjustor.hpp create mode 100644 include/boost/gdtl/parse_format_base.hpp create mode 100644 include/boost/gdtl/period.hpp create mode 100644 include/boost/gdtl/posix_time/posix_time.hpp create mode 100644 include/boost/gdtl/posix_time/posix_time_config.hpp create mode 100644 include/boost/gdtl/posix_time/posix_time_duration.hpp create mode 100644 include/boost/gdtl/posix_time/posix_time_system.hpp create mode 100644 include/boost/gdtl/posix_time/posix_time_types.hpp create mode 100644 include/boost/gdtl/posix_time/ptime.hpp create mode 100644 include/boost/gdtl/posix_time/time_formatters.hpp create mode 100644 include/boost/gdtl/posix_time/time_parsers.hpp create mode 100644 include/boost/gdtl/posix_time/time_period.hpp create mode 100644 include/boost/gdtl/special_defs.hpp create mode 100644 include/boost/gdtl/testfrmwk.hpp create mode 100644 include/boost/gdtl/time.hpp create mode 100644 include/boost/gdtl/time_clock.hpp create mode 100644 include/boost/gdtl/time_defs.hpp create mode 100644 include/boost/gdtl/time_duration.hpp create mode 100644 include/boost/gdtl/time_iterator.hpp create mode 100644 include/boost/gdtl/time_parsing.hpp create mode 100644 include/boost/gdtl/time_resolution_traits.hpp create mode 100644 include/boost/gdtl/time_system_counted.hpp create mode 100644 include/boost/gdtl/time_system_split.hpp create mode 100644 include/boost/gdtl/wrapping_int.hpp create mode 100644 include/boost/gdtl/year_month_day.hpp diff --git a/include/boost/gdtl/adjust_functors.hpp b/include/boost/gdtl/adjust_functors.hpp new file mode 100644 index 0000000..6239b22 --- /dev/null +++ b/include/boost/gdtl/adjust_functors.hpp @@ -0,0 +1,135 @@ +#ifndef _GDTL_ADJUST_FUNCTORS_HPP___ +#define _GDTL_ADJUST_FUNCTORS_HPP___ +/* Copyright (c) 2001 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/date.hpp" +#include "boost/gdtl/wrapping_int.hpp" + +namespace boost { +namespace gdtl { + + + //! Functor to iterate a fixed number of days + template + 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 + { + 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 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 + } + } + gdtl::wrapping_int2 wi(ymd.month); + unsigned long year = wi.add(f_); //calc the year wrap around + year += ymd.year; //now add in the current 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; + } + private: + int f_; + mutable short origDayOfMonth_; + }; + + + //! Functor to iterate a over weeks + template + 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 + { + return duration_type(f_*calendar_type::days_in_week()); + } + private: + int f_; + }; + + //! Functor to iterate by a year adjusting for leap years + /*! + *@throws bad_day if date value is invalid (eg: feb 29) + */ + template + class year_functor + { + public: + typedef typename date_type::year_type year_type; + typedef typename date_type::duration_type duration_type; + year_functor(int f) : f_(f) {} + duration_type get_offset(const date_type& d) const + { + date_type new_date(d.year()+f_, d.month(), d.day()); + return new_date-d; + } + private: + int f_; + }; + + + +} }//namespace gdtl + +/* Copyright (c) 2001 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + +#endif diff --git a/include/boost/gdtl/c_local_time_adjustor.hpp b/include/boost/gdtl/c_local_time_adjustor.hpp new file mode 100644 index 0000000..a205a77 --- /dev/null +++ b/include/boost/gdtl/c_local_time_adjustor.hpp @@ -0,0 +1,72 @@ + +#ifndef GDTL_C_LOCAL_TIME_ADJUSTOR_HPP__ +#define GDTL_C_LOCAL_TIME_ADJUSTOR_HPP__ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +/*! @file c_local_time_adjustor.hpp + Time adjustment calculations based on machine +*/ + +#include +#include "boost/gdtl/c_time.hpp" + +namespace boost { +namespace gdtl { + + //! Adjust to / from utc using the C API + /*! Warning!!! This class assumes that timezone settings of the + * machine are correct. This can be a very dangerous assumption. + */ + template + class c_local_adjustor + { + public: + typedef typename time_type::time_duration_type time_duration_type; + typedef typename time_type::date_type date_type; + typedef typename date_type::duration_type date_duration_type; + //! Convert a utc time to local time + static time_type utc_to_local(const time_type& t) + { + date_type time_t_start_day(1970,1,1); + time_type time_t_start_time(time_t_start_day,time_duration_type(0,0,0)); + if (t < time_t_start_time) { + throw std::out_of_range("Cannot convert dates prior to Jan 1, 1970"); + } + date_duration_type dd = t.date() - time_t_start_day; + time_duration_type td = t.time_of_day(); + std::time_t t2 = dd.days()*86400 + td.hours()*3600 + td.minutes()*60 + td.seconds(); + std::tm* tms = std::localtime(&t2); + date_type d(tms->tm_year + 1900, + tms->tm_mon + 1, + tms->tm_mday); + time_duration_type td2(tms->tm_hour, + tms->tm_min, + tms->tm_sec, + t.time_of_day().fractional_seconds()); + + return time_type(d,td2); + } + }; + + + +} } //namespace gdtl + + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + */ + +#endif diff --git a/include/boost/gdtl/c_time.hpp b/include/boost/gdtl/c_time.hpp new file mode 100644 index 0000000..4491b05 --- /dev/null +++ b/include/boost/gdtl/c_time.hpp @@ -0,0 +1,35 @@ + +#ifndef GDTL_C_TIME_HPP___ +#define GDTL_C_TIME_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +/*! @file c_time.hpp + Provide workarounds related to the ctime header +*/ + + +#include +//Work around libraries that don't put time_t and time in namespace std +#include "boost/config.hpp" +#ifdef BOOST_NO_STDC_NAMESPACE +namespace std { using ::time_t; using ::time; using ::localtime; + using ::tm; using ::gmtime; } +#endif + + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + +#endif diff --git a/include/boost/gdtl/constrained_value.hpp b/include/boost/gdtl/constrained_value.hpp new file mode 100644 index 0000000..454a054 --- /dev/null +++ b/include/boost/gdtl/constrained_value.hpp @@ -0,0 +1,104 @@ + +#ifndef CONSTRAINED_VALUE_HPP___ +#define CONSTRAINED_VALUE_HPP___ +/* Copyright (c) 2000 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +namespace boost { + +//! Namespace containing constrained_value template and types +namespace CV { + //! Represent a min or max violation type + enum violation_enum {min_violation, max_violation}; + + //! A template to specify a constrained basic value type + /*! This template provides a quick way to generate + * an integer type with a constrained range. The type + * provides for the ability to specify the min, max, and + * and error handling policy. + * + * value policies + * A class that provides the range limits via the min and + * max functions as well as a function on_error that + * determines how errors are handled. A common strategy + * would be to assert or throw and exception. The on_error + * is passed both the current value and the new value that + * is in error. + * + */ + template + class constrained_value { + public: + typedef typename value_policies::value_type value_type; + // typedef except_type exception_type; + constrained_value(value_type value) + { + assign(value); + }; + constrained_value& operator=(value_type v) + { + assign(v); + return *this; + } + //! Return the max allowed value (traits method) + static value_type max() {return value_policies::max();}; + //! Return the min allowed value (traits method) + static value_type min() {return value_policies::min();}; + //! Coerce into the representation type + operator value_type() const {return value_;}; + protected: + value_type value_; + private: + void assign(value_type value) + { + //adding 1 below gets rid of a compiler warning which occurs when the + //min_value is 0 and the type is unsigned.... + if (value+1 < min()+1) { + value_policies::on_error(value_, value, min_violation); + return; + } + if (value > max()) { + value_policies::on_error(value_, value, max_violation); + return; + } + value_ = value; + + } + +}; + + //! Template to shortcut the constrained_value policy creation process + template + class simple_exception_policy + { + public: + typedef rep_type value_type; + static rep_type min() { return min_value; }; + static rep_type max() { return max_value;}; + static void on_error(rep_type, rep_type, violation_enum) + { + throw exception_type(); + } + }; + + + +} } //namespace CV + + +/* Copyright (c) 2000-2002 + * CrystalClear Software, Inc. + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + + +#endif diff --git a/include/boost/gdtl/date.hpp b/include/boost/gdtl/date.hpp new file mode 100644 index 0000000..8292f1e --- /dev/null +++ b/include/boost/gdtl/date.hpp @@ -0,0 +1,178 @@ +#ifndef GDTL_DATE_HPP___ +#define GDTL_DATE_HPP___ +/* Copyright (c) 2000 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/year_month_day.hpp" +#include "boost/gdtl/special_defs.hpp" +#include "boost/operators.hpp" + +namespace boost { +namespace gdtl { + + //!Representation of timepoint at the one day level resolution. + /*! + 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 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. + */ + + + template + class date + : boost::less_than_comparable > + { + public: + typedef T date_type; + typedef calendar calendar_type; + typedef typename calendar::date_traits_type traits_type; + typedef duration_type_ duration_type; + typedef typename calendar::year_type year_type; + typedef typename calendar::month_type month_type; + typedef typename calendar::day_type day_type; + typedef typename calendar::ymd_type ymd_type; + typedef typename calendar::date_rep_type date_rep_type; + typedef typename calendar::date_int_type date_int_type; + typedef typename calendar::day_of_week_type day_of_week_type; + date(year_type year, month_type month, day_type day) + : days_(calendar::day_number(ymd_type(year, month, day))) + {} + date(const ymd_type& ymd) + : days_(calendar::day_number(ymd)) + {} + //let the compiler write copy, assignment, and destructor + year_type year() const + { + ymd_type ymd = calendar::from_day_number(days_); + return ymd.year; + } + month_type month() const + { + ymd_type ymd = calendar::from_day_number(days_); + return ymd.month; + } + day_type day() const + { + ymd_type ymd = calendar::from_day_number(days_); + return ymd.day; + } + day_of_week_type day_of_week() const + { + ymd_type ymd = calendar::from_day_number(days_); + return calendar::day_of_week(ymd); + } + ymd_type year_month_day() const + { + return calendar::from_day_number(days_); + } + bool operator<(const date_type& rhs) const + { + return days_ < rhs.days_; + } + bool operator==(const date_type& rhs) const + { + return days_ == rhs.days_; + } + //! check to see if date is not a value + bool is_not_a_date() const + { + return traits_type::is_not_a_number(days_); + } + //! check to see if date is one of the infinity values + bool is_infinity() const + { + return traits_type::is_inf(days_); + } + //! check to see if date is greater than all possible dates + bool is_pos_infinity() const + { + return traits_type::is_pos_infinity(days_); + } + //! check to see if date is greater than all possible dates + bool is_neg_infinity() const + { + return traits_type::is_neg_infinity(days_); + } + //! return as a special value or a not_special if a normal date + special_values as_special() const + { + return traits_type::to_special(days_); + } + duration_type operator-(const date_type& d) const + { + date_rep_type val = date_rep_type(days_) - date_rep_type(d.days_); + return duration_type(val.as_number()); + } + + date_type operator-(const duration_type& dd) const + { + return date_type(date_rep_type(days_) - dd.days()); + } + date_rep_type day_count() const {return days_;}; + //allow internal access from operators + date_type operator+(const duration_type& dd) const + { + return date_type(date_rep_type(days_) + dd.days()); + } + + //see reference + protected: + /*! 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. + */ + date(date_int_type days) : days_(days) {}; + date(date_rep_type days) : days_(days.as_number()) {}; + date_int_type days_; + + }; + + + + +} } // namespace gdtl + +/* Copyright (c) 2000 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + + + +#endif diff --git a/include/boost/gdtl/date_clock_device.hpp b/include/boost/gdtl/date_clock_device.hpp new file mode 100644 index 0000000..2f4d5e4 --- /dev/null +++ b/include/boost/gdtl/date_clock_device.hpp @@ -0,0 +1,83 @@ + +#ifndef DATE_CLOCK_DEVICE_HPP___ +#define DATE_CLOCK_DEVICE_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/c_time.hpp" + + +namespace boost { +namespace gdtl { + + //! A clock providing day level services based on C time_t capabilities + /*! This clock uses Posix interfaces as its implemenation and hence + * uses the timezone settings of the operating system. Incorrect + * user settings will result in incorrect results for the calls + * to local_day. + */ + template + class day_clock + { + public: + typedef typename date_type::ymd_type ymd_type; + //! Get the local day as a date type + static date_type local_day() + { + return date_type(local_day_ymd()); + } + //! Get the local day as a ymd_type + static typename date_type::ymd_type local_day_ymd() + { + ::std::tm* curr = get_local_time(); + return ymd_type(curr->tm_year + 1900, + curr->tm_mon + 1, + curr->tm_mday); + } + //! Get the current day in universal date as a ymd_type + static typename date_type::ymd_type universal_day_ymd() + { + ::std::tm* curr = get_universal_time(); + return ymd_type(curr->tm_year + 1900, + curr->tm_mon + 1, + curr->tm_mday); + } + //! Get the UTC day as a date type + static date_type universal_day() + { + return date_type(universal_day_ymd()); + } + + private: + static ::std::tm* get_local_time() + { + ::std::time_t t; + ::std::time(&t); + return ::std::localtime(&t); + } + static ::std::tm* get_universal_time() + { + ::std::time_t t; + ::std::time(&t); + return ::std::gmtime(&t); + } + + }; + +} } //namespace gdtl + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + +#endif diff --git a/include/boost/gdtl/date_defs.hpp b/include/boost/gdtl/date_defs.hpp new file mode 100644 index 0000000..4e4d5d0 --- /dev/null +++ b/include/boost/gdtl/date_defs.hpp @@ -0,0 +1,33 @@ + +#ifndef GDTL_DATE_DEFS_HPP +#define GDTL_DATE_DEFS_HPP + +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + + +namespace boost { +namespace gdtl { + + //! An enumeration of weekday names + enum weekdays {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}; + +} } //namespace gdtl + +/* Copyright (c) 2000,2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + */ + + +#endif diff --git a/include/boost/gdtl/date_duration.hpp b/include/boost/gdtl/date_duration.hpp new file mode 100644 index 0000000..11c63e7 --- /dev/null +++ b/include/boost/gdtl/date_duration.hpp @@ -0,0 +1,77 @@ +#ifndef GDTL_DATE_DURATION__ +#define GDTL_DATE_DURATION__ +/* Copyright (c) 2000 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + + +#include + +namespace boost { +namespace gdtl { + + + //! Duration type with date level resolution + template + class date_duration + : boost::less_than_comparable + , boost::equality_comparable< date_duration< duration_rep> + > > + { + public: + //! Construct from a day count + explicit date_duration(duration_rep days) : days_(days) {}; + duration_rep days() const + { + return days_; + }; + //! Returns the smallest duration -- used by to calculate 'end' + static date_duration unit() + { + return date_duration(1); + } + //! Equality + bool operator==(const date_duration& rhs) const + { + return days_ == rhs.days_; + } + //! Less + bool operator<(const date_duration& rhs) const + { + return days_ < rhs.days_; + } + //! Subtract another duration -- result is signed + date_duration operator-(const date_duration& rhs) const + { + return date_duration(days_ - rhs.days_); + } + //! Add a duration -- result is signed + date_duration operator+(const date_duration& rhs) const + { + return date_duration(days_ + rhs.days_); + } + //! return sign information + bool is_negative() const + { + return days_ < 0; + } + private: + duration_rep days_; + }; + +} } //namspace gdtl + +/* Copyright (c) 2000, 2001 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + +#endif diff --git a/include/boost/gdtl/date_format_simple.hpp b/include/boost/gdtl/date_format_simple.hpp new file mode 100644 index 0000000..6c7205a --- /dev/null +++ b/include/boost/gdtl/date_format_simple.hpp @@ -0,0 +1,95 @@ +#ifndef GDTL_SIMPLE_FORMAT_HPP___ +#define GDTL_SIMPLE_FORMAT_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/parse_format_base.hpp" + +namespace boost { +namespace gdtl { + +//! Class to provide simple basic formatting rules +class simple_format { +public: + + //! String used printed is date is invalid + static const char* not_a_date() + { + return "not-a-date"; + } + //! String used to for positive infinity value + static const char* pos_infinity() + { //2001-Jan-03 + return "+infinity "; + } + //! String used to for positive infinity value + static const char* neg_infinity() + { + return "-infinity "; + } + //! Describe month format + static month_format_spec month_format() + { + return month_as_short_string; + } + static ymd_order_spec date_order() + { + return ymd_order_iso; //YYYY-MM-DD + } + //! This format uses '-' to separate date elements + static bool has_date_sep_chars() + { + return true; + } + //! Char to sep? + static char year_sep_char() + { + return '-'; + } + //! char between year-month + static char month_sep_char() + { + return '-'; + } + //! Char to separate month-day + static char day_sep_char() + { + return '-'; + } + //! char between date-hours + static char hour_sep_char() + { + return ' '; + } + //! char between hour and minute + static char minute_sep_char() + { + return ':'; + } + //! char for second + static char second_sep_char() + { + return ':'; + } + +}; + +} } //namespace gdtl + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + + + +#endif diff --git a/include/boost/gdtl/date_formatting.hpp b/include/boost/gdtl/date_formatting.hpp new file mode 100644 index 0000000..41f239b --- /dev/null +++ b/include/boost/gdtl/date_formatting.hpp @@ -0,0 +1,127 @@ +#ifndef GDTL_DATE_FORMATTING_HPP___ +#define GDTL_DATE_FORMATTING_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/iso_format.hpp" +#include +#include +#include + + +namespace boost { +namespace gdtl { + + //! Formats a month as as string into an ostream + template + class month_formatter + { + public: + //! Formats a month as as string into an ostream + /*! This function demands that month_type provide + * functions for converting to short and long strings + * if that capability is used. + */ + static std::ostream& format_month(const month_type& month, + std::ostream& os) + { + switch (format_type::month_format()) + { + case month_as_short_string: + { + os << month.as_short_string(); + break; + } + case month_as_long_string: + { + os << month.as_long_string(); + break; + } + case month_as_integer: + { + os << std::setw(2) << std::setfill('0') << month; + break; + } + + } + return os; + } // format_month + }; + + + //! Convert ymd to a standard string formatting policies + template + class ymd_formatter + { + public: + //! Convert ymd to a standard string formatting policies + /*! This is standard code for handling date formatting with + * year-month-day based date information. This function + * uses the format_type to control whether the string will + * contain separator characters, and if so what the character + * will be. In addtion, it can format the month as either + * an integer or a string as controled by the formatting + * policy + */ + static std::string ymd_to_string(ymd_type ymd) + { + typedef typename ymd_type::month_type month_type; + std::ostringstream ss; + ss << ymd.year; + if (format_type::has_date_sep_chars()) { + ss << format_type::month_sep_char(); + } + //this name is a bit ugly, oh well.... + month_formatter::format_month(ymd.month, ss); + if (format_type::has_date_sep_chars()) { + ss << format_type::day_sep_char(); + } + ss << std::setw(2) << std::setfill('0') + << ymd.day; + return ss.str(); + } + }; + + + //! Convert a date to string using format policies + template + class date_formatter + { + public: + //! Convert to a date to standard string using format policies + static std::string date_to_string(date_type d) + { + typedef typename date_type::ymd_type ymd_type; + if (d.is_not_a_date()) { + return format_type::not_a_date(); + } + if (d.is_neg_infinity()) { + return format_type::neg_infinity(); + } + if (d.is_pos_infinity()) { + return format_type::pos_infinity(); + } + ymd_type ymd = d.year_month_day(); + return ymd_formatter::ymd_to_string(ymd); + } + }; + +} } //namespace gdtl + + + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ +#endif + diff --git a/include/boost/gdtl/date_formatting_locales.hpp b/include/boost/gdtl/date_formatting_locales.hpp new file mode 100644 index 0000000..7ea67c3 --- /dev/null +++ b/include/boost/gdtl/date_formatting_locales.hpp @@ -0,0 +1,179 @@ +#ifndef GDTL_DATE_FORMATTING_LOCALES_HPP___ +#define GDTL_DATE_FORMATTING_LOCALES_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#ifndef BOOST_NO_STD_LOCALE + +#include "boost/gdtl/iso_format.hpp" +#include "boost/gdtl/date_names_put.hpp" +#include "boost/gdtl/parse_format_base.hpp" +//#include +#include +#include + + +namespace boost { +namespace gdtl { + + //! Formats a month as as string into an ostream + template + class ostream_month_formatter + { + public: + typedef typename facet_type::month_type month_type; + typedef std::basic_ostream ostream_type; + + //! Formats a month as as string into an output iterator + static void format_month(const month_type& month, + ostream_type& os, + const facet_type& f, + month_format_spec fs) + { + + switch (fs) + { + case month_as_short_string: + { + std::ostreambuf_iterator oitr(os); + f.put_month_short(oitr, month.as_enum()); + break; + } + case month_as_long_string: + { + std::ostreambuf_iterator oitr(os); + f.put_month_long(oitr, month.as_enum()); + break; + } + case month_as_integer: + { + os << std::setw(2) << std::setfill('0') << month; + break; + } + + } + } // format_month + + }; + + + //! Convert ymd to a standard string formatting policies + template + class ostream_ymd_formatter + { + public: + typedef typename ymd_type::month_type month_type; + typedef ostream_month_formatter month_formatter; + typedef std::basic_ostream ostream_type; + typedef std::basic_string foo_type; + + //! Convert ymd to a standard string formatting policies + /*! This is standard code for handling date formatting with + * year-month-day based date information. This function + * uses the format_type to control whether the string will + * contain separator characters, and if so what the character + * will be. In addtion, it can format the month as either + * an integer or a string as controled by the formatting + * policy + */ + // static string_type ymd_to_string(ymd_type ymd) +// { +// std::ostringstream ss; +// facet_type dnp; +// ymd_put(ymd, ss, dnp); +// return ss.str(); +// } + + // Put ymd to ostream -- part of ostream refactor + static void ymd_put(ymd_type ymd, + ostream_type& os, + const facet_type& f) + { + os << ymd.year; + std::ostreambuf_iterator oitr(os); + if (f.has_date_sep_chars()) { + f.month_sep_char(oitr); + } + month_formatter::format_month(ymd.month, os, + f, month_as_short_string); + + + if (f.has_date_sep_chars()) { + f.day_sep_char(oitr); + } + os << std::setw(2) << std::setfill('0') + << ymd.day; + } + + }; + + + //! Convert a date to string using format policies + template + class ostream_date_formatter + { + public: + typedef std::basic_ostream ostream_type; + typedef typename date_type::ymd_type ymd_type; + + //! Put date into an ostream + static void date_put(const date_type& d, + ostream_type& os, + const facet_type& f) + { + special_values sv = d.as_special(); + if (sv == not_special) { + ymd_type ymd = d.year_month_day(); + ostream_ymd_formatter::ymd_put(ymd, os, f); + } + else { // output a special value + std::ostreambuf_iterator coi(os); + f.put_special_value(coi, sv); + } + } + + + //! Put date into an ostream + static void date_put(const date_type& d, + ostream_type& os) + { + //retrieve the local from the ostream + std::locale locale = os.getloc(); + if (std::has_facet(locale)) { + // std::cout << "Good Deal Dude" << std::endl; + const facet_type& f = std::use_facet(locale); + date_put(d, os, f); + } + else { + //default to something sensible if no facet installed + facet_type default_facet; + date_put(d, os, default_facet); + } + } // date_to_ostream + }; //class date_formatter + + +} } //namespace gdtl + +#endif + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ +#endif + diff --git a/include/boost/gdtl/date_generators.hpp b/include/boost/gdtl/date_generators.hpp new file mode 100644 index 0000000..9408621 --- /dev/null +++ b/include/boost/gdtl/date_generators.hpp @@ -0,0 +1,270 @@ +#ifndef GDTL_DATE_GENERATORS_HPP__ +#define GDTL_DATE_GENERATORS_HPP__ +/* Copyright (c) 2000 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ +/*! @file date_generators.hpp + Definition and implementation of date algorithm templates +*/ + +#include "boost/gdtl/date.hpp" + +namespace boost { +namespace gdtl { + + + //! Generates a date by applying the year to the given month and day. + /*! + *Example usage: + *@code + * partial_date pd(Jan,1); + * date d = pd.get_date(2002);//2002-Jan-01 + *@endcode + * \ingroup date_alg + */ + template + class partial_date { + public: + typedef typename date_type::calendar_type calendar_type; + typedef typename calendar_type::day_type day_type; + typedef typename calendar_type::month_type month_type; + typedef typename calendar_type::year_type year_type; + typedef typename date_type::duration_type duration_type; + partial_date(month_type m, day_type d) : + month_(m), + day_(d) + {} + //! Return a concrete date when provided with a year specific year. + date_type get_date(year_type y) const + { + return date_type(y, month_, day_); + } + date_type operator()(year_type y) const + { + return date_type(y, month_, day_); + } + + + private: + month_type month_; + day_type day_; + }; + + + //! Useful generator functor for finding holidays + /*! Based on the idea in Cal. Calc. for finding holidays that are + * the 'first Monday of September'. + * The algorithm here basically guesses for the first + * day of the month. Then finds the first day of the correct + * type. That is, if the first of the month is a Tuesday + * and it needs Wenesday then we simply increment by a day + * and then we can add the length of a week until we get + * to the 'nth kday'. There are probably more efficient + * algorithms based on using a mod 7, but this one works + * reasonably well for basic applications. + * \ingroup date_alg + */ + template + class nth_kday_of_month { + public: + typedef typename date_type::calendar_type calendar_type; + typedef typename calendar_type::day_of_week_type day_of_week_type; + typedef typename calendar_type::month_type month_type; + typedef typename calendar_type::year_type year_type; + typedef typename date_type::duration_type duration_type; + enum week_num {first=1, second, third, fourth, fifth}; + nth_kday_of_month(week_num week_no, + day_of_week_type dow, + month_type m) : + month_(m), + wn_(week_no), + dow_(dow) + {} + //! Return a concrete date when provided with a year specific year. + date_type get_date(year_type y) const + { + date_type d(y, month_, 1); //first day of month + duration_type one_day(1); + duration_type one_week(7); + while (dow_ != d.day_of_week()) { + d = d + one_day; + } + int week = 1; + while (week < wn_) { + d = d + one_week; + week++; + } + return d; + } + private: + month_type month_; + week_num wn_; + day_of_week_type dow_; + }; + + //! Useful generator functor for finding holidays and daylight savings + /*! Similar to nth_kday_of_month, but requires less paramters + * \ingroup date_alg + */ + template + class first_kday_of_month { + public: + typedef typename date_type::calendar_type calendar_type; + typedef typename calendar_type::day_of_week_type day_of_week_type; + typedef typename calendar_type::month_type month_type; + typedef typename calendar_type::year_type year_type; + typedef typename date_type::duration_type duration_type; + //!Specify the first 'Sunday' in 'April' spec + /*!@param dow The day of week, eg: Sunday, Monday, etc + * @param month The month of the year, eg: Jan, Feb, Mar, etc + */ + first_kday_of_month(day_of_week_type dow, month_type month) : + month_(month), + dow_(dow) + {} + //! Return a concrete date when provided with a year specific year. + date_type get_date(year_type year) const + { + date_type d(year, month_,1); + duration_type one_day(1); + while (dow_ != d.day_of_week()) { + d = d + one_day; + } + return d; + } + private: + month_type month_; + day_of_week_type dow_; + }; + + + + //! Calculate something like Last Sunday of January + /*! Useful generator functor for finding holidays and daylight savings + * Get the last day of the month and then calculate the difference + * to the last previous day. + * \ingroup date_alg + */ + template + class last_kday_of_month { + public: + typedef typename date_type::calendar_type calendar_type; + typedef typename calendar_type::day_of_week_type day_of_week_type; + typedef typename calendar_type::month_type month_type; + typedef typename calendar_type::year_type year_type; + typedef typename date_type::duration_type duration_type; + //!Specify the date spec like 'Sunday' in 'April' spec + /*!@param dow The day of week, eg: Sunday, Monday, etc + * @param month The month of the year, eg: Jan, Feb, Mar, etc + */ + last_kday_of_month(day_of_week_type dow, month_type month) : + month_(month), + dow_(dow) + {} + //! Return a concrete date when provided with a year specific year. + date_type get_date(year_type year) const + { + date_type d(year, month_, calendar_type::end_of_month_day(year,month_)); + duration_type one_day(1); + while (dow_ != d.day_of_week()) { + d = d - one_day; + } + return d; + } + private: + month_type month_; + day_of_week_type dow_; + }; + + + //! Calculate something like "First Sunday after Jan 1,2002 + /*! Date generator that takes a date and finds kday after + *@code + typedef boost::gdtl::first_kday_after firstkdayafter; + firstkdayafter fkaf(Monday); + fkaf.get_date(date(2002,Feb,1)); + @endcode + * \ingroup date_alg + */ + template + class first_kday_after { + public: + typedef typename date_type::calendar_type calendar_type; + typedef typename calendar_type::day_of_week_type day_of_week_type; + typedef typename date_type::duration_type duration_type; + first_kday_after(day_of_week_type dow) : + dow_(dow) + {} + //! Return next kday given. + date_type get_date(date_type start_day) const + { + duration_type one_day(1); + date_type d = start_day + one_day; + while (dow_ != d.day_of_week()) { + d = d + one_day; + } + return d; + } + + private: + day_of_week_type dow_; + }; + + //! Calculate something like "First Sunday before Jan 1,2002 + /*! Date generator that takes a date and finds kday after + *@code + typedef boost::gdtl::first_kday_before firstkdaybefore; + firstkdaybefore fkbf(Monday); + fkbf.get_date(date(2002,Feb,1)); + @endcode + * \ingroup date_alg + */ + template + class first_kday_before { + public: + typedef typename date_type::calendar_type calendar_type; + typedef typename calendar_type::day_of_week_type day_of_week_type; + typedef typename date_type::duration_type duration_type; + first_kday_before(day_of_week_type dow) : + dow_(dow) + {} + //! Return next kday given. + date_type get_date(date_type start_day) const + { + duration_type one_day(1); + date_type d = start_day - one_day; + while (dow_ != d.day_of_week()) { + d = d - one_day; + } + return d; + } + + private: + day_of_week_type dow_; + }; + + +} } //namespace gdtl + + +/* + * Copyright (c) 2001 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided as is without express or implied warranty. + * + * + * Author: Jeff Garland (jeff@CrystalClearSoftware.com) + * Created: Sun Sep 9 17:20:39 2001 + * + */ + + +#endif diff --git a/include/boost/gdtl/date_iterator.hpp b/include/boost/gdtl/date_iterator.hpp new file mode 100644 index 0000000..b715973 --- /dev/null +++ b/include/boost/gdtl/date_iterator.hpp @@ -0,0 +1,98 @@ +#ifndef DATE_ITERATOR_HPP___ +#define DATE_ITERATOR_HPP___ +/* Copyright (c) 2000 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include + +namespace boost { +namespace gdtl { + //! An iterator over dates with varying resolution (day, week, month, year, etc) + enum date_resolutions {day, week, months, year, decade, century, NumDateResolutions}; + + //! Base date iterator type + /*! This class provides the skeleton for the creation of iterators. + * New and interesting interators can be created by plugging in a new + * function that derives the next value from the current state. + * generation of various types of -based information. + * + * Template Parameters + * + * date_type + * + * The date_type is a concrete date_type. The date_type must + * define a duration_type and a calendar_type. + */ + template + class date_itr_base { + // works, but benefit unclear at the moment + // class date_itr_base : public std::iterator{ + public: + typedef typename date_type::duration_type duration_type; + typedef date_type value_type; + typedef std::input_iterator_tag iterator_category; + + date_itr_base(date_type d) : current_(d) {} + virtual ~date_itr_base() {}; + date_itr_base& operator++() + { + current_ = current_ + get_offset(current_); + return *this; + }; + virtual duration_type get_offset(const date_type& current) const=0; + date_type operator*() {return current_;}; + date_type* operator->() {return ¤t_;}; + bool operator< (const date_type& d) {return current_ < d;} + bool operator<= (const date_type& d) {return current_ <= d;} + bool operator> (const date_type& d) {return current_ > d;} + bool operator>= (const date_type& d) {return current_ >= d;} + bool operator== (const date_type& d) {return current_ == d;} + bool operator!= (const date_type& d) {return current_ != d;} + private: + date_type current_; + }; + + //! Overrides the base date iterator providing hook for functors + /* + * offset_functor + * + * The offset functor must define a get_offset function that takes the + * current point in time and calculates and offset. + * + */ + template + class date_itr : public date_itr_base { + public: + typedef typename date_type::duration_type duration_type; + date_itr(date_type d, int factor=1) : + date_itr_base(d), + of_(factor) + {} + virtual duration_type get_offset(const date_type& current) const + { + return of_.get_offset(current); + } + private: + offset_functor of_; + }; + + + +} } //namespace gdtl + +/* Copyright (c) 2000, 2001 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + +#endif diff --git a/include/boost/gdtl/date_names_put.hpp b/include/boost/gdtl/date_names_put.hpp new file mode 100644 index 0000000..7b04abc --- /dev/null +++ b/include/boost/gdtl/date_names_put.hpp @@ -0,0 +1,230 @@ +#ifndef GDTL_DATE_NAMES_PUT_HPP___ +#define GDTL_DATE_NAMES_PUT_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + + +#include "boost/config.hpp" //sets BOOST_NO_STD_LOCALE + +//This file basically becomes a noop if locales are not supported +#ifndef BOOST_NO_STD_LOCALE + +#include "boost/gdtl/special_defs.hpp" +#include "boost/gdtl/date_defs.hpp" +#include + + +namespace boost { +namespace gdtl { + + //! Output facet base class for gregorian dates. + /*! This class is a base class for date facets used to localize the + * names of months and the names of days in the week. + * + * Requirements of Config + * - define an enumeration month_enum that enumerates the months. + * The enumeration should be '1' based eg: Jan==1 + * - define as_short_string and as_long_string + * + * (see langer & kreft p334). + * + */ + template > + class date_names_put : public std::locale::facet + { + public: + date_names_put() {}; + typedef OutputIterator iter_type; + typedef typename Config::month_type month_type; + typedef typename Config::month_enum month_enum; + typedef typename Config::weekday_enum weekday_enum; + typedef typename Config::special_value_enum special_value_enum; + //typedef typename Config::format_type format_type; + typedef std::basic_string string_type; + + static std::locale::id id; + void put_special_value(iter_type& oitr, special_value_enum sv) const + { + do_put_special_value(oitr, sv); + } + void put_month_short(iter_type& oitr, month_enum moy) const + { + do_put_month_short(oitr, moy); + } + void put_month_long(iter_type& oitr, month_enum moy) const + { + do_put_month_long(oitr, moy); + } + void put_weekday_short(iter_type& oitr, weekday_enum wd) const + { + do_put_weekday_short(oitr, wd); + } + void put_weekday_long(iter_type& oitr, weekday_enum wd) const + { + do_put_weekday_long(oitr, wd); + } + bool has_date_sep_chars() const + { + return do_has_date_sep_chars(); + } + void year_sep_char(iter_type& oitr) const + { + do_year_sep_char(oitr); + } + //! char between year-month + void month_sep_char(iter_type& oitr) const + { + do_month_sep_char(oitr); + } + //! Char to separate month-day + void day_sep_char(iter_type& oitr) const + { + do_day_sep_char(oitr); + } + + protected: + //! Default facet implementation uses month_type defaults + virtual void do_put_month_short(iter_type& oitr, month_enum moy) const + { + month_type gm(moy); + put_string(oitr, gm.as_short_string()); + } + //! Default facet implementation uses month_type defaults + virtual void do_put_month_long(iter_type& oitr, + month_enum moy) const + { + month_type gm(moy); + put_string(oitr, gm.as_long_string()); + } + //! Default facet implementation for special value types + virtual void do_put_special_value(iter_type& oitr, special_value_enum sv) const + { + switch (sv) { + case not_a_date_time: + { + put_string(oitr, "not-a-date-time"); + break; + } + case pos_infin: + { + put_string(oitr, "+infinity"); + break; + } + case neg_infin: + { + put_string(oitr, "-infinity"); + break; + } + } + } + virtual void do_put_weekday_short(iter_type& oitr, weekday_enum wd) const + { + } + virtual void do_put_weekday_long(iter_type& oitr, weekday_enum wd) const + { + } + virtual bool do_has_date_sep_chars() const + { + return true; + } + virtual void do_year_sep_char(iter_type& oitr) const + { + put_string(oitr, "-"); + } + //! char between year-month + virtual void do_month_sep_char(iter_type& oitr) const + { + put_string(oitr, "-"); + } + //! Char to separate month-day + virtual void do_day_sep_char(iter_type& oitr) const + { + put_string(oitr, "-"); + } + + void put_string(iter_type& oi, const char* const s) const + { + string_type s1(s); + typename string_type::iterator si,end; + for (si=s1.begin(), end=s1.end(); si!=end; si++, oi++) { + *oi = *si; + } + } + }; + + + //! An date name output facet that takes an array of char* to define strings + template > + class all_date_names_put : public date_names_put + { + public: + all_date_names_put(const charT* const month_short_names[], + const charT* const month_long_names[], + const charT* const special_value_names[], + const charT* const weekday_short_names[], + const charT* const weekday_long_names[]) : + month_short_names_(month_short_names), + month_long_names_(month_long_names), + special_value_names_(special_value_names), + weekday_short_names_(weekday_short_names), + weekday_long_names_(weekday_long_names) + {}; + typedef OutputIterator iter_type; + typedef typename Config::month_enum month_enum; + typedef typename Config::weekday_enum weekday_enum; + typedef typename Config::special_value_enum special_value_enum; + + protected: + //! Generic facet that takes array of chars + virtual void do_put_month_short(iter_type& oitr, month_enum moy) const + { + put_string(oitr, month_short_names_[moy-1]); + } + //! Long month names + virtual void do_put_month_long(iter_type& oitr, month_enum moy) const + { + put_string(oitr, month_long_names_[moy-1]); + } + //! Special values names + virtual void do_put_special_value(iter_type& oitr, special_value_enum sv) const + { + put_string(oitr, special_value_names_[sv]); + } + virtual void do_put_weekday_short(iter_type& oitr, weekday_enum wd) const + { + put_string(oitr, weekday_short_names_[wd]); + } + virtual void do_put_weekday_long(iter_type& oitr, weekday_enum wd) const + { + put_string(oitr, weekday_long_names_[wd]); + } + private: + const char* const* month_short_names_; + const char* const* month_long_names_; + const char* const* special_value_names_; + const char* const* weekday_short_names_; + const char* const* weekday_long_names_; + }; + +} } //namespace boost::gdtl + +#endif //BOOST_NO_STD_LOCALE + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ +#endif diff --git a/include/boost/gdtl/date_parsing.hpp b/include/boost/gdtl/date_parsing.hpp new file mode 100644 index 0000000..f1c6508 --- /dev/null +++ b/include/boost/gdtl/date_parsing.hpp @@ -0,0 +1,103 @@ + +#ifndef _GDTL_DATE_PARSING_HPP___ +#define _GDTL_DATE_PARSING_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/tokenizer.hpp" +#include "boost/lexical_cast.hpp" + + +namespace boost { + +namespace gdtl { + + + //! Generic function to parse a delimited date (eg: 2002-02-10) + template + date_type + parse_date(const std::string& s) + { + typedef typename date_type::year_type year_type; + int pos = 0; + typename date_type::ymd_type ymd(year_type::min(),1,1); + boost::tokenizer > tok(s); + for(boost::tokenizer<>::iterator beg=tok.begin(); beg!=tok.end();++beg){ + int i = boost::lexical_cast(*beg); + switch(pos) { + case 0: ymd.year = i; break; + case 1: ymd.month = i; break; + case 2: ymd.day = i; break; + }; + pos++; + } + return date_type(ymd); + } + + //! Generic function to parse undelimited date (eg: 20020201) + template + date_type + parse_undelimited_date(const std::string& s) + { + int offsets[] = {4,2,2}; + int pos = 0; + typedef typename date_type::year_type year_type; + typename date_type::ymd_type ymd(year_type::min(),1,1); + boost::offset_separator osf(offsets, offsets+3); + boost::tokenizer tok(s, osf); + for(boost::tokenizer::iterator ti=tok.begin(); ti!=tok.end();++ti){ + int i = boost::lexical_cast(*ti); + // std::cout << i << std::endl; + switch(pos) { + case 0: ymd.year = i; break; + case 1: ymd.month = i; break; + case 2: ymd.day = i; break; + }; + pos++; + } + return date_type(ymd); + } + + + template + date_type + parse_date2(const std::string& s) + { + //using namespace boost; + int pos = 0; + typedef typename date_type::year_type year_type; + typename date_type::ymd_type ymd(year_type::min(),1,1); + boost::char_delimiters_separator delim("DT"); + boost::tokenizer > tok(s); + for(boost::tokenizer<>::iterator beg=tok.begin(); beg!=tok.end();++beg){ + int i = boost::lexical_cast(*beg); + switch(pos) { + case 0: ymd.year = i; break; + case 1: ymd.month = i; break; + case 2: ymd.day = i; break; + }; + pos++; + } + return date_type(ymd); + } + + +} } //namespace gdtl + + + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + +#endif diff --git a/include/boost/gdtl/dst_rules.hpp b/include/boost/gdtl/dst_rules.hpp new file mode 100644 index 0000000..4fd8def --- /dev/null +++ b/include/boost/gdtl/dst_rules.hpp @@ -0,0 +1,266 @@ + +#ifndef GDTL_DST_RULES_HPP__ +#define GDTL_DST_RULES_HPP__ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +/*! @file dst_rules.hpp + Contains template class to provide static dst rule calculations +*/ + +#include "boost/gdtl/date_generators.hpp" +#include "boost/gdtl/period.hpp" +#include "boost/gdtl/date_defs.hpp" +#include + +namespace boost { +namespace gdtl { + + enum time_is_dst_result {is_not_in_dst, is_in_dst, + ambiguous, invalid_time_label}; + + + //! Generic functions to calculate dst transition information + template + class dst_calculator + { + public: + typedef time_duration_type_ time_duration_type; + typedef date_type_ date_type; + + //! Check the local time offset when on dst start day + /*! On this dst transition, the time label between + * the transition boundary and the boudary + the offset + * are invalid times. If before the boundary then still + * not in dst. + *@param time_of_day Time offset in the day for the local time + *@param dst_start_offset_minutes Local day offset for start of dst + *@param dst_length_minutes Number of minutes to adjust clock forward + *@retval status of time label w.r.t. dst + */ + static time_is_dst_result + process_local_dst_start_day(const time_duration_type& time_of_day, + unsigned int dst_start_offset_minutes, + long dst_length_minutes) + { + // std::cout << "here" << std::endl; + if (time_of_day < time_duration_type(0,dst_start_offset_minutes,0)) { + return is_not_in_dst; + } + long offset = dst_start_offset_minutes + dst_length_minutes; + if (time_of_day >= time_duration_type(0,offset,0)) { + return is_in_dst; + } + return invalid_time_label; + } + + //! Check the local time offset when on the last day of dst + /*! This is the calculation for the DST end day. On that day times + * prior to the conversion time - dst_length (1 am in US) are still + * in dst. Times between the above and the switch time are + * ambiguous. Times after the start_offset are not in dst. + *@param time_of_day Time offset in the day for the local time + *@param dst_end_offset_minutes Local time of day for end of dst + *@retval status of time label w.r.t. dst + */ + static time_is_dst_result + process_local_dst_end_day(const time_duration_type& time_of_day, + unsigned int dst_end_offset_minutes, + long dst_length_minutes) + { + //in US this will be 60 so offset in day is 1,0,0 + int offset = dst_end_offset_minutes-dst_length_minutes; + if (time_of_day < time_duration_type(0,offset,0)) { + return is_in_dst; + } + if (time_of_day >= time_duration_type(0,dst_end_offset_minutes,0)) { + return is_not_in_dst; + } + return ambiguous; + } + + //! Calculates if the given local time is dst or not + /*! Determines if the time is really in DST or not. Also checks for + * invalid and ambiguous. + * @param current_day The day to check for dst + * @param time_of_day Time offset within the day to check + * @param dst_start_day Starting day of dst for the given locality + * @param dst_start_offset_minutes Offset within day for dst + * boundary (eg 120 for US which is 02:00:00) + * @param dst_end_day Ending day of dst for the given locality + * @param dst_end_offset_minutes Offset within day for dst + * boundary (eg 120 for US which is 02:00:00) + * @param dst_length_minutes Length of dst adjusment (eg: 60 for US) + * @retval The time is either ambiguous, invalid, in dst, or not in dst + */ + static time_is_dst_result + local_is_dst(const date_type& current_day, + const time_duration_type& time_of_day, + const date_type& dst_start_day, + unsigned int dst_start_offset_minutes, + const date_type& dst_end_day, + unsigned int dst_end_offset_minutes, + long dst_length_minutes) + { + //in northern hemisphere dst is in the middle of the year + if (dst_start_day < dst_end_day) { + if ((current_day > dst_start_day) && (current_day < dst_end_day)) { + return is_in_dst; + } + if ((current_day < dst_start_day) || (current_day > dst_end_day)) { + return is_not_in_dst; + } + } + else {//southern hemisphere dst is at begining /end of year + if ((current_day < dst_start_day) && (current_day > dst_end_day)) { + return is_not_in_dst; + } + if ((current_day > dst_start_day) || (current_day < dst_end_day)) { + return is_in_dst; + } + } + + if (current_day == dst_start_day) { + return process_local_dst_start_day(time_of_day, + dst_start_offset_minutes, + dst_length_minutes); + } + + if (current_day == dst_end_day) { + return process_local_dst_end_day(time_of_day, + dst_end_offset_minutes, + dst_length_minutes); + } + //you should never reach this statement + return invalid_time_label; + } + + }; + + + //! Class to calculate dst boundaries for US time zones + /* + */ + template //1 hour == 60 min in US + class us_dst_rules + { + public: + typedef time_duration_type_ time_duration_type; + typedef date_type_ date_type; + typedef typename date_type::year_type year_type; + typedef typename date_type::calendar_type calendar_type; + typedef gdtl::last_kday_of_month lkday; + typedef gdtl::first_kday_of_month fkday; + typedef dst_calculator dstcalc; + + //! Calculates if the given local time is dst or not + /*! Determines if the time is really in DST or not. Also checks for + * invalid and ambiguous. + * @retval The time is either ambiguous, invalid, in dst, or not in dst + */ + static time_is_dst_result local_is_dst(const date_type& d, + const time_duration_type& td) + { + + year_type y = d.year(); + date_type dst_start = local_dst_start_day(y); + date_type dst_end = local_dst_end_day(y); + return dstcalc::local_is_dst(d,td, + dst_start,dst_start_offset_minutes, + dst_end, dst_start_offset_minutes, + dst_length_minutes); + + } + + + static bool is_dst_boundary_day(date_type d) + { + year_type y = d.year(); + return ((d == local_dst_start_day(y)) || + (d == local_dst_end_day(y))); + } + + static date_type local_dst_start_day(year_type year) + { + //first sunday in april + fkday fsia(Sunday, gregorian::Apr); + return fsia.get_date(year); + } + + static date_type local_dst_end_day(year_type year) + { + //last sunday in october + lkday lsio(Sunday, gregorian::Oct); + return lsio.get_date(year); + } + + static time_duration_type dst_offset() + { + return time_duration_type(0,dst_length_minutes,0); + } + + }; + + + + //! Used for local time adjustments in places that don't use dst + template + class null_dst_rules + { + public: + typedef time_duration_type_ time_duration_type; + typedef date_type_ date_type; + + + //! Calculates if the given local time is dst or not + /*! @retval Always is_not_in_dst since this is for zones without dst + */ + static time_is_dst_result local_is_dst(const date_type&, + const time_duration_type&) + { + return is_not_in_dst; + } + + //! Calculates if the given utc time is in dst + static time_is_dst_result utc_is_dst(const date_type&, + const time_duration_type&) + { + return is_not_in_dst; + } + + static bool is_dst_boundary_day(date_type d) + { + return false; + } + + static time_duration_type dst_offset() + { + return time_duration_type(0,0,0); + } + + }; + + +} } //namespace gdtl + + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + */ + +#endif diff --git a/include/boost/gdtl/gregorian/formatters.hpp b/include/boost/gdtl/gregorian/formatters.hpp new file mode 100644 index 0000000..1d73a7f --- /dev/null +++ b/include/boost/gdtl/gregorian/formatters.hpp @@ -0,0 +1,111 @@ +#ifndef GREGORIAN_FORMATTERS_HPP___ +#define GREGORIAN_FORMATTERS_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/gregorian/gregorian_types.hpp" +#include "boost/gdtl/gregorian/greg_facet.hpp" +#include "boost/gdtl/date_formatting.hpp" +#include "boost/gdtl/iso_format.hpp" +#include "boost/gdtl/date_format_simple.hpp" + +namespace boost { +namespace gregorian { + + + + //! To YYYY-mmm-DD string where mmm 3 char month name. Example: 2002-Jan-01 + /*!\ingroup date_format + */ + inline std::string to_simple_string(const date& d) { + return gdtl::date_formatter::date_to_string(d); + } + + //! Convert date period to simple string. Example: [2002-Jan-01/2002-Jan-02] + /*!\ingroup date_format + */ + inline std::string to_simple_string(const date_period& d) { + std::string s("["); + std::string d1(gdtl::date_formatter::date_to_string(d.begin())); + std::string d2(gdtl::date_formatter::date_to_string(d.last())); + return std::string("[" + d1 + "/" + d2 + "]"); + } + + //! Date period to iso standard format CCYYMMDD/CCYYMMDD. Example: 20021225/20021231 + /*!\ingroup date_format + */ + inline std::string to_iso_string(const date_period& d) { + std::string s(gdtl::date_formatter::date_to_string(d.begin())); + return s + "/" + gdtl::date_formatter::date_to_string(d.last()); + } + + + //! Convert to iso extended format string CCYY-MM-DD. Example 2002-12-31 + /*!\ingroup date_format + */ + inline std::string to_iso_extended_string(const date& d) { + return gdtl::date_formatter::date_to_string(d); + } + + //! Convert to iso standard string YYYYMMDD. Example: 20021231 + /*!\ingroup date_format + */ + inline std::string to_iso_string(const date& d) { + return gdtl::date_formatter::date_to_string(d); + } + + + + inline std::string to_sql_string(const date& d) + { + date::ymd_type ymd = d.year_month_day(); + std::ostringstream ss; + ss << ymd.year << "-" + << std::setw(2) << std::setfill('0') + << ymd.month + << "-" + << std::setw(2) << std::setfill('0') + << ymd.day; + return ss.str(); + } + +} } //namespace gregorian + + + + + + + + + + + + + + + + + + + + + + + + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ +#endif + diff --git a/include/boost/gdtl/gregorian/greg_calendar.hpp b/include/boost/gdtl/gregorian/greg_calendar.hpp new file mode 100644 index 0000000..2b1e969 --- /dev/null +++ b/include/boost/gdtl/gregorian/greg_calendar.hpp @@ -0,0 +1,52 @@ +#ifndef GREGORIAN_GREGORIAN_CALENDAR_HPP__ +#define GREGORIAN_GREGORIAN_CALENDAR_HPP__ +/* Copyright (c) 2001 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/gregorian/greg_weekday.hpp" +#include "boost/gdtl/gregorian/greg_day_of_year.hpp" +#include "boost/gdtl/gregorian_calendar.hpp" +#include "boost/gdtl/gregorian/greg_ymd.hpp" +#include "boost/gdtl/int_adapter.hpp" + +namespace boost { +namespace gregorian { + + //!An internal date representation that includes infinities, not a date + typedef gdtl::int_adapter fancy_date_rep; + + //! Gregorian calendar for this implementation, hard work in the base + class gregorian_calendar : + public gdtl::gregorian_calendar_base { + public: + //! Type to hold a weekday (eg: Sunday, Monday,...) + typedef greg_weekday day_of_week_type; + //! Internal date representation that handles infinity, not a date + typedef fancy_date_rep date_rep_type; + //! Date rep implements the traits stuff as well + typedef fancy_date_rep date_traits_type; + + + private: + }; + +} } //namespace gregorian + + +/* Copyright (c) 2001 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + + +#endif + diff --git a/include/boost/gdtl/gregorian/greg_date.hpp b/include/boost/gdtl/gregorian/greg_date.hpp new file mode 100644 index 0000000..66aea2d --- /dev/null +++ b/include/boost/gdtl/gregorian/greg_date.hpp @@ -0,0 +1,90 @@ +#ifndef GREG_DATE_HPP___ +#define GREG_DATE_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/date.hpp" +#include "boost/gdtl/special_defs.hpp" +#include "boost/gdtl/gregorian/greg_calendar.hpp" +#include "boost/gdtl/gregorian/greg_duration.hpp" + + +namespace boost { +namespace gregorian { + + //bring special enum values into the namespace + using gdtl::special_values; + using gdtl::not_special; + using gdtl::neg_infin; + using gdtl::pos_infin; + using gdtl::not_a_date_time; + using gdtl::max_date_time; + using gdtl::min_date_time; + + //! A date type based on gregorian_calendar + /*! This class is the primary interface for programming with + greogorian dates. The is a lightweight type that can be + freely passed by value. All comparison operators are + supported. + \ingroup date_basics + */ + class date : public gdtl::date + { + public: + typedef gregorian_calendar::year_type year_type; + typedef gregorian_calendar::month_type month_type; + typedef gregorian_calendar::day_type day_type; + typedef gregorian_calendar::ymd_type ymd_type; + typedef gregorian_calendar::date_rep_type date_rep_type; + typedef gregorian_calendar::date_int_type date_int_type; + typedef date_duration duration_type; + //! Main constructor with year, month, day + date(year_type year, month_type month, day_type day) + : gdtl::date(year, month, day) + {} + //! Constructor from a ymd_type structure + explicit date(const ymd_type& ymd) + : gdtl::date(ymd) + {} + //! Needed copy constructor + date(const date_int_type& rhs): + gdtl::date(rhs) + {} + //! Needed copy constructor + date(date_rep_type rhs): + gdtl::date(rhs) + {} + //! Constructor for infinities, not a date, max and min date + explicit date(special_values sv): + gdtl::date(date_rep_type::from_special(sv)) + {} + //! Return the day number from the calendar + date_int_type day_number() const + { + return days_; + } + + private: + + }; + + + +} } //namespace gregorian + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + + +#endif diff --git a/include/boost/gdtl/gregorian/greg_day.hpp b/include/boost/gdtl/gregorian/greg_day.hpp new file mode 100644 index 0000000..51bb897 --- /dev/null +++ b/include/boost/gdtl/gregorian/greg_day.hpp @@ -0,0 +1,59 @@ +#ifndef GREG_DAY_HPP___ +#define GREG_DAY_HPP___ +/* Copyright (c) 2000 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/constrained_value.hpp" +#include + +namespace boost { +namespace gregorian { + + //! Exception type for gregorian day of month (1..31) + struct bad_day_of_month : public std::out_of_range + { + bad_day_of_month() : + std::out_of_range("Day of month value is out of range 1..31") + {} + }; + //! Policy class that declares error handling and day of month ranges + typedef CV::simple_exception_policy greg_day_policies; + + //! Generated represetation for gregorian day of month + typedef CV::constrained_value greg_day_rep; + + //! Represent a day of the month (range 1 - 31) + /*! This small class allows for simple conversion an integer value into + a day of the month for a standard gregorian calendar. The type + is automatically range checked so values outside of the range 1-31 + will cause a bad_day_of_month exception + */ + class greg_day : public greg_day_rep { + public: + greg_day(unsigned short day_of_month) : greg_day_rep(day_of_month) {} + unsigned short as_number() const {return value_;} + operator unsigned short() const {return value_;} + private: + + }; + + + +} } //namespace gregorian + +/* Copyright (c) 2000 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + + +#endif diff --git a/include/boost/gdtl/gregorian/greg_day_of_year.hpp b/include/boost/gdtl/gregorian/greg_day_of_year.hpp new file mode 100644 index 0000000..dba857b --- /dev/null +++ b/include/boost/gdtl/gregorian/greg_day_of_year.hpp @@ -0,0 +1,44 @@ +#ifndef GREG_DAY_OF_YEAR_HPP___ +#define GREG_DAY_OF_YEAR_HPP___ +/* Copyright (c) 2000 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/constrained_value.hpp" +#include + +namespace boost { +namespace gregorian { + + //! Exception type for day of year (1..366) + struct bad_day_of_year : public std::out_of_range + { + bad_day_of_year() : + std::out_of_range("Day of year value is out of range 1..366") + {} + }; + + //! A day of the year range (1..366) + typedef CV::simple_exception_policy greg_day_of_year_policies; + + //! Define a range representation type for the day of the year 1..366 + typedef CV::constrained_value greg_day_of_year_rep; + + +} } //namespace gregorian + +/* Copyright (c) 2000 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + + +#endif diff --git a/include/boost/gdtl/gregorian/greg_duration.hpp b/include/boost/gdtl/gregorian/greg_duration.hpp new file mode 100644 index 0000000..3ddd4a3 --- /dev/null +++ b/include/boost/gdtl/gregorian/greg_duration.hpp @@ -0,0 +1,41 @@ +#ifndef GREG_DURATION_HPP___ +#define GREG_DURATION_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/date_duration.hpp" +#include "boost/gdtl/int_adapter.hpp" + + +namespace boost { +namespace gregorian { + + //!An internal date representation that includes infinities, not a date + //typedef gdtl::int_adapter date_duration_rep; + typedef long date_duration_rep; + + //! Durations in days for gregorian system + /*! \ingroup date_basics + */ + typedef gdtl::date_duration date_duration; + // typedef gdtl::date_duration date_duration; + typedef date_duration days; + +} } //namespace gregorian + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + + +#endif diff --git a/include/boost/gdtl/gregorian/greg_facet.hpp b/include/boost/gdtl/gregorian/greg_facet.hpp new file mode 100644 index 0000000..be46cd8 --- /dev/null +++ b/include/boost/gdtl/gregorian/greg_facet.hpp @@ -0,0 +1,76 @@ +#ifndef GREGORIAN_FORMATTERS_HPP___ +#define GREGORIAN_FORMATTERS_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/gregorian/gregorian_types.hpp" +#include "boost/gdtl/date_names_put.hpp" +#include "boost/gdtl/date_formatting_locales.hpp" + +//This file basically becomes a noop if locales are not supported +#ifndef BOOST_NO_STD_LOCALE + +namespace boost { +namespace gregorian { + + //! Configuration of the output facet + struct greg_facet_config + { + typedef boost::gregorian::greg_month month_type; + typedef boost::gdtl::special_values special_value_enum; + typedef boost::gregorian::months_of_year month_enum; + typedef boost::gdtl::weekdays weekday_enum; + }; + + typedef boost::gdtl::date_names_put greg_base_facet; + + template + std::basic_ostream& + operator<<(std::basic_ostream& os, const date& d) + { + typedef boost::gdtl::ostream_date_formatter greg_ostream_formatter; + greg_ostream_formatter::date_put(d, os); + return os; + } + + +} } //namespace gregorian + +#endif + + + + + + + + + + + + + + + + + + + + + + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ +#endif + diff --git a/include/boost/gdtl/gregorian/greg_month.hpp b/include/boost/gdtl/gregorian/greg_month.hpp new file mode 100644 index 0000000..508f475 --- /dev/null +++ b/include/boost/gdtl/gregorian/greg_month.hpp @@ -0,0 +1,62 @@ +#ifndef GREG_MONTH_HPP___ +#define GREG_MONTH_HPP___ +/* Copyright (c) 2000 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/constrained_value.hpp" +#include + + +namespace boost { +namespace gregorian { + + + //! Simple enum to allow for nice programming with Jan, Feb, etc + enum months_of_year {Jan=1,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec,NotAMonth,NumMonths}; + + //! Exception thrown if a greg_month is constructed with a value out of range + struct bad_month : public std::out_of_range + { + bad_month() : std::out_of_range("Month number is out of range 1..12") {} + }; + //! Build a policy class for the greg_month_rep + typedef CV::simple_exception_policy greg_month_policies; + //! A constrained range that implements the gregorian_month rules + typedef CV::constrained_value greg_month_rep; + + + //! Wrapper class to represent months in gregorian based calendar + class greg_month : public greg_month_rep { + public: + typedef months_of_year month_enum; + //! Construct a month from the months_of_year enumeration + greg_month(months_of_year theMonth) : greg_month_rep(theMonth) {} + //! Construct from a short value + greg_month(short theMonth) : greg_month_rep(theMonth) {} + //! Convert the value back to a short + operator unsigned short() const {return value_;} + //! Returns month as number from 1 to 12 + unsigned short as_number() const {return value_;} + month_enum as_enum() const {return static_cast(value_);} + const char* as_short_string() const; + const char* as_long_string() const; + }; + +} } //namespace gregorian + +/* Copyright (c) 2000 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + + +#endif diff --git a/include/boost/gdtl/gregorian/greg_weekday.hpp b/include/boost/gdtl/gregorian/greg_weekday.hpp new file mode 100644 index 0000000..cc23822 --- /dev/null +++ b/include/boost/gdtl/gregorian/greg_weekday.hpp @@ -0,0 +1,64 @@ +#ifndef GREG_WEEKDAY_HPP___ +#define GREG_WEEKDAY_HPP___ +/* Copyright (c) 2000 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/constrained_value.hpp" +#include "boost/gdtl/date_defs.hpp" +#include + +namespace boost { +namespace gregorian { + + //bring enum values into the namespace + using gdtl::Sunday; + using gdtl::Monday; + using gdtl::Tuesday; + using gdtl::Wednesday; + using gdtl::Thursday; + using gdtl::Friday; + using gdtl::Saturday; + + + //! Exception that flags that a weekday number is incorrect + struct bad_weekday : public std::out_of_range + { + bad_weekday() : std::out_of_range("Weekday os out of range 0..6") {} + }; + typedef CV::simple_exception_policy greg_weekday_policies; + typedef CV::constrained_value greg_weekday_rep; + + + //! Represent a day within a week (range 0==Sun to 6==Sat) + class greg_weekday : public greg_weekday_rep { + public: + greg_weekday(unsigned short day_of_week_num) : + greg_weekday_rep(day_of_week_num) + {} + + unsigned short as_number() const {return value_;} + const char* as_short_string() const; + const char* as_long_string() const; + + }; + + + +} } //namespace gregorian + +/* Copyright (c) 2000 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + + +#endif diff --git a/include/boost/gdtl/gregorian/greg_year.hpp b/include/boost/gdtl/gregorian/greg_year.hpp new file mode 100644 index 0000000..4a1ceb4 --- /dev/null +++ b/include/boost/gdtl/gregorian/greg_year.hpp @@ -0,0 +1,59 @@ +#ifndef GREG_YEAR_HPP___ +#define GREG_YEAR_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/constrained_value.hpp" +#include + +namespace boost { +namespace gregorian { + + //! Exception type for gregorian year + struct bad_year : public std::out_of_range + { + bad_year() : + std::out_of_range("Year is out of valid range: 1400..10000") + {} + }; + //! Policy class that declares error handling gregorian year type + typedef CV::simple_exception_policy greg_year_policies; + + //! Generated representation for gregorian year + typedef CV::constrained_value greg_year_rep; + + //! Represent a day of the month (range 1900 - 10000) + /*! This small class allows for simple conversion an integer value into + a year for the gregorian calendar. This currently only allows a + range of 1900 to 10000. Both ends of the range are a bit arbitrary + at the moment, but they are the limits of current testing of the + library. As such they may be increased in the future. + */ + class greg_year : public greg_year_rep { + public: + greg_year(unsigned short year) : greg_year_rep(year) {} + operator unsigned short() const {return value_;} + private: + + }; + + + +} } //namespace gregorian + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + + +#endif diff --git a/include/boost/gdtl/gregorian/greg_ymd.hpp b/include/boost/gdtl/gregorian/greg_ymd.hpp new file mode 100644 index 0000000..1822bf4 --- /dev/null +++ b/include/boost/gdtl/gregorian/greg_ymd.hpp @@ -0,0 +1,40 @@ +#ifndef GDTL_GREG_YMD_HPP__ +#define GDTL_GREG_YMD_HPP__ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/year_month_day.hpp" +#include "boost/gdtl/special_defs.hpp" +#include "boost/gdtl/gregorian/greg_day.hpp" +#include "boost/gdtl/gregorian/greg_year.hpp" +#include "boost/gdtl/gregorian/greg_month.hpp" + +namespace boost { +namespace gregorian { + + typedef gdtl::year_month_day_base greg_year_month_day; + + + +} } //namespace gregorian + + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + + +#endif + diff --git a/include/boost/gdtl/gregorian/gregorian.hpp b/include/boost/gdtl/gregorian/gregorian.hpp new file mode 100644 index 0000000..21f39aa --- /dev/null +++ b/include/boost/gdtl/gregorian/gregorian.hpp @@ -0,0 +1,32 @@ +#ifndef GREGORIAN_HPP__ +#define GREGORIAN_HPP__ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ +/*! @file gregorian.hpp + Single file header that provides overall include for all elements of + the gregorian date-time system. This includes the various types + defined, but also other functions for formatting and parsing. +*/ + + +#include "boost/gdtl/gregorian/gregorian_types.hpp" +#include "boost/gdtl/gregorian/formatters.hpp" +#include "boost/gdtl/gregorian/greg_facet.hpp" +#include "boost/gdtl/gregorian/parsers.hpp" + + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + +#endif diff --git a/include/boost/gdtl/gregorian/gregorian_types.hpp b/include/boost/gdtl/gregorian/gregorian_types.hpp new file mode 100644 index 0000000..cbeb606 --- /dev/null +++ b/include/boost/gdtl/gregorian/gregorian_types.hpp @@ -0,0 +1,77 @@ +#ifndef _GREGORIAN_TYPES_HPP__ +#define _GREGORIAN_TYPES_HPP__ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ +/*! @file gregorian_types.hpp + Single file header that defines most of the types for the gregorian + date-time system. +*/ + + +#include "boost/gdtl/date.hpp" +#include "boost/gdtl/period.hpp" +#include "boost/gdtl/gregorian/greg_calendar.hpp" +#include "boost/gdtl/gregorian/greg_duration.hpp" +#include "boost/gdtl/gregorian/greg_date.hpp" +#include "boost/gdtl/date_generators.hpp" +#include "boost/gdtl/date_clock_device.hpp" +#include "boost/gdtl/date_iterator.hpp" +#include "boost/gdtl/adjust_functors.hpp" + +namespace boost { + +//! Gregorian date system based on gdtl components +/*! This date system defines a full complement of types including + * a date, date_duration, date_period, day_clock, and a + * day_iterator. + */ +namespace gregorian { + //! Date periods for the gregorian system + /*!\ingroup date_basics + */ + typedef gdtl::period date_period; + //! A date generation object type + typedef gdtl::partial_date partial_date; + //! A clock to get the current day from the local computer + /*!\ingroup date_basics + */ + typedef gdtl::day_clock day_clock; + + //! Base date_iterator type for gregorian types. + /*!\ingroup date_basics + */ + typedef gdtl::date_itr_base date_iterator; + + //! A day level iterator + /*!\ingroup date_basics + */ + typedef gdtl::date_itr, + date> day_iterator; + //! A week level iterator + /*!\ingroup date_basics + */ + typedef gdtl::date_itr, + date> week_iterator; + //! A month level iterator + /*!\ingroup date_basics + */ + typedef gdtl::date_itr, + date> month_iterator; +} } //namespace gregorian + + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + +#endif diff --git a/include/boost/gdtl/gregorian/parsers.hpp b/include/boost/gdtl/gregorian/parsers.hpp new file mode 100644 index 0000000..f6a893b --- /dev/null +++ b/include/boost/gdtl/gregorian/parsers.hpp @@ -0,0 +1,62 @@ +#ifndef GREGORIAN_PARSERS_HPP___ +#define GREGORIAN_PARSERS_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/gregorian/gregorian_types.hpp" +#include "boost/gdtl/date_parsing.hpp" + + +namespace boost { +namespace gregorian { + + //! From delimited date string where with order year-month-day eg: 2002-1-25 + inline date from_string(std::string s) { + return gdtl::parse_date(s); + } + + //! From iso type date string where with order year-month-day eg: 20020125 + inline date from_undelimited_string(std::string s) { + return gdtl::parse_undelimited_date(s); + } + +} } //namespace gregorian + + + + + + + + + + + + + + + + + + + + + + + + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ +#endif + diff --git a/include/boost/gdtl/gregorian_calendar.hpp b/include/boost/gdtl/gregorian_calendar.hpp new file mode 100644 index 0000000..ad0c3eb --- /dev/null +++ b/include/boost/gdtl/gregorian_calendar.hpp @@ -0,0 +1,63 @@ +#ifndef GDTL_GREGORIAN_CALENDAR_HPP__ +#define GDTL_GREGORIAN_CALENDAR_HPP__ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + + +namespace boost { +namespace gdtl { + + + //! An implementation of the Gregorian calendar + template + class gregorian_calendar_base { + public: + //! define a type a date split into components + typedef ymd_type_ ymd_type; + //! define a type for representing months + typedef typename ymd_type::month_type month_type; + //! define a type for representing days + typedef typename ymd_type::day_type day_type; + //! Type to hold a stand alone year value (eg: 2002) + typedef typename ymd_type::year_type year_type; + //! Define the integer type to use for internal calculations + typedef date_int_type_ date_int_type; + + + static unsigned short day_of_week(const ymd_type& ymd); + //static unsigned short day_of_year(date_rep_type); + static date_int_type day_number(const ymd_type& ymd); + static ymd_type from_day_number(date_int_type); + static bool is_leap_year(year_type); + static unsigned short end_of_month_day(year_type y, month_type m); + static ymd_type epoch(); + static unsigned short days_in_week(); + + private: + }; + + + +} } //namespace + +#ifdef GDTL_INLINE +#include "gregorian_calendar.ipp" +#endif + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + + +#endif + diff --git a/include/boost/gdtl/gregorian_calendar.ipp b/include/boost/gdtl/gregorian_calendar.ipp new file mode 100644 index 0000000..fbde1f2 --- /dev/null +++ b/include/boost/gdtl/gregorian_calendar.ipp @@ -0,0 +1,140 @@ +/* Copyright (c) 2001 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#ifdef GDTL_INLINE + #undef GDTL_INLINE + #define GDTL_INLINE inline +#else + #define GDTL_INLINE +#endif + +namespace boost { +namespace gdtl { + //! Return the day of the week (0==Sunday, 1==Monday, etc) + /*! Converts a the ymd_type into a day of the week number + */ + template + GDTL_INLINE + unsigned short + gregorian_calendar_base::day_of_week(const ymd_type& ymd) + { + unsigned short a = (14-ymd.month)/12; + unsigned short y = ymd.year - a; + unsigned short m = ymd.month + 12*a - 2; + unsigned short d = (ymd.day + y + (y/4) - (y/100) + (y/400) + (31*m)/12) % 7; + //std::cout << year << "-" << month << "-" << day << " is day: " << d << "\n"; + return d; + } + + //! Convert a ymd_type into a day number + /*! The day number is an absolute number of days since the start of count + */ + template + GDTL_INLINE + date_int_type_ + gregorian_calendar_base::day_number(const ymd_type& ymd) + { + unsigned short a = (14-ymd.month)/12; + unsigned short y = ymd.year + 4800 - a; + unsigned short m = ymd.month + 12*a - 3; + unsigned long d = ymd.day + ((153*m + 2)/5) + 365*y + (y/4) - (y/100) + (y/400) - 32045; + return d; + } + + //! Change a day number into a ymd_type structure + /*! + * + */ + template + GDTL_INLINE + ymd_type_ + gregorian_calendar_base::from_day_number(date_int_type dayNumber) + { + date_int_type a = dayNumber + 32044; + date_int_type b = (4*a + 3)/146097; + date_int_type c = a-((146097*b)/4); + date_int_type d = (4*c + 3)/1461; + date_int_type e = c - (1461*d)/4; + date_int_type m = (5*e + 2)/153; + unsigned short day = e - ((153*m + 2)/5) + 1; + unsigned short month = m + 3 - 12 * (m/10); + date_int_type year = 100*b + d - 4800 + (m/10); + //std::cout << year << "-" << month << "-" << day << "\n"; + + return ymd_type(year,month,day); + } + + + //! Determine if the provided year is a leap year + /*! + *@return true if year is a leap year, false otherwise + */ + template + GDTL_INLINE + bool + gregorian_calendar_base::is_leap_year(year_type year) + { + //divisible by 4, not if divisible by 100, but true if divisible by 400 + return (!(year % 4)) && ((year % 100) || (!(year % 400))); + } + + //! Calculate the last day of the month + /*! Find the day which is the end of the month given year and month + * No error checking is performed. + */ + template + GDTL_INLINE + unsigned short + gregorian_calendar_base::end_of_month_day(year_type year, + month_type month) + { + // switch (month.as_number()) { + switch (month) { + case 2: + if (is_leap_year(year)) {return 29;} + else {return 28;}; + case 4: + case 6: + case 9: + case 11: + return 30; + default: + return 31; + }; + + } + + //! Provide the ymd_type specification for the calandar start + template + GDTL_INLINE + ymd_type_ + gregorian_calendar_base::epoch() + { + return ymd_type(1400,1,1); + } + + //! Defines length of a week for week calculations + template + GDTL_INLINE + unsigned short + gregorian_calendar_base::days_in_week() + { + return 7; + } + + +} } //namespace gregorian + +/* Copyright (c) 2001 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ diff --git a/include/boost/gdtl/int_adapter.hpp b/include/boost/gdtl/int_adapter.hpp new file mode 100644 index 0000000..badec06 --- /dev/null +++ b/include/boost/gdtl/int_adapter.hpp @@ -0,0 +1,212 @@ +#ifndef _GDTL_INT_ADAPTER_HPP__ +#define _GDTL_INT_ADAPTER_HPP__ +/* Copyright (c) 2001 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + + +#include "boost/limits.hpp" //work around compilers without limits +#include "boost/gdtl/special_defs.hpp" + +namespace boost { +namespace gdtl { + +//! Adapter to create integer types with +-infinity, and not a value +/*! This class is used internally in counted date/time representations. + * It adds the floating point like features of infinities and + * not a number. + */ + +template +class int_adapter { +public: + typedef int_type_ int_type; + int_adapter(int_type v) : + value_(v) + {} + static bool has_infinity() + { + return true; + } + static const int_adapter pos_infinity() + { + return ::std::numeric_limits::max(); + } + static const int_adapter neg_infinity() + { + return ::std::numeric_limits::min(); + } + static const int_adapter not_a_number() + { + return ::std::numeric_limits::max()-1; + } + static int_adapter max() + { + return ::std::numeric_limits::max()-2; + } + static int_adapter min() + { + return ::std::numeric_limits::min()+1; + } + static int_adapter from_special(special_values sv) + { + switch (sv) { + case not_a_date_time: return not_a_number(); + case neg_infin: return neg_infinity(); + case pos_infin: return pos_infinity(); + case max_date_time: return max(); + case min_date_time: return min(); + default: return not_a_number(); + } + + } + static bool is_inf(int_type v) + { + return (v == neg_infinity().as_number() || + v == pos_infinity().as_number()); + } + static bool is_neg_infinity(int_type v) + { + return (v == neg_infinity().as_number()); + } + static bool is_pos_infinity(int_type v) + { + return (v == pos_infinity().as_number()); + } + static bool is_not_a_number(int_type v) + { + return (v == not_a_number().as_number()); + } + //! Returns either special value type or is_not_special + static special_values to_special(int_type v) + { + if (is_not_a_number(v)) return not_a_date_time; + if (is_neg_infinity(v)) return neg_infin; + if (is_pos_infinity(v)) return pos_infin; + return not_special; + } + + + //-3 leaves room for representations of infinity and not a date + static int_type maxcount() + { + return ::std::numeric_limits::max()-3; + } + bool is_infinity() const + { + return (value_ == neg_infinity().as_number() || + value_ == pos_infinity().as_number()); + } + bool is_nan() const + { + return (value_ == not_a_number().as_number()); + } + bool operator==(const int_adapter& rhs) const + { + return value_ == rhs.value_; + } + bool operator!=(const int_adapter& rhs) const + { + return value_ != rhs.value_; + } + bool operator<(const int_adapter& rhs) const + { + return value_ < rhs.value_; + } + bool operator>(const int_adapter& rhs) const + { + return value_ > rhs.value_; + } + int_type as_number() const + { + return value_; + } + //! Returns either special value type or is_not_special + special_values as_special() const + { + return int_adapter::to_special(value_); + } + //creates nasty ambiguities +// operator int_type() const +// { +// return value_; +// } + int_adapter operator+(const int_adapter& rhs) const + { + if (is_nan() || rhs.is_nan()) { + return int_adapter(not_a_number()); + } + if (is_infinity()) { + return *this; + } + if (rhs.is_infinity()) { + return rhs; + } + return value_ + rhs.value_; + } + + int_adapter operator+(int_type rhs) const + { + if (is_nan()) { + return int_adapter(not_a_number()); + } + if (is_infinity()) { + return *this; + } + return value_ + rhs; + } + + int_adapter operator-(const int_adapter& rhs) const + { + if (is_nan() || rhs.is_nan()) { + return int_adapter(not_a_number()); + } + if (is_infinity()) { + return *this; + } + if (rhs.is_infinity()) { + return rhs; + } + return int_adapter(value_ - rhs.value_); + } + + int_adapter operator-(int_type rhs) const + { + if (is_nan()) { + return int_adapter(not_a_number()); + } + if (is_infinity()) { + return *this; + } + return int_adapter(value_ - rhs); + } + + + +private: + int_type value_; +}; + +} } //namespace gdtl + +/* + * Copyright (c) 2001 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided as is without express or implied warranty. + * + * + * Author: Jeff Garland (jeff@CrystalClearSoftware.com) + * Created: Sat Sep 8 19:37:11 2001 + * + */ + + +#endif diff --git a/include/boost/gdtl/iso_format.hpp b/include/boost/gdtl/iso_format.hpp new file mode 100644 index 0000000..26e3093 --- /dev/null +++ b/include/boost/gdtl/iso_format.hpp @@ -0,0 +1,177 @@ +#ifndef ISO_FORMAT_HPP___ +#define ISO_FORMAT_HPP___ +/* Copyright (c) 2001 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/parse_format_base.hpp" + +namespace boost { +namespace gdtl { + +//! Class to provide common iso formatting spec +class iso_format_base { +public: + //! Describe month format -- its an integer in iso format + static month_format_spec month_format() + { + return month_as_integer; + } + + //! String used printed is date is invalid + static const char* not_a_date() + { //20010102 + return "NotADate"; + } + //! String used to for positive infinity value + static const char* pos_infinity() + { + return "+infin "; + } + //! String used to for positive infinity value + static const char* neg_infinity() + { + return "-infin "; + } + + //! ISO char for a year -- used in durations + static char year_sep_char() + { + return 'Y'; + } + //! ISO char for a month + static char month_sep_char() + { + return '-'; + } + //! ISO char for a day + static char day_sep_char() + { + return '-'; + } + //! char for minute + static char hour_sep_char() + { + return ':'; + } + //! char for minute + static char minute_sep_char() + { + return ':'; + } + //! char for second + static char second_sep_char() + { + return ':'; + } + //! ISO char for a period + static char period_start_char() + { + return 'P'; + } + //! Used in time in mixed strings to set start of time + static char time_start_char() + { + return 'T'; + } + + //! Used in mixed strings to identify start of a week number + static char week_start_char() + { + return 'W'; + } + + //! Separators for periods + static char period_sep_char() + { + return '/'; + } + //! Separator for hh:mm:ss + static char time_sep_char() + { + return ':'; + } + //! Preferred Separator for hh:mm:ss,decimal_fraction + static char fractional_time_sep_char() + { + return ','; + } + + static bool is_component_sep(char sep) + { + switch(sep) { + case 'H': + case 'M': + case 'S': + case 'W': + case 'T': + case 'Y': + case 'D':return true; + default: + return false; + } + } + + static bool is_fractional_time_sep(char sep) + { + switch(sep) { + case ',': + case '.': return true; + default: return false; + } + } + static bool is_timezone_sep(char sep) + { + switch(sep) { + case '+': + case '-': return true; + default: return false; + } + } + static char element_sep_char() + { + return '-'; + } + +}; + + +//! Format description for iso normal YYYYMMDD +class iso_format : public iso_format_base { +public: + //! The ios standard format doesn't use char separators + static bool has_date_sep_chars() + { + return false; + } +}; + +//! Extended format uses seperators YYYY-MM-DD +class iso_extended_format : public iso_format_base { +public: + //! Extended format needs char separators + static bool has_date_sep_chars() + { + return true; + } + +}; + +} } //namespace gdtl + +/* Copyright (c) 2001 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + + + +#endif diff --git a/include/boost/gdtl/local_time_adjustor.hpp b/include/boost/gdtl/local_time_adjustor.hpp new file mode 100644 index 0000000..b5134e0 --- /dev/null +++ b/include/boost/gdtl/local_time_adjustor.hpp @@ -0,0 +1,222 @@ + +#ifndef GDTL_LOCAL_TIME_ADJUSTOR_HPP__ +#define GDTL_LOCAL_TIME_ADJUSTOR_HPP__ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +/*! @file local_time_adjustor.hpp + Time adjustment calculations for local times +*/ + +#include "boost/gdtl/date_generators.hpp" +#include "boost/gdtl/dst_rules.hpp" +#include + +namespace boost { +namespace gdtl { + + + //! Provides a base offset adjustment from utc + template + class utc_adjustment + { + public: + static time_duration_type local_to_utc_base_offset() + { + time_duration_type td(hours,minutes,0); + return td.invert_sign(); + } + static time_duration_type utc_to_local_base_offset() + { + return time_duration_type(hours,minutes,0); + } + }; + + + + //! Allow sliding utc adjustment with fixed dst rules + template + class dynamic_local_time_adjustor : public dst_rules + { + public: + typedef typename time_type::time_duration_type time_duration_type; + typedef typename time_type::date_type date_type; + + dynamic_local_time_adjustor(time_duration_type utc_offset) : + utc_offset_(utc_offset) + {} + + //! Presumes local time + time_duration_type utc_offset(bool is_dst) + { + if (is_dst) { + return utc_offset_ + dst_offset(); + } + else { + return utc_offset_; + } + + } + private: + time_duration_type utc_offset_; + + }; + + + + //! Embed the rules for local time adjustments at compile time + template + class static_local_time_adjustor: public dst_rules, public utc_offset_rules + { + public: + typedef typename time_type::time_duration_type time_duration_type; + typedef typename time_type::date_type date_type; + + //! Calculates the offset from a utc time to local based on dst and utc offset + /*! @param t UTC time to calculate offset to local time + * This adjustment depends on the following observations about the + * workings of the DST boundary offset. Since UTC time labels are + * monotonically increasing we can determine if a given local time + * is in DST or not and therefore adjust the offset appropriately. + * + * The logic is as follows. Starting with UTC time use the offset to + * create a label for an non-dst adjusted local time. Then call + * dst_rules::local_is_dst with the non adjust local time. The + * results of this function will either unabiguously decide that + * the initial local time is in dst or return an illegal or + * ambiguous result. An illegal result only occurs at the end + * of dst (where labels are skipped) and indicates that dst has + * ended. An ambiguous result means that we need to recheck by + * making a dst adjustment and then rechecking. If the dst offset + * is added to the utc time and the recheck proves non-ambiguous + * then we are past the boundary. If it is still ambiguous then + * we are ahead of the boundary and dst is still in effect. + * + * TODO -- check if all dst offsets are positive. If not then + * the algorithm needs to check for this and reverse the + * illegal/ambiguous logic. + */ + static time_duration_type utc_to_local_offset(const time_type& t) + { + //get initial local time guess by applying utc offset + time_type initial = t + utc_to_local_base_offset(); + time_is_dst_result dst_flag = + dst_rules::local_is_dst(initial.date(), initial.time_of_day()); + switch(dst_flag) { + case is_in_dst: return utc_to_local_base_offset() + dst_offset(); + case is_not_in_dst: return utc_to_local_base_offset(); + case invalid_time_label:return utc_to_local_base_offset() + dst_offset(); + case ambiguous: { + time_type retry = initial + dst_offset(); + dst_flag = dst_rules::local_is_dst(retry.date(), retry.time_of_day()); + //if still ambibuous then the utc time still translates to a dst time + if (dst_flag == ambiguous) { + return utc_to_local_base_offset() + dst_offset(); + } + // we are past the dst boundary + else { + return utc_to_local_base_offset(); + } + } + }//case + //TODO better excpetion type + throw std::out_of_range("Unreachable case"); + + } + + //! Get the offset to UTC given a local time + static time_duration_type local_to_utc_offset(const time_type& t, + gdtl::dst_flags dst=gdtl::calculate) + { + switch (dst) { + case is_dst: + return local_to_utc_base_offset() - dst_offset(); + case not_dst: + return local_to_utc_base_offset(); + case calculate: + time_is_dst_result res = + dst_rules::local_is_dst(t.date(), t.time_of_day()); + switch(res) { + case is_in_dst: return local_to_utc_base_offset() - dst_offset(); + case is_not_in_dst: return local_to_utc_base_offset(); + case ambiguous: return local_to_utc_base_offset(); + case invalid_time_label: throw std::out_of_range("Time label invalid"); + } + } + throw std::out_of_range("Time label invalid"); + } + + + private: + + }; + + void dummy_to_prevent_msvc6_ice(); //why ask why? + + //! Template that simplifies the creation of local time calculator + /*! Use this template to create the timezone to utc convertors as required. + * + * This class will also work for other regions that don't use dst and + * have a utc offset which is an integral number of hours. + * + * Template Parameters + * -time_type -- Time class to use + * -utc_offset -- Number hours local time is adjust from utc + * -use_dst -- true (default) if region uses dst, false otherwise + * For example: + * @code + * //eastern timezone is utc-5 + typedef gdtl::us_local_adjustor us_eastern; + typedef gdtl::us_local_adjustor us_central; + typedef gdtl::us_local_adjustor us_mountain; + typedef gdtl::us_local_adjustor us_pacific; + typedef gdtl::us_local_adjustor us_arizona; + @endcode + + */ + template + class local_adjustor + { + public: + typedef typename time_type::time_duration_type time_duration_type; + typedef typename time_type::date_type date_type; + typedef static_local_time_adjustor > dst_adjustor; + //! Convert a utc time to local time + static time_type utc_to_local(const time_type& t) + { + time_duration_type td = dst_adjustor::utc_to_local_offset(t); + return t + td; + } + //! Convert a local time to utc + static time_type local_to_utc(const time_type& t, + gdtl::dst_flags dst=gdtl::calculate) + { + time_duration_type td = dst_adjustor::local_to_utc_offset(t, dst); + return t + td; + } + }; + + +} } //namespace gdtl + + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + */ + +#endif diff --git a/include/boost/gdtl/parse_format_base.hpp b/include/boost/gdtl/parse_format_base.hpp new file mode 100644 index 0000000..b69382e --- /dev/null +++ b/include/boost/gdtl/parse_format_base.hpp @@ -0,0 +1,34 @@ + +#ifndef GDTL_PARSE_FORMAT_BASE_HPP___ +#define GDTL_PARSE_FORMAT_BASE_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +namespace boost { +namespace gdtl { + + //! Enum for distinguishing parsing and formatting options + enum month_format_spec {month_as_integer, month_as_short_string, + month_as_long_string}; + + enum ymd_order_spec {ymd_order_iso, //order is year-month-day + ymd_order_dmy, //day-month-year + ymd_order_us}; //order is month-day-year + + +} }//namespace gdtl + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ +#endif diff --git a/include/boost/gdtl/period.hpp b/include/boost/gdtl/period.hpp new file mode 100644 index 0000000..d580cf2 --- /dev/null +++ b/include/boost/gdtl/period.hpp @@ -0,0 +1,253 @@ + +#ifndef PERIOD_HPP___ +#define PERIOD_HPP___ +/* Copyright (c) 2000 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +/*! \file period.hpp + This file contain the implementation of the period abstraction. This is + basically the same idea as a range. Although this class is intended for + use in the time library, it is pretty close to general enough for other + numeric uses. + +*/ + +#include "boost/operators.hpp" + + +namespace boost { +namespace gdtl { + //!Provides generalized period type useful in date-time systems + /*!This template uses a class to represent a time point within the period + and another class to represent a duration. As a result, this class is + not appropriate for use when the number and duration representation + are the same (eg: in the regular number domain). + + A period can be specified by providing either the starting point and + a duration or the starting point and the last point. A period + will always have a duration of at least 1 and it's start will always + be before or eqaul to the last. + + In the case that the begin and last are the same, the period has a + length of one unit. For example, suppose this is a period of days. + That is, each "point" represents a single day. If the start and the + last is the same day then the period represents that single day for + a length of one. The same applies if each "point" represents a month + or a year. The way to think of this is that the granularity of the + point_rep class is similar to the ticks on a ruler. The more ticks, + the finer the resolution of a range that can be defined. A range + defined on a ruler with 1cm resolution between the 1cm mark and the + 2cm mark is 1cm long. In the ruler range, the 1cm mark is in the + range while the 2cm mark is not. + + While the ruler analogy useful, it is not how date ranges are naturally + thought about (at least by me). That is, it is more natural to think + of a date as including up to the end of the second time point. So when + I say day 1 to day 2 I usually mean from the beginning of day 1 to the + end of day 2. + + The best way to handle periods is usually to provide a start point and + a duration. So, day1 + 7 days is a week period which includes all of the + first day and 6 more days (eg: Sun to Sat). + + */ + template + class period + : boost::less_than_comparable + , boost::equality_comparable< period + > > + { + public: + period(point_rep begin, point_rep last); + period(point_rep begin, duration_rep len); + point_rep begin() const; + point_rep end() const; + point_rep last() const; + bool is_null() const; + bool operator==(const period& rhs) const; + bool operator<(const period& rhs) const; + void shift(const duration_rep& d); + bool contains(const point_rep& point) const; + bool contains(const period& other) const; + bool intersects(const period& other) const; + period intersection(const period& other) const; + period merge(const period& other) const; + private: + point_rep begin_; + point_rep last_; + }; + + //! create a period from begin to last eg: [begin,end) + /*! If last <= begin then the period will is defined as null + */ + template + inline + period::period(point_rep begin, + point_rep end) : + begin_(begin), + last_(end - duration_rep::unit()) + {} + + //! create a period as [begin, begin+len) + /*! If len is <= 0 then the period will be defined as null + */ + template + inline + period::period(point_rep begin, duration_rep len) : + begin_(begin), + last_(begin + len-duration_rep::unit()) + {} + + + //! Return the first element in the period + template + inline + point_rep period::begin() const + { + return begin_; + } + + //! Return one past the last element + template + inline + point_rep period::end() const + { + return last_ + duration_rep::unit(); + } + + //! Return the last item in the period + template + inline + point_rep period::last() const + { + return last_; + } + + //! True if period is ill formed + template + inline + bool period::is_null() const + { + return last_ <= begin_; + } + + //! Equality operator + template + inline + bool period::operator==(const period& rhs) const + { + return ((begin_ == rhs.begin_) && + (last_ == rhs.last_)); + } + + //! Strict as defined by rhs.last <= lhs.last + template + inline + bool period::operator<(const period& rhs) const + { + return (last_ <= rhs.begin_); + } + + + //! Shift the start and end by the specified amount + template + inline + void period::shift(const duration_rep& d) + { + begin_ = begin_ + d; + last_ = last_ + d; + } + + //! True if the point is inside the period + template + inline + bool period::contains(const point_rep& point) const + { + return ((point >= begin_) && + (point <= last_)); + } + + + //! True if this period fully contains (or equals) the other period + template + inline + bool period::contains(const period& other) const + { + return ((begin_ <= other.begin_) && (last_ >= other.last_)); + } + + + //! True if the periods overlap in any way + template + inline + bool period::intersects(const period& other) const + { + return ( contains(other.begin_) || + other.contains(begin_) || + ((other.begin_ < begin_) && (other.last_ >= begin_)));// || + // ((begin_ < other.begin_) && (last_ <= other.last_))); + + } + + //! Returns the period of intersection or invalid range no intersection + template + inline + period + period::intersection(const period& other) const + { + if (begin_ > other.begin_) { + if (last_ <= other.last_) { //case2 + return *this; + } + //case 1 + return period(begin_, other.end()); + + } + else { + if (last_ <= other.last_) { //case3 + return period(other.begin_, this->end()); + } + //case4 + return other; + } + //unreachable + } + + //! Returns the union of intersecting periods -- or null period + /*! + */ + template + inline + period + period::merge(const period& other) const + { + if (this->intersects(other)) { + if (begin_ < other.begin_) { + return period(begin_, last_ > other.last_ ? this->end() : other.end()); + } + + return period(other.begin_, last_ > other.last_ ? this->end() : other.end()); + + } + return period(begin_,begin_); // no intersect return null + } + + +} } //namespace gdtl + +/* Copyright (c) 2000 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + + +#endif diff --git a/include/boost/gdtl/posix_time/posix_time.hpp b/include/boost/gdtl/posix_time/posix_time.hpp new file mode 100644 index 0000000..96da39f --- /dev/null +++ b/include/boost/gdtl/posix_time/posix_time.hpp @@ -0,0 +1,30 @@ +#ifndef POSIX_TIME_HPP___ +#define POSIX_TIME_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ +//!@file Global header file to get all of posix time types + +#include "boost/gdtl/posix_time/ptime.hpp" +#include "boost/gdtl/posix_time/time_formatters.hpp" +#include "boost/gdtl/posix_time/time_parsers.hpp" + + + + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + */ + +#endif + diff --git a/include/boost/gdtl/posix_time/posix_time_config.hpp b/include/boost/gdtl/posix_time/posix_time_config.hpp new file mode 100644 index 0000000..836033d --- /dev/null +++ b/include/boost/gdtl/posix_time/posix_time_config.hpp @@ -0,0 +1,144 @@ +#ifndef POSIX_TIME_CONFIG_HPP___ +#define POSIX_TIME_CONFIG_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/time_duration.hpp" +#include "boost/gdtl/time_resolution_traits.hpp" +#include "boost/gdtl/gregorian/gregorian_types.hpp" +#include "boost/gdtl/wrapping_int.hpp" +#include "boost/limits.hpp" +//force the definition of INT64_C macro used in posix_time_system +#ifndef __STDC_CONSTANT_MACROS +#define __STDC_CONSTANT_MACROS +# if __GNUC__ >= 2 +# undef BOOST_HAS_STDINT_H +# endif +#endif +#include "boost/cstdint.hpp" +#include +#include //for MCW 7.2 std::abs(long long) + +namespace boost { +namespace posix_time { + +//Remove the following line if you want 64 bit millisecond resolution time +//#define BOOST_GDTL_POSIX_TIME_STD_CONFIG + +#ifdef BOOST_GDTL_POSIX_TIME_STD_CONFIG + // set up conditional test compilations +#define BOOST_GDTL_HAS_MILLISECONDS +#define BOOST_GDTL_HAS_MICROSECONDS +#define BOOST_GDTL_HAS_NANOSECONDS + typedef gdtl::time_resolution_traits time_res_traits; +#else + // set up conditional test compilations +#define BOOST_GDTL_HAS_MILLISECONDS +#define BOOST_GDTL_HAS_MICROSECONDS +#undef BOOST_GDTL_HAS_NANOSECONDS + typedef gdtl::time_resolution_traits time_res_traits; + + +// #undef BOOST_GDTL_HAS_MILLISECONDS +// #undef BOOST_GDTL_HAS_MICROSECONDS +// #undef BOOST_GDTL_HAS_NANOSECONDS +// typedef gdtl::time_resolution_traits time_res_traits; + +#endif + + + //! Base time duration type + /*! \ingroup time_basics + */ + class time_duration : + public gdtl::time_duration + { + public: + typedef time_res_traits rep_type; + typedef time_res_traits::day_type day_type; + typedef time_res_traits::hour_type hour_type; + typedef time_res_traits::min_type min_type; + typedef time_res_traits::sec_type sec_type; + typedef time_res_traits::fractional_seconds_type fractional_seconds_type; + typedef time_res_traits::tick_type tick_type; + time_duration(hour_type hour, + min_type min, + sec_type sec, + fractional_seconds_type fs=0) : + gdtl::time_duration(hour,min,sec,fs) + {} + time_duration() : + gdtl::time_duration(0,0,0) + {} + //Give duration access to ticks constructor -- hide from users + friend class gdtl::time_duration; + private: + explicit time_duration(tick_type ticks) : + gdtl::time_duration(ticks) + {} + }; + +#ifdef BOOST_GDTL_POSIX_TIME_STD_CONFIG + + //! Simple implementation for the time rep + struct simple_time_rep + { + typedef gregorian::date date_type; + typedef time_duration time_duration_type; + simple_time_rep(date_type d, time_duration_type tod) : + day(d), + time_of_day(tod) + {} + date_type day; + time_duration_type time_of_day; + }; + + class posix_time_system_config + { + public: + typedef simple_time_rep time_rep_type; + typedef gregorian::date date_type; + typedef gregorian::date_duration date_duration_type; + typedef time_duration time_duration_type; + typedef time_res_traits::tick_type int_type; + typedef time_res_traits resolution_traits; + }; + +#else + + class millisec_posix_time_system_config + { + public: + typedef boost::int64_t time_rep_type; + typedef gregorian::date date_type; + typedef gregorian::date_duration date_duration_type; + typedef time_duration time_duration_type; + typedef time_res_traits::tick_type int_type; + typedef time_res_traits resolution_traits; + }; + +#endif + +} }//namespace posix_time + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + */ + +#endif + + diff --git a/include/boost/gdtl/posix_time/posix_time_duration.hpp b/include/boost/gdtl/posix_time/posix_time_duration.hpp new file mode 100644 index 0000000..2c15923 --- /dev/null +++ b/include/boost/gdtl/posix_time/posix_time_duration.hpp @@ -0,0 +1,100 @@ +#ifndef POSIX_TIME_DURATION_HPP___ +#define POSIX_TIME_DURATION_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/posix_time/posix_time_config.hpp" + +namespace boost { +namespace posix_time { + + //! Allows expression of durations as an hour count + /*! \ingroup time_basics + */ + class hours : public time_duration + { + public: + explicit hours(long h) : + time_duration(h,0,0) + {} + }; + + //! Allows expression of durations as a minute count + /*! \ingroup time_basics + */ + class minutes : public time_duration + { + public: + explicit minutes(long m) : + time_duration(0,m,0) + {} + }; + + //! Allows expression of durations as a seconds count + /*! \ingroup time_basics + */ + class seconds : public time_duration + { + public: + explicit seconds(long s) : + time_duration(0,0,s) + {} + }; + + + +#if defined(BOOST_GDTL_HAS_NANOSECONDS) + + //! Allows expression of durations as milli seconds + /*! \ingroup time_basics + */ + typedef gdtl::subsecond_duration millisec; + + //! Allows expression of durations as micro seconds + /*! \ingroup time_basics + */ + typedef gdtl::subsecond_duration microsec; + + //! Allows expression of durations as nano seconds + /*! \ingroup time_basics + */ + typedef gdtl::subsecond_duration nanosec; + + +#endif + +#if defined(BOOST_GDTL_HAS_MICROSECONDS) && (!defined(BOOST_GDTL_HAS_NANOSECONDS)) + + //! Allows expression of durations as milli seconds + /*! \ingroup time_basics + */ + typedef gdtl::subsecond_duration millisec; + + //! Allows expression of durations as micro seconds + /*! \ingroup time_basics + */ + typedef gdtl::subsecond_duration microsec; + +#endif + + + +} }//namespace posix_time + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + */ + +#endif + diff --git a/include/boost/gdtl/posix_time/posix_time_system.hpp b/include/boost/gdtl/posix_time/posix_time_system.hpp new file mode 100644 index 0000000..b3708d5 --- /dev/null +++ b/include/boost/gdtl/posix_time/posix_time_system.hpp @@ -0,0 +1,72 @@ + + +#ifndef POSIX_TIME_SYSTEM_HPP___ +#define POSIX_TIME_SYSTEM_HPP___ +/* Copyright (c) 2000 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/posix_time/posix_time_config.hpp" +#include "boost/gdtl/time_system_split.hpp" +#include "boost/gdtl/time_system_counted.hpp" + + +namespace boost { +namespace posix_time { + +#ifdef BOOST_GDTL_POSIX_TIME_STD_CONFIG + + typedef gdtl::split_timedate_system posix_time_system; + +#else + + typedef gdtl::counted_time_rep int64_time_rep; + typedef gdtl::counted_time_system posix_time_system; + +#endif + +} }//namespace posix_time + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + */ + +#endif + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/include/boost/gdtl/posix_time/posix_time_types.hpp b/include/boost/gdtl/posix_time/posix_time_types.hpp new file mode 100644 index 0000000..e676145 --- /dev/null +++ b/include/boost/gdtl/posix_time/posix_time_types.hpp @@ -0,0 +1,56 @@ +#ifndef POSIX_TIME_TYPES_HPP___ +#define POSIX_TIME_TYPES_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/time_clock.hpp" +//#include "gdtl/high_precision_clock.hpp" +#include "boost/gdtl/posix_time/ptime.hpp" +#include "boost/gdtl/posix_time/posix_time_duration.hpp" +#include "boost/gdtl/posix_time/posix_time_system.hpp" +#include "boost/gdtl/posix_time/time_period.hpp" +#include "boost/gdtl/time_iterator.hpp" +#include "boost/gdtl/dst_rules.hpp" + +namespace boost { + +//!Defines a non-adjusted time system with nano-second resolution and stable calculation properties +namespace posix_time { + + //! Iterator over a defined time duration + /*! \ingroup time_basics + */ + typedef gdtl::time_itr time_iterator; + //! A time clock that has a resolution of one second + /*! \ingroup time_basics + */ + typedef gdtl::second_clock second_clock; + // typedef gdtl::high_precision_clock high_precision_clock; + + //! Define a dst null dst rule for the posix_time system + typedef gdtl::null_dst_rules no_dst; + //! Define US dst rule calculator for the posix_time system + typedef gdtl::us_dst_rules us_dst; + + +} } //namespace posix_time + + + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + */ + +#endif + diff --git a/include/boost/gdtl/posix_time/ptime.hpp b/include/boost/gdtl/posix_time/ptime.hpp new file mode 100644 index 0000000..21ac4ef --- /dev/null +++ b/include/boost/gdtl/posix_time/ptime.hpp @@ -0,0 +1,55 @@ +#ifndef POSIX_PTIME_HPP___ +#define POSIX_PTIME_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/posix_time/posix_time_system.hpp" +#include "boost/gdtl/time.hpp" + +namespace boost { + +namespace posix_time { + + //! Time type with no timezone or other adjustments + /*! \ingroup time_basics + */ + class ptime : public gdtl::gdtl_time + { + public: + typedef posix_time_system time_system_type; + typedef time_system_type::time_rep_type time_rep_type; + typedef time_system_type::time_duration_type time_duration_type; + typedef ptime time_type; + //! Construct with date and offset in day + ptime(gregorian::date d,time_duration_type td) : gdtl::gdtl_time(d,td) + {} + //! Construct a time at start of the given day (midnight) + explicit ptime(gregorian::date d) : gdtl::gdtl_time(d,time_duration_type(0,0,0)) + {} + //! Copy from time_rep + ptime(const time_rep_type& rhs): + gdtl::gdtl_time(rhs) + {} + }; + + + +} }//namespace posix_time + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + */ + +#endif + diff --git a/include/boost/gdtl/posix_time/time_formatters.hpp b/include/boost/gdtl/posix_time/time_formatters.hpp new file mode 100644 index 0000000..303edbc --- /dev/null +++ b/include/boost/gdtl/posix_time/time_formatters.hpp @@ -0,0 +1,129 @@ +#ifndef POSIXTIME_FORMATTERS_HPP___ +#define POSIXTIME_FORMATTERS_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/gregorian/gregorian.hpp" +#include "boost/gdtl/iso_format.hpp" +#include "boost/gdtl/date_format_simple.hpp" +#include "boost/gdtl/posix_time/posix_time_types.hpp" + + +namespace boost { + +namespace posix_time { + + //! Time duration to string hh::mm::ss.fffffff. Example: 10:09:03.0123456 + /*!\ingroup time_format + */ + inline std::string to_simple_string(time_duration td) { + std::ostringstream ss; + ss << std::setw(2) << std::setfill('0') << td.hours() << ":"; + ss << std::setw(2) << std::setfill('0') << td.minutes() << ":"; + ss << std::setw(2) << std::setfill('0') << td.seconds(); + //TODO the following is totally non-generic, yelling FIXME +#if (defined(BOOST_MSVC) && (_MSC_VER <= 1200)) // 1200 == VC++ 6.0 + boost::int64_t frac_sec = td.fractional_seconds(); +// JDG [7/6/02 VC++ compatibility] + char buff[32]; + _i64toa(frac_sec, buff, 10); +#else + time_duration::fractional_seconds_type frac_sec = td.fractional_seconds(); +#endif + if (frac_sec != 0) { + ss << "." << std::setw(time_duration::num_fractional_digits()) + << std::setfill('0') + +// JDG [7/6/02 VC++ compatibility] +#if (defined(BOOST_MSVC) && (_MSC_VER <= 1200)) // 1200 == VC++ 6.0 + << buff; +#else + << frac_sec; +#endif + } + return ss.str(); + } + + //! Time duration in iso format hhmmss,fffffff Example: 10:09:03,0123456 + /*!\ingroup time_format + */ + inline std::string to_iso_string(time_duration td) { + std::ostringstream ss; + ss << std::setw(2) << std::setfill('0') << td.hours(); + ss << std::setw(2) << std::setfill('0') << td.minutes(); + ss << std::setw(2) << std::setfill('0') << td.seconds(); + //TODO the following is totally non-generic, yelling FIXME +#if (defined(BOOST_MSVC) && (_MSC_VER <= 1200)) // 1200 == VC++ 6.0 + boost::int64_t frac_sec = td.fractional_seconds(); + // JDG [7/6/02 VC++ compatibility] + char buff[32]; + _i64toa(frac_sec, buff, 10); +#else + time_duration::fractional_seconds_type frac_sec = td.fractional_seconds(); +#endif + if (frac_sec != 0) { + ss << "." << std::setw(time_duration::num_fractional_digits()) + << std::setfill('0') + +// JDG [7/6/02 VC++ compatibility] +#if (defined(BOOST_MSVC) && (_MSC_VER <= 1200)) // 1200 == VC++ 6.0 + << buff; +#else + << frac_sec; +#endif + } + return ss.str(); + } + + //! Time to simple format CCYY-mmm-dd hh:mm:ss.fffffff + /*!\ingroup time_format + */ + inline std::string to_simple_string(ptime t) { + std::string ts = gregorian::to_simple_string(t.date()) + " "; + return ts + to_simple_string(t.time_of_day()); + } + + //! Convert to string of form [YYYY-mmm-DD HH:MM::SS.ffffff/YYYY-mmm-DD HH:MM::SS.fffffff] + /*!\ingroup time_format + */ + inline std::string to_simple_string(time_period tp) { + std::string d1(to_simple_string(tp.begin())); + std::string d2(to_simple_string(tp.last())); + return std::string("[" + d1 + "/" + d2 +"]"); + } + + //! Convert iso short form YYYYMMDDTHHMMSS where T is the date-time separator + /*!\ingroup time_format + */ + inline std::string to_iso_string(ptime t) { + std::string ts = gregorian::to_iso_string(t.date()) + "T"; + return ts + to_iso_string(t.time_of_day()); + } + + //! Convert to form YYYY-MM-DDTHH:MM:SS where T is the date-time separator + /*!\ingroup time_format + */ + inline std::string to_iso_extended_string(ptime t) { + std::string ts = gregorian::to_iso_extended_string(t.date()) + "T"; + return ts + to_simple_string(t.time_of_day()); + } + + +} } //namespace posix_time + + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ +#endif + diff --git a/include/boost/gdtl/posix_time/time_parsers.hpp b/include/boost/gdtl/posix_time/time_parsers.hpp new file mode 100644 index 0000000..a5d14ea --- /dev/null +++ b/include/boost/gdtl/posix_time/time_parsers.hpp @@ -0,0 +1,42 @@ +#ifndef POSIXTIME_PARSERS_HPP___ +#define POSIXTIME_PARSERS_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/gregorian/gregorian.hpp" +#include "boost/gdtl/time_parsing.hpp" +#include "boost/gdtl/posix_time/posix_time_types.hpp" + + +namespace boost { + +namespace posix_time { + + inline time_duration duration_from_string(const std::string& s) { + return gdtl::parse_delimited_time_duration(s); + } + + inline ptime time_from_string(const std::string& s) { + return gdtl::parse_delimited_time(s, ' '); + } + + + +} } //namespace posix_time + + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ +#endif + diff --git a/include/boost/gdtl/posix_time/time_period.hpp b/include/boost/gdtl/posix_time/time_period.hpp new file mode 100644 index 0000000..386ef17 --- /dev/null +++ b/include/boost/gdtl/posix_time/time_period.hpp @@ -0,0 +1,37 @@ +#ifndef POSIX_TIME_PERIOD_HPP___ +#define POSIX_TIME_PERIOD_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/gdtl/period.hpp" +#include "boost/gdtl/posix_time/posix_time_duration.hpp" +#include "boost/gdtl/posix_time/ptime.hpp" + +namespace boost { +namespace posix_time { + + //! Time period type + /*! \ingroup time_basics + */ + typedef gdtl::period time_period; + + +} }//namespace posix_time + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + */ + +#endif + diff --git a/include/boost/gdtl/special_defs.hpp b/include/boost/gdtl/special_defs.hpp new file mode 100644 index 0000000..772057d --- /dev/null +++ b/include/boost/gdtl/special_defs.hpp @@ -0,0 +1,32 @@ +#ifndef GDTL_SPECIAL_DEFS_HPP__ +#define GDTL_SPECIAL_DEFS_HPP__ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +namespace boost { +namespace gdtl { + + enum special_values {not_a_date_time, + neg_infin, pos_infin, + min_date_time, max_date_time, + not_special}; + + +} } //namespace gdtl + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + +#endif + diff --git a/include/boost/gdtl/testfrmwk.hpp b/include/boost/gdtl/testfrmwk.hpp new file mode 100644 index 0000000..de2505b --- /dev/null +++ b/include/boost/gdtl/testfrmwk.hpp @@ -0,0 +1,74 @@ + +#ifndef TEST_FRMWK_HPP___ +#define TEST_FRMWK_HPP___ +/* + * Copyright (c) 2000 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + * + * Author: Jeff Garland + */ + + +#include +#include + +//! Really simple test framework for counting and printing +class TestStats +{ +public: + static TestStats& instance() {static TestStats ts; return ts;} + void addPassingTest() {testcount_++; passcount_++;} + void addFailingTest() {testcount_++;} + unsigned int testcount() const {return testcount_;} + unsigned int passcount() const {return passcount_;} + void print(std::ostream& out = std::cout) const + { + out << testcount_ << " Tests Executed: " ; + if (passcount() != testcount()) { + out << (testcount() - passcount()) << " FAILURES"; + } + else { + out << "All Succeeded" << std::endl; + } + out << std::endl; + } +private: + TestStats() : testcount_(0), passcount_(0) {} + unsigned int testcount_; + unsigned int passcount_; +}; + + +bool check(const std::string& testname, bool testcond) +{ + TestStats& stat = TestStats::instance(); + if (testcond) { + std::cout << "Pass :: " << testname << " " << std::endl; + stat.addPassingTest(); + return true; + } + else { + stat.addFailingTest(); + std::cout << "FAIL :: " << testname << " " << std::endl; + return false; + } +}; + + +int printTestStats() +{ + TestStats& stat = TestStats::instance(); + stat.print(); + return stat.testcount() - stat.passcount(); +}; + +#endif diff --git a/include/boost/gdtl/time.hpp b/include/boost/gdtl/time.hpp new file mode 100644 index 0000000..cbbebc9 --- /dev/null +++ b/include/boost/gdtl/time.hpp @@ -0,0 +1,156 @@ + +#ifndef GDTL_TIME_HPP___ +#define GDTL_TIME_HPP___ +/* Copyright (c) 2000, 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +/*! @file time.hpp + This file contains the interface for the time associated classes. +*/ +#include "boost/gdtl/time_defs.hpp" +#include "boost/operators.hpp" +#include + +namespace boost { +namespace gdtl { + + //! Representation of a precise moment in time, including the date. + /*! + This class is a skeleton for the interface of a temporal type + with a resolution is higher than a day. It is intended that + this class be the base class and that the actual time + class be derived using the BN pattern. In this way, the derived + class can make decisions such as 'should there be a default constructor' + and what should it set its value to, should there be optional constructors + say allowing only an time_durations that generate a time from a clock,etc. + So, in fact multiple time types can be created for a time_system with + different construction policies, and all of them can perform basic + operations by only writing a copy constructor. Finally, compiler + errors are also shorter. + + The real behavior of the time class is provided by the time_system + template parameter. This class must provide all the logic + for addition, subtraction, as well as define all the interface + types. + + */ + + template + class gdtl_time + : boost::less_than_comparable > + { + public: + typedef T time_type; + typedef typename time_system::time_rep_type time_rep_type; + typedef typename time_system::date_type date_type; + typedef typename time_system::date_duration_type date_duration_type; + typedef typename time_system::time_duration_type time_duration_type; + //typedef typename time_system::hms_type hms_type; + + gdtl_time(const date_type& day, + const time_duration_type& td, + dst_flags dst=not_dst) : + time_(time_system::get_time_rep(day, td, dst)) + {} + gdtl_time(const time_rep_type& rhs) : + time_(rhs) + {} + date_type date() const + { + return time_system::get_date(time_); + } + time_duration_type time_of_day() const + { + return time_system::get_time_of_day(time_); + } + std::string zone_name() const + { + return time_system::zone_name(time_); + } + //TODO add these special functions +// //! check to see if date is not a value +// bool is_not_a_date() const +// { +// return traits_type::is_not_a_number(days_); +// } +// //! check to see if date is one of the infinity values +// bool is_infinity() const +// { +// return traits_type::is_infinity(days_); +// } + +// //! check to see if date is greater than all possible dates +// bool is_pos_infinity() const +// { +// return traits_type::is_pos_infinity(days_); +// } +// //! check to see if date is greater than all possible dates +// bool is_neg_infinity() const +// { +// return traits_type::is_neg_infinity(days_); +// } + + //!Equality operator -- others generated by boost::equality_comparable + bool operator==(const time_type& rhs) const + { + return time_system::is_equal(time_,rhs.time_); + } + //!Equality operator -- others generated by boost::less_than_comparable + bool operator<(const time_type& rhs) const + { + return time_system::is_less(time_,rhs.time_); + } + //! add date durations + time_type operator+(const date_duration_type& dd) const + { + return time_system::add_days(time_, dd); + } + //! subtract date durations + time_type operator-(const date_duration_type& dd) const + { + return time_system::subtract_days(time_, dd); + } + //! subtract time durations + time_type operator-(const time_duration_type& rhs) const + { + return time_system::subtract_time_duration(time_, rhs); + } + //! difference between to times + time_duration_type operator-(const time_type& rhs) const + { + return time_system::subtract_times(time_, rhs.time_); + } + //! add time durations + time_type operator+(const time_duration_type& td) const + { + return time_type(time_system::add_time_duration(time_, td)); + } + + protected: + time_rep_type time_; + }; + + + + + +} } //namespace gdtl::boost + +/* Copyright (c) 2000,2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + */ + +#endif diff --git a/include/boost/gdtl/time_clock.hpp b/include/boost/gdtl/time_clock.hpp new file mode 100644 index 0000000..a7af863 --- /dev/null +++ b/include/boost/gdtl/time_clock.hpp @@ -0,0 +1,76 @@ + +#ifndef GDTL_TIME_CLOCK_HPP___ +#define GDTL_TIME_CLOCK_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +/*! @file time_clock.hpp + This file contains the interface for clock devices. +*/ + +#include "boost/gdtl/c_time.hpp" + +namespace boost { +namespace gdtl { + + + //! A clock providing time level services based on C time_t capabilities + /*! This clock provides resolution to the 1 second level + */ + template + class second_clock + { + public: + // typedef typename time_type::date_type date_type; + typedef typename time_type::time_duration_type time_duration_type; + + static time_type local_time() + { + ::std::time_t t; + ::std::time(&t); + ::std::tm* curr = ::std::localtime(&t); + return create_time(curr); + } + + //! Get the current day in universal date as a ymd_type + static time_type universal_time() + { + + ::std::time_t t; + ::std::time(&t); + ::std::tm* curr= ::std::gmtime(&t); + return create_time(curr); + } + + private: + static time_type create_time(::std::tm* current) + { + date_type d(current->tm_year + 1900, + current->tm_mon + 1, + current->tm_mday); + time_duration_type td(current->tm_hour, + current->tm_min, + current->tm_sec); + return time_type(d,td); + } + + }; + + +} } //namespace gdtl + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + +#endif diff --git a/include/boost/gdtl/time_defs.hpp b/include/boost/gdtl/time_defs.hpp new file mode 100644 index 0000000..635f00f --- /dev/null +++ b/include/boost/gdtl/time_defs.hpp @@ -0,0 +1,49 @@ + +#ifndef GDTL_TIME_PRECISION_LIMITS_HPP +#define GDTL_TIME_PRECISION_LIMITS_HPP + +/* Copyright (c) 2000, 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + + +/*! \file time_defs.hpp + This file contains nice definitions for handling the resoluion of various time + reprsentations. +*/ + +namespace boost { +namespace gdtl { + + //!Defines some nice types for handling time level resolutions + enum time_resolutions {sec, tenth, hundreth, milli, micro, nano, NumResolutions }; + + //! Flags for daylight savings or summer time + enum dst_flags {not_dst, is_dst, calculate}; + + + //Todo move resolution strings + //!A resolution adjustment table which corresponds to the name table + // static const unsigned long resolution_adjust[NumResolutions] = {1,10,100,1000,1000000,1000000000 }; + //!Some strings for the various resolutions + //const char* const resolution_names[NumResolutions] = {"Second", "Tenth", "Hundreth", "Milli", "Micro", "Nano"}; + + +} } //namespace gdtl + +/* Copyright (c) 2000,2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + */ + + +#endif diff --git a/include/boost/gdtl/time_duration.hpp b/include/boost/gdtl/time_duration.hpp new file mode 100644 index 0000000..8fffc19 --- /dev/null +++ b/include/boost/gdtl/time_duration.hpp @@ -0,0 +1,151 @@ +#ifndef GDTL_TIME_DURATION_HPP___ +#define GDTL_TIME_DURATION_HPP___ +/* Copyright (c) 2000 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/operators.hpp" +#include "boost/gdtl/time_defs.hpp" + +//Work around compilers that don't have std::abs +#if (__GNUC__ <= 3)|| (defined(BOOST_MSVC) && _MSC_VER <= 1200) +# define BOOST_NO_LONG_LONG_ABS +#endif + +#if defined(BOOST_NO_LONG_LONG_ABS) +namespace std +{ + template // JDG [7/6/02 made a template] + inline T abs(T x) + { + return x < 0 ? -x : x; + } +} +#endif + + +namespace boost { +namespace gdtl { + + //! Represents some amount of elapsed time measure to a given resolution + /*! + + */ + template + class time_duration + : boost::less_than_comparable > + { + public: + typedef T duration_type; //the subclass + typedef typename rep_type::day_type day_type; + typedef typename rep_type::hour_type hour_type; + typedef typename rep_type::min_type min_type; + typedef typename rep_type::sec_type sec_type; + typedef typename rep_type::fractional_seconds_type fractional_seconds_type; + typedef typename rep_type::tick_type tick_type; + + time_duration() : ticks_(0) {} + time_duration(hour_type hours, + min_type minutes, + sec_type seconds=0, + fractional_seconds_type frac_sec = 0) : + ticks_(rep_type::to_tick_count(hours,minutes,seconds,frac_sec)) + {} + + //! Returns smallest representable duration + static duration_type unit() + { + return duration_type(0,0,0,1); + } + static time_resolutions resolution() + { + return rep_type::resolution(); + } + //! Returns number of hours in the duration + hour_type hours() const + { + return (ticks_ / (3600*rep_type::res_adjust())); + } + //! Returns normalized number of minutes + min_type minutes() const + { + return std::abs(((ticks() / (60*rep_type::res_adjust())) % 60)); + } + //! Returns normalized number of seconds + sec_type seconds() const + { + return std::abs((ticks()/rep_type::res_adjust()) % 60); + } + //! Returns count of fractional seconds at given resolution + fractional_seconds_type fractional_seconds() const + { + return std::abs((ticks()%rep_type::res_adjust())); + } + //! Returns number of possible digits in fractional seconds + static unsigned short num_fractional_digits() + { + return rep_type::num_fractional_digits(); + } + duration_type invert_sign() const + { + return duration_type(-ticks_); + } + bool is_negative() const + { + return ticks_ < 0; + } + bool operator<(const time_duration& rhs) const + { + return ticks_ < rhs.ticks_; + } + bool operator==(const time_duration& rhs) const + { + return ticks_ == rhs.ticks_; + } + duration_type operator-(const duration_type& d) const + { + return duration_type(ticks_ - d.ticks_); + } + duration_type operator+(const duration_type& d) const + { + return duration_type(ticks_ + d.ticks_); + } + tick_type ticks() const { return ticks_;}; + + protected: + explicit time_duration(tick_type in) : ticks_(in) {}; + tick_type ticks_; + }; + + //! Template for instantiating derived adjusting durations + template + class subsecond_duration : public base_duration + { + public: + explicit subsecond_duration(long ss) : + base_duration(0,0,0,ss*adjustment) + {} + }; + + + +} } //namespace gdtl + + +/* Copyright (c) 2000 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + + +#endif diff --git a/include/boost/gdtl/time_iterator.hpp b/include/boost/gdtl/time_iterator.hpp new file mode 100644 index 0000000..d05e703 --- /dev/null +++ b/include/boost/gdtl/time_iterator.hpp @@ -0,0 +1,54 @@ +#ifndef GDTL_TIME_ITERATOR_HPP___ +#define GDTL_TIME_ITERATOR_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + + +namespace boost { +namespace gdtl { + + + //! Simple time iterator skeleton class + template + class time_itr { + public: + typedef typename time_type::time_duration_type time_duration_type; + time_itr(time_type t, time_duration_type d) : current_(t), offset_(d) {}; + time_itr& operator++() + { + current_ = current_ + offset_; + return *this; + }; + time_type operator*() {return current_;}; + time_type* operator->() {return ¤t_;}; + bool operator< (const time_type& t) {return current_ < t;}; + bool operator<= (const time_type& t) {return current_ <= t;}; + bool operator!= (const time_type& t) {return current_ != t;}; + bool operator== (const time_type& t) {return current_ == t;}; + bool operator> (const time_type& t) {return current_ > t;}; + bool operator>= (const time_type& t) {return current_ >= t;}; + + private: + time_type current_; + time_duration_type offset_; + }; + + + +} }//namespace gdtl + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + +#endif diff --git a/include/boost/gdtl/time_parsing.hpp b/include/boost/gdtl/time_parsing.hpp new file mode 100644 index 0000000..1b2f5af --- /dev/null +++ b/include/boost/gdtl/time_parsing.hpp @@ -0,0 +1,109 @@ + +#ifndef _GDTL_TIME_PARSING_HPP___ +#define _GDTL_TIME_PARSING_HPP___ +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include "boost/tokenizer.hpp" +#include "boost/lexical_cast.hpp" +#include "boost/gdtl/date_parsing.hpp" +#include "boost/cstdint.hpp" + + +namespace boost { +namespace gdtl { + + + template + inline + time_duration + parse_delimited_time_duration(const std::string& s) + { + unsigned short hour=0, min=0, sec =0; + boost::int64_t fs=0; + int pos = 0; + boost::tokenizer > tok(s); + for(boost::tokenizer<>::iterator beg=tok.begin(); beg!=tok.end();++beg){ + switch(pos) { + case 0: { + hour = boost::lexical_cast(*beg); + break; + } + case 1: { + min = boost::lexical_cast(*beg); + break; + } + case 2: { + sec = boost::lexical_cast(*beg); + break; + }; + case 3: { + //Works around a bug in MSVC 6 library that does not support + //operator>> thus meaning lexical_cast will fail to compile. +#if (defined(BOOST_MSVC) && (_MSC_VER <= 1200)) // 1200 == VC++ 6.0 + fs = _atoi64(beg->c_str()); +#else + fs = boost::lexical_cast(*beg); +#endif + break; + } + }//switch + pos++; + } + return time_duration(hour, min, sec, fs); + } + + //TODO this could use some error checking! + inline + bool + split(const std::string& s, + char sep, + std::string& first, + std::string& second) + { + int sep_pos = s.find(sep); + first = s.substr(0,sep_pos); + second = s.substr(sep_pos+1); + return true; + } + + template + inline + time_type + parse_delimited_time(const std::string& s, char sep) + { + typedef typename time_type::time_duration_type time_duration; + typedef typename time_type::date_type date_type; + + //split date/time on a unique delimiter char such as ' ' or 'T' + std::string date_string, tod_string; + split(s, sep, date_string, tod_string); + //call parse_date with first string + date_type d = parse_date(date_string); + //call parse_time_duration with remaining string + time_duration td = parse_delimited_time_duration(tod_string); + //construct a time + return time_type(d, td); + + } + + +} }//namespace gdtl + + + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + +#endif diff --git a/include/boost/gdtl/time_resolution_traits.hpp b/include/boost/gdtl/time_resolution_traits.hpp new file mode 100644 index 0000000..b5af98c --- /dev/null +++ b/include/boost/gdtl/time_resolution_traits.hpp @@ -0,0 +1,76 @@ + +#ifndef GDTL_TIME_RESOLUTION_TRAITS_HPP +#define GDTL_TIME_RESOLUTION_TRAITS_HPP + +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + + +#include "boost/gdtl/time_defs.hpp" +#include "boost/cstdint.hpp" + +namespace boost { +namespace gdtl { + + template + class time_resolution_traits { + public: + typedef frac_sec_type fractional_seconds_type; + typedef frac_sec_type tick_type; + typedef v_type day_type; + typedef v_type hour_type; + typedef v_type min_type; + typedef v_type sec_type; + + static const fractional_seconds_type ticks_per_second = resolution_adjust; + + static time_resolutions resolution() + { + return res; + } + static unsigned short num_fractional_digits() + { + return frac_digits; + } + static fractional_seconds_type res_adjust() + { + return resolution_adjust; + } + static tick_type to_tick_count(hour_type hours, + min_type minutes, + sec_type seconds, + fractional_seconds_type fs) + { + return (((hours*3600) + (minutes*60) + seconds)*res_adjust()) + fs; + } + + }; + + typedef time_resolution_traits milli_res; + typedef time_resolution_traits micro_res; + typedef time_resolution_traits nano_res; + + +} } //namespace gdtl + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + */ + + +#endif diff --git a/include/boost/gdtl/time_system_counted.hpp b/include/boost/gdtl/time_system_counted.hpp new file mode 100644 index 0000000..7a966e9 --- /dev/null +++ b/include/boost/gdtl/time_system_counted.hpp @@ -0,0 +1,149 @@ + +#ifndef GDTL_TIME_SYSTEM_COUNTED_HPP +#define GDTL_TIME_SYSTEM_COUNTED_HPP + +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + + +#include "boost/gdtl/time_defs.hpp" +#include + + +namespace boost { +namespace gdtl { + + //! Time representation that uses a single integer count + template + struct counted_time_rep + { + typedef typename config::int_type int_type; + typedef typename config::date_type date_type; + typedef typename date_type::duration_type date_duration_type; + typedef typename date_type::calendar_type calendar_type; + typedef typename date_type::ymd_type ymd_type; + typedef typename config::time_duration_type time_duration_type; + typedef typename config::resolution_traits resolution_traits; + + counted_time_rep(const date_type& d, const time_duration_type& tod) + { + time_count_ = (d.day_number() * frac_sec_per_day()) + tod.ticks(); + } + explicit counted_time_rep(int_type count) : + time_count_(count) + {} + date_type date() const + { + typename calendar_type::date_int_type dc = day_count(); + //std::cout << "time_rep here:" << dc << std::endl; + ymd_type ymd = calendar_type::from_day_number(dc); + return date_type(ymd); + } + int_type day_count() const + { + return time_count_ / frac_sec_per_day(); + } + int_type time_count() const + { + return time_count_; + } + int_type tod() const + { + return time_count_ % frac_sec_per_day(); + } + static int_type frac_sec_per_day() + { + int_type seconds_per_day = 60*60*24; + int_type fractional_sec_per_sec(resolution_traits::ticks_per_second); + return seconds_per_day*fractional_sec_per_sec; + } + private: + int_type time_count_; + }; + + //! An unadjusted time system implementation. + template + class counted_time_system + { + public: + typedef time_rep time_rep_type; + typedef typename time_rep_type::time_duration_type time_duration_type; + typedef typename time_duration_type::fractional_seconds_type fractional_seconds_type; + typedef typename time_rep_type::date_type date_type; + typedef typename time_rep_type::date_duration_type date_duration_type; + + static time_rep_type get_time_rep(const date_type& day, + const time_duration_type& tod, + gdtl::dst_flags) + { + return time_rep_type(day, tod); + } + static date_type get_date(const time_rep_type& val) + { + return val.date(); + } + static time_duration_type get_time_of_day(const time_rep_type& val) + { + return time_duration_type(0,0,0,val.tod()); + } + static std::string zone_name(const time_rep_type&) + { + return ""; + } + static bool is_equal(const time_rep_type& lhs, const time_rep_type& rhs) + { + return (lhs.time_count() == rhs.time_count()); + } + static bool is_less(const time_rep_type& lhs, const time_rep_type& rhs) + { + return (lhs.time_count() < rhs.time_count()); + } + static time_rep_type add_days(const time_rep_type& base, + const date_duration_type& dd) + { + return time_rep_type(base.time_count() + (dd.days() * time_rep_type::frac_sec_per_day())); + } + static time_rep_type subtract_days(const time_rep_type& base, + const date_duration_type& dd) + { + return time_rep_type(base.time_count() - (dd.days() * time_rep_type::frac_sec_per_day())); + } + static time_rep_type subtract_time_duration(const time_rep_type& base, + const time_duration_type& td) + { + return time_rep_type(base.time_count() - td.ticks()); + } + static time_rep_type add_time_duration(const time_rep_type& base, + time_duration_type td) + { + return time_rep_type(base.time_count() + td.ticks()); + } + static time_duration_type subtract_times(const time_rep_type& lhs, + const time_rep_type& rhs) + { + fractional_seconds_type fs = lhs.time_count() - rhs.time_count(); + return time_duration_type(0,0,0,fs); + } + + }; + + +} } //namespace gdtl + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + */ + + +#endif diff --git a/include/boost/gdtl/time_system_split.hpp b/include/boost/gdtl/time_system_split.hpp new file mode 100644 index 0000000..a567fb1 --- /dev/null +++ b/include/boost/gdtl/time_system_split.hpp @@ -0,0 +1,124 @@ + +#ifndef GDTL_TIME_SYSTEM_SPLIT_HPP +#define GDTL_TIME_SYSTEM_SPLIT_HPP + +/* Copyright (c) 2002 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +#include + +namespace boost { +namespace gdtl { + + //! An unadjusted time system implementation. + template + class split_timedate_system + { + public: + typedef typename config::time_rep_type time_rep_type; + typedef typename config::date_type date_type; + typedef typename config::time_duration_type time_duration_type; + typedef typename config::date_duration_type date_duration_type; + typedef typename config::int_type int_type; + typedef typename config::resolution_traits resolution_traits; + + //86400 is number of seconds in a day... + typedef gdtl::wrapping_int wrap_int_type; + + static time_rep_type get_time_rep(const date_type& day, + const time_duration_type& tod, + gdtl::dst_flags) + { + return time_rep_type(day, tod); + } + static date_type get_date(const time_rep_type& val) + { + return date_type(val.day); + } + static time_duration_type get_time_of_day(const time_rep_type& val) + { + return time_duration_type(val.time_of_day); + } + static std::string zone_name(const time_rep_type&) + { + return ""; + } + static bool is_equal(const time_rep_type& lhs, const time_rep_type& rhs) + { + return ((lhs.day == rhs.day) && (lhs.time_of_day == rhs.time_of_day)); + // return true; + } + static bool is_less(const time_rep_type& lhs, const time_rep_type& rhs) + { + if (lhs.day < rhs.day) return true; + if (lhs.day > rhs.day) return false; + return (lhs.time_of_day < rhs.time_of_day); + } + static time_rep_type add_days(const time_rep_type& base, + const date_duration_type& dd) + { + return time_rep_type(base.day+dd, base.time_of_day); + } + static time_rep_type subtract_days(const time_rep_type& base, + const date_duration_type& dd) + { + return time_rep_type(base.day-dd, base.time_of_day); + } + static time_rep_type subtract_time_duration(const time_rep_type& base, + const time_duration_type& td) + { + wrap_int_type day_offset(base.time_of_day.ticks()); + date_duration_type day_overflow(day_offset.subtract(td.ticks())); +// std::cout << "sub: " << base.time_of_day.ticks() << "|" +// << day_offset.as_int() << "|" +// << day_overflow.days() << std::endl; + return time_rep_type(base.day-day_overflow, + time_duration_type(0,0,0,day_offset.as_int())); + } + static time_rep_type add_time_duration(const time_rep_type& base, + time_duration_type td) + { + if (td.is_negative()) { + time_duration_type td1 = td.invert_sign(); + return subtract_time_duration(base,td1); + } + wrap_int_type day_offset(base.time_of_day.ticks()); + int_type doff = day_offset.add(td.ticks()); +// std::cout << "day overflow: " << doff << std::endl; +// std::cout << "ticks: " << td.ticks() << std::endl; + date_duration_type day_overflow(doff); +// std::cout << "base: " << to_simple_string(base.day) << std::endl; +// std::cout << "overflow " << day_overflow.days() << std::endl; + return time_rep_type(base.day+day_overflow, + time_duration_type(0,0,0,day_offset.as_int())); + } + static time_duration_type subtract_times(const time_rep_type& lhs, + const time_rep_type& rhs) + { + date_duration_type dd = lhs.day - rhs.day; + time_duration_type td(dd.days()*24,0,0); //days * 24 hours + time_duration_type td2 = lhs.time_of_day - rhs.time_of_day; + return td+td2; + // return time_rep_type(base.day-dd, base.time_of_day); + } + + }; + +} } //namespace gdtl + +/* Copyright (c) 2002 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + */ + +#endif diff --git a/include/boost/gdtl/wrapping_int.hpp b/include/boost/gdtl/wrapping_int.hpp new file mode 100644 index 0000000..18b65cc --- /dev/null +++ b/include/boost/gdtl/wrapping_int.hpp @@ -0,0 +1,137 @@ +#ifndef _GDTL_WRAPPING_INT_HPP__ +#define _GDTL_WRAPPING_INT_HPP__ +/* Copyright (c) 2001 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + + +namespace boost { +namespace gdtl { + +//! A wrapping integer used to support time durations +/*! In composite date and time types this type is used to + * wrap at the day boundary. + * + */ +template +class wrapping_int { +public: + typedef int_type_ int_type; + //typedef overflow_type_ overflow_type; + static int_type wrap_value() {return wrap_val;} + //!Add, return true if wrapped + wrapping_int(int_type v) : value_(v) {}; + //! Explicit converion method + int_type as_int() const {return value_;} + operator int_type() const {return value_;} + int_type add(int_type v) + { + //take the mod here and assign it.... + int_type remainder = v % wrap_val; + int_type overflow = v / wrap_val; + value_ += remainder; + if ((value_) >= wrap_val) { + value_ -= wrap_val; + overflow++; + } + return overflow; + } + int_type subtract(int_type v) + { + //take the mod here and assign it.... + int_type remainder = v % wrap_val; + int_type underflow = v / wrap_val; + +// std::cout << "wi :" << value_ << "|" +// << v << "|" +// << underflow << "|" +// << remainder << "|" +// << wrap_val << std::endl; + if (remainder > value_) { + underflow++; + // value_ = remainder - value_; + value_ = wrap_val - (remainder-value_); + } + else { + value_ -= remainder; + //value_ = wrap_val-(remainder-value_); + // value_ = wrap_val -(value_-remainder); + } +// std::cout << "wi final uf: " << underflow +// << " value: " << value_ << std::endl; + return underflow; + } + +private: + int_type value_; + +}; + + +//! A wrapping integer used to wrap around at the top +/*! Bad name, quick impl to fix a bug -- fix later!! + * This allows the wrap to restart at a value other than 0. + * Currently this only works if wrap_min == 1 + */ +template +class wrapping_int2 { +public: + typedef int_type_ int_type; + static unsigned long wrap_value() {return wrap_max;} + static unsigned long min_value() {return wrap_min;} + //!Add, return true if wrapped + wrapping_int2(int_type v) : value_(v) {}; + //! Explicit converion method + int_type as_int() const {return value_;} + operator int_type() const {return value_;} + int_type add(int_type v) + { + //take the mod here and assign it.... + int_type remainder = v % wrap_max; + int_type overflow = v / wrap_max; +// std::cout << "wi2: " << value_ << "|" +// << v << "|" +// << remainder << "|" +// << wrap_max << "|" +// << wrap_min << std::endl; + value_ += remainder; + if ((value_) > wrap_max) { + overflow++; + value_ -= (wrap_max - wrap_min + 1); + } +// std::cout << "wi2 final: " +// << overflow << "|" +// << value_ << std::endl; + return overflow; + } + +private: + int_type value_; + +}; + + + +} } //namespace gdtl + +/* + * Copyright (c) 2001 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided as is without express or implied warranty. + * + * + * Author: Jeff Garland (jeff@CrystalClearSoftware.com) + * Created: Sat Sep 8 19:37:11 2001 + * + */ + + +#endif diff --git a/include/boost/gdtl/year_month_day.hpp b/include/boost/gdtl/year_month_day.hpp new file mode 100644 index 0000000..23b3d81 --- /dev/null +++ b/include/boost/gdtl/year_month_day.hpp @@ -0,0 +1,52 @@ +#ifndef YearMonthDayBase_HPP__ +#define YearMonthDayBase_HPP__ +/* Copyright (c) 2001 CrystalClear Software, Inc. + * Disclaimer & Full Copyright at end of file + * Author: Jeff Garland + */ + +namespace boost { +namespace gdtl { + +//! Allow rapid creation of ymd triples of different types +template +struct year_month_day_base { + year_month_day_base(YearType year, + MonthType month, + DayType day); + YearType year; + MonthType month; + DayType day; + typedef YearType year_type; + typedef MonthType month_type; + typedef DayType day_type; +}; + + +//! A basic constructor +template +inline +year_month_day_base::year_month_day_base(YearType y, + MonthType m, + DayType d) : + year(y), + month(m), + day(d) +{} + +} }//namespace gdtl + +/* Copyright (c) 2001 + * CrystalClear Software, Inc. + * + * Permission to use, copy, modify, distribute and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear + * in supporting documentation. CrystalClear Software makes no + * representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + */ + +#endif +