static time_rep_type subtract_time_duration(const time_rep_type& base, const time_duration_type& td) { if(base.is_special() || td.is_special()) { return(time_rep_type(base.get_rep() - td.get_rep())); } else { 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) { if(base.is_special() || td.is_special()) { return(time_rep_type(base.get_rep() + td.get_rep())); } else { return time_rep_type(base.time_count() + td.ticks()); } }
counted_time_rep(const date_type& d, const time_duration_type& time_of_day) : time_count_(1) { if(d.is_infinity() || d.is_not_a_date() || time_of_day.is_special()) { time_count_ = time_of_day.get_rep() + d.day_count(); //std::cout << time_count_ << std::endl; } else { time_count_ = (d.day_number() * frac_sec_per_day()) + time_of_day.ticks(); } }
//! Put time into an ostream static void duration_put(const time_duration_type& td, ostream_type& os) { os << std::setw(2) << std::setfill('0') << td.hours() << ":"; os << std::setw(2) << std::setfill('0') << td.minutes() << ":"; os << std::setw(2) << std::setfill('0') << td.seconds(); fractional_seconds_type frac_sec = td.fractional_seconds(); if (frac_sec != 0) { os << "." << std::setw(time_duration_type::num_fractional_digits()) << std::setfill('0') << frac_sec; } } // duration_put
static string_type fractional_seconds_as_string(const time_duration_type& a_time, bool null_when_zero) { typename time_duration_type::fractional_seconds_type frac_sec = a_time.fractional_seconds(); if (null_when_zero && (frac_sec == 0)) { return string_type(); } std::basic_ostringstream<char_type> ss; ss.imbue(std::locale::classic()); // don't want any formatting ss << std::setw(time_duration_type::num_fractional_digits()) << std::setfill(static_cast<char_type>('0')); #if (defined(BOOST_MSVC) && (_MSC_VER <= 1200)) // 1200 == VC++ 6.0 // JDG [7/6/02 VC++ compatibility] char_type buff[34]; ss << _i64toa(static_cast<boost::int64_t>(frac_sec), buff, 10); #else ss << frac_sec; #endif return ss.str(); }
static time_rep_type add_time_duration(const time_rep_type& base, time_duration_type td) { if(base.day.is_special() || td.is_special()) { return split_timedate_system::get_time_rep(base.day, 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()); date_duration_type day_overflow(static_cast< typename date_duration_type::duration_rep_type >(day_offset.add(td.ticks()))); return time_rep_type(base.day+day_overflow, time_duration_type(0,0,0,day_offset.as_int())); }
//! Put time into an ostream static void duration_put(const time_duration_type& td, ostream_type& os) { if(td.is_special()) { os << td.get_rep(); } else { charT fill_char = '0'; if(td.is_negative()) { os << '-'; } os << std::setw(2) << std::setfill(fill_char) << absolute_value(td.hours()) << ":"; os << std::setw(2) << std::setfill(fill_char) << absolute_value(td.minutes()) << ":"; os << std::setw(2) << std::setfill(fill_char) << absolute_value(td.seconds()); fractional_seconds_type frac_sec = absolute_value(td.fractional_seconds()); if (frac_sec != 0) { os << "." << std::setw(time_duration_type::num_fractional_digits()) << std::setfill(fill_char) << frac_sec; } } // else } // duration_put
static time_rep_type add_time_duration(const time_rep_type& base, time_duration_type td) { if(base.day.is_special() || td.is_special()) { return split_timedate_system::get_time_rep(base.day, 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(static_cast<int_type>(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_rep_type subtract_time_duration(const time_rep_type& base, const time_duration_type& td) { if(base.day.is_special() || td.is_special()) { return split_timedate_system::get_time_rep(base.day, -td); } if (td.is_negative()) { time_duration_type td1 = td.invert_sign(); return add_time_duration(base,td1); } //std::cout << td.ticks() << std::endl; wrap_int_type day_offset(base.time_of_day.ticks()); date_duration_type day_overflow(static_cast<int_type>(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 get_time_rep(const date_type& day, const time_duration_type& tod, date_time::dst_flags /* dst */ = not_dst) { if(day.is_special() || tod.is_special()) { if(day.is_not_a_date() || tod.is_not_a_date_time()) { return time_rep_type(date_type(not_a_date_time), time_duration_type(not_a_date_time)); } else if(day.is_pos_infinity()) { if(tod.is_neg_infinity()) { return time_rep_type(date_type(not_a_date_time), time_duration_type(not_a_date_time)); } else { return time_rep_type(day, time_duration_type(pos_infin)); } } else if(day.is_neg_infinity()) { if(tod.is_pos_infinity()) { return time_rep_type(date_type(not_a_date_time), time_duration_type(not_a_date_time)); } else { return time_rep_type(day, time_duration_type(neg_infin)); } } else if(tod.is_pos_infinity()) { if(day.is_neg_infinity()) { return time_rep_type(date_type(not_a_date_time), time_duration_type(not_a_date_time)); } else { return time_rep_type(date_type(pos_infin), tod); } } else if(tod.is_neg_infinity()) { if(day.is_pos_infinity()) { return time_rep_type(date_type(not_a_date_time), time_duration_type(not_a_date_time)); } else { return time_rep_type(date_type(neg_infin), tod); } } } return time_rep_type(day, tod); }
/*! 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 Time offset within day for dst boundary * @param dst_end_day Ending day of dst for the given locality * @param dst_end_offset Time offset within day given in dst for dst boundary * @param dst_length lenght of dst adjusment * @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, const time_duration_type& dst_start_offset, const date_type& dst_end_day, const time_duration_type& dst_end_offset, const time_duration_type& dst_length_minutes) { unsigned int start_minutes = dst_start_offset.hours() * 60 + dst_start_offset.minutes(); unsigned int end_minutes = dst_end_offset.hours() * 60 + dst_end_offset.minutes(); long length_minutes = dst_length_minutes.hours() * 60 + dst_length_minutes.minutes(); return local_is_dst(current_day, time_of_day, dst_start_day, start_minutes, dst_end_day, end_minutes, length_minutes); }
bool is_not_a_date_time()const { return(day.is_not_a_date() || time_of_day.is_not_a_date_time()); }
bool is_neg_infinity()const { return(day.is_neg_infinity() || time_of_day.is_neg_infinity()); }
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) { boost::throw_exception(std::out_of_range("Cannot convert dates prior to Jan 1, 1970")); BOOST_DATE_TIME_UNREACHABLE_EXPRESSION(return time_t_start_time); // should never reach } date_duration_type dd = t.date() - time_t_start_day; time_duration_type td = t.time_of_day(); uint64_t t2 = static_cast<uint64_t>(dd.days())*86400 + static_cast<uint64_t>(td.hours())*3600 + static_cast<uint64_t>(td.minutes())*60 + td.seconds(); // detect y2038 issue and throw instead of proceed with bad time std::time_t tv = boost::numeric_cast<std::time_t>(t2); std::tm tms, *tms_ptr; tms_ptr = c_time::localtime(&tv, &tms); date_type d(static_cast<unsigned short>(tms_ptr->tm_year + 1900), static_cast<unsigned short>(tms_ptr->tm_mon + 1), static_cast<unsigned short>(tms_ptr->tm_mday)); time_duration_type td2(tms_ptr->tm_hour, tms_ptr->tm_min, tms_ptr->tm_sec, t.time_of_day().fractional_seconds());
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) { boost::throw_exception(std::out_of_range("Cannot convert dates prior to Jan 1, 1970")); BOOST_DATE_TIME_UNREACHABLE_EXPRESSION(return time_t_start_time); // should never reach } 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, *tms_ptr; tms_ptr = c_time::localtime(&t2, &tms); date_type d(static_cast<unsigned short>(tms_ptr->tm_year + 1900), static_cast<unsigned short>(tms_ptr->tm_mon + 1), static_cast<unsigned short>(tms_ptr->tm_mday)); time_duration_type td2(tms_ptr->tm_hour, tms_ptr->tm_min, tms_ptr->tm_sec, t.time_of_day().fractional_seconds()); return time_type(d,td2); } };
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) { boost::throw_exception(std::out_of_range("Cannot convert dates prior to Jan 1, 1970")); BOOST_DATE_TIME_UNREACHABLE_EXPRESSION(return time_t_start_time); // should never reach } date_duration_type dd = t.date() - time_t_start_day; time_duration_type td = t.time_of_day(); std::time_t t2 = static_cast<std::time_t>(dd.days())*86400 + static_cast<std::time_t>(td.hours())*3600 + static_cast<std::time_t>(td.minutes())*60 + td.seconds(); std::tm tms, *tms_ptr; tms_ptr = c_time::localtime(&t2, &tms); date_type d(static_cast<unsigned short>(tms_ptr->tm_year + 1900), static_cast<unsigned short>(tms_ptr->tm_mon + 1), static_cast<unsigned short>(tms_ptr->tm_mday)); time_duration_type td2(tms_ptr->tm_hour, tms_ptr->tm_min, tms_ptr->tm_sec, t.time_of_day().fractional_seconds()); return time_type(d,td2);