| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250 |
- // (C) Copyright Howard Hinnant
- // (C) Copyright 2010-2011 Vicente J. Botet Escriba
- // Use, modification and distribution are subject to the Boost Software License,
- // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
- // http://www.boost.org/LICENSE_1_0.txt).
- //===-------------------------- locale ------------------------------------===//
- //
- // The LLVM Compiler Infrastructure
- //
- // This file is dual licensed under the MIT and the University of Illinois Open
- // Source Licenses. See LICENSE.TXT for details.
- //
- //===----------------------------------------------------------------------===//
- // This code was adapted by Vicente from Howard Hinnant's experimental work
- // on chrono i/o to Boost and some functions from libc++/locale to emulate the missing time_get::get()
- #ifndef BOOST_CHRONO_IO_TIME_POINT_IO_HPP
- #define BOOST_CHRONO_IO_TIME_POINT_IO_HPP
- #include <boost/chrono/io/time_point_put.hpp>
- #include <boost/chrono/io/time_point_get.hpp>
- #include <boost/chrono/io/duration_io.hpp>
- #include <boost/chrono/io/ios_base_state.hpp>
- #include <boost/chrono/io/utility/manip_base.hpp>
- #include <boost/chrono/time_point.hpp>
- #include <boost/chrono/clock_string.hpp>
- #include <boost/chrono/round.hpp>
- #include <boost/chrono/detail/scan_keyword.hpp>
- #include <boost/static_assert.hpp>
- #include <boost/detail/no_exceptions_support.hpp>
- #include <cstring>
- #include <locale>
- #include <ctime>
- #if ( defined BOOST_WINDOWS && ! defined(__CYGWIN__) ) \
- || (defined(sun) || defined(__sun)) \
- || (defined __IBMCPP__) \
- || defined __ANDROID__ \
- || defined __QNXNTO__ \
- || (defined(_AIX) && defined __GNUC__)
- #define BOOST_CHRONO_INTERNAL_TIMEGM
- #endif
- #if (defined BOOST_WINDOWS && ! defined(__CYGWIN__)) \
- || ( (defined(sun) || defined(__sun)) && defined __GNUC__) \
- || (defined __IBMCPP__) \
- || defined __ANDROID__ \
- || (defined(_AIX) && defined __GNUC__)
- #define BOOST_CHRONO_INTERNAL_GMTIME
- #endif
- #define BOOST_CHRONO_USES_INTERNAL_TIME_GET
- namespace boost
- {
- namespace chrono
- {
- typedef double fractional_seconds;
- namespace detail
- {
- template <class CharT, class InputIterator = std::istreambuf_iterator<CharT> >
- struct time_get
- {
- std::time_get<CharT> const &that_;
- time_get(std::time_get<CharT> const& that) : that_(that) {}
- typedef std::time_get<CharT> facet;
- typedef typename facet::iter_type iter_type;
- typedef typename facet::char_type char_type;
- typedef std::basic_string<char_type> string_type;
- static int
- get_up_to_n_digits(
- InputIterator& b, InputIterator e,
- std::ios_base::iostate& err,
- const std::ctype<CharT>& ct,
- int n)
- {
- // Precondition: n >= 1
- if (b == e)
- {
- err |= std::ios_base::eofbit | std::ios_base::failbit;
- return 0;
- }
- // get first digit
- CharT c = *b;
- if (!ct.is(std::ctype_base::digit, c))
- {
- err |= std::ios_base::failbit;
- return 0;
- }
- int r = ct.narrow(c, 0) - '0';
- for (++b, --n; b != e && n > 0; ++b, --n)
- {
- // get next digit
- c = *b;
- if (!ct.is(std::ctype_base::digit, c))
- return r;
- r = r * 10 + ct.narrow(c, 0) - '0';
- }
- if (b == e)
- err |= std::ios_base::eofbit;
- return r;
- }
- void get_day(
- int& d,
- iter_type& b, iter_type e,
- std::ios_base::iostate& err,
- const std::ctype<char_type>& ct) const
- {
- int t = get_up_to_n_digits(b, e, err, ct, 2);
- if (!(err & std::ios_base::failbit) && 1 <= t && t <= 31)
- d = t;
- else
- err |= std::ios_base::failbit;
- }
- void get_month(
- int& m,
- iter_type& b, iter_type e,
- std::ios_base::iostate& err,
- const std::ctype<char_type>& ct) const
- {
- int t = get_up_to_n_digits(b, e, err, ct, 2);
- if (!(err & std::ios_base::failbit) && 1 <= t && t <= 12)
- m = --t;
- else
- err |= std::ios_base::failbit;
- }
- void get_year4(int& y,
- iter_type& b, iter_type e,
- std::ios_base::iostate& err,
- const std::ctype<char_type>& ct) const
- {
- int t = get_up_to_n_digits(b, e, err, ct, 4);
- if (!(err & std::ios_base::failbit))
- y = t - 1900;
- }
- void
- get_hour(int& h,
- iter_type& b, iter_type e,
- std::ios_base::iostate& err,
- const std::ctype<char_type>& ct) const
- {
- int t = get_up_to_n_digits(b, e, err, ct, 2);
- if (!(err & std::ios_base::failbit) && t <= 23)
- h = t;
- else
- err |= std::ios_base::failbit;
- }
- void
- get_minute(int& m,
- iter_type& b, iter_type e,
- std::ios_base::iostate& err,
- const std::ctype<char_type>& ct) const
- {
- int t = get_up_to_n_digits(b, e, err, ct, 2);
- if (!(err & std::ios_base::failbit) && t <= 59)
- m = t;
- else
- err |= std::ios_base::failbit;
- }
- void get_second(int& s,
- iter_type& b, iter_type e,
- std::ios_base::iostate& err,
- const std::ctype<char_type>& ct) const
- {
- int t = get_up_to_n_digits(b, e, err, ct, 2);
- if (!(err & std::ios_base::failbit) && t <= 60)
- s = t;
- else
- err |= std::ios_base::failbit;
- }
- void get_white_space(iter_type& b, iter_type e,
- std::ios_base::iostate& err,
- const std::ctype<char_type>& ct) const
- {
- for (; b != e && ct.is(std::ctype_base::space, *b); ++b)
- ;
- if (b == e)
- err |= std::ios_base::eofbit;
- }
- void get_12_hour(int& h,
- iter_type& b, iter_type e,
- std::ios_base::iostate& err,
- const std::ctype<char_type>& ct) const
- {
- int t = get_up_to_n_digits(b, e, err, ct, 2);
- if (!(err & std::ios_base::failbit) && 1 <= t && t <= 12)
- h = t;
- else
- err |= std::ios_base::failbit;
- }
- void get_percent(iter_type& b, iter_type e,
- std::ios_base::iostate& err,
- const std::ctype<char_type>& ct) const
- {
- if (b == e)
- {
- err |= std::ios_base::eofbit | std::ios_base::failbit;
- return;
- }
- if (ct.narrow(*b, 0) != '%')
- err |= std::ios_base::failbit;
- else if(++b == e)
- err |= std::ios_base::eofbit;
- }
- void get_day_year_num(int& d,
- iter_type& b, iter_type e,
- std::ios_base::iostate& err,
- const std::ctype<char_type>& ct) const
- {
- int t = get_up_to_n_digits(b, e, err, ct, 3);
- if (!(err & std::ios_base::failbit) && 1 <= t && t <= 366)
- d = --t;
- else
- err |= std::ios_base::failbit;
- }
- void
- get_weekday(int& w,
- iter_type& b, iter_type e,
- std::ios_base::iostate& err,
- const std::ctype<char_type>& ct) const
- {
- int t = get_up_to_n_digits(b, e, err, ct, 1);
- if (!(err & std::ios_base::failbit) && t <= 6)
- w = t;
- else
- err |= std::ios_base::failbit;
- }
- #if 0
- void
- get_am_pm(int& h,
- iter_type& b, iter_type e,
- std::ios_base::iostate& err,
- const std::ctype<char_type>& ct) const
- {
- const string_type* ap = am_pm();
- if (ap[0].size() + ap[1].size() == 0)
- {
- err |= ios_base::failbit;
- return;
- }
- ptrdiff_t i = detail::scan_keyword(b, e, ap, ap+2, ct, err, false) - ap;
- if (i == 0 && h == 12)
- h = 0;
- else if (i == 1 && h < 12)
- h += 12;
- }
- #endif
- InputIterator get(
- iter_type b, iter_type e,
- std::ios_base& iob,
- std::ios_base::iostate& err,
- std::tm* tm,
- char fmt, char) const
- {
- err = std::ios_base::goodbit;
- const std::ctype<char_type>& ct = std::use_facet<std::ctype<char_type> >(iob.getloc());
- switch (fmt)
- {
- case 'a':
- case 'A':
- {
- std::tm tm2;
- std::memset(&tm2, 0, sizeof(std::tm));
- that_.get_weekday(b, e, iob, err, &tm2);
- //tm->tm_wday = tm2.tm_wday;
- }
- break;
- case 'b':
- case 'B':
- case 'h':
- {
- std::tm tm2;
- std::memset(&tm2, 0, sizeof(std::tm));
- that_.get_monthname(b, e, iob, err, &tm2);
- //tm->tm_mon = tm2.tm_mon;
- }
- break;
- // case 'c':
- // {
- // const string_type& fm = c();
- // b = get(b, e, iob, err, tm, fm.data(), fm.data() + fm.size());
- // }
- // break;
- case 'd':
- case 'e':
- get_day(tm->tm_mday, b, e, err, ct);
- break;
- case 'D':
- {
- const char_type fm[] = {'%', 'm', '/', '%', 'd', '/', '%', 'y'};
- b = get(b, e, iob, err, tm, fm, fm + sizeof(fm)/sizeof(fm[0]));
- }
- break;
- case 'F':
- {
- const char_type fm[] = {'%', 'Y', '-', '%', 'm', '-', '%', 'd'};
- b = get(b, e, iob, err, tm, fm, fm + sizeof(fm)/sizeof(fm[0]));
- }
- break;
- case 'H':
- get_hour(tm->tm_hour, b, e, err, ct);
- break;
- case 'I':
- get_12_hour(tm->tm_hour, b, e, err, ct);
- break;
- case 'j':
- get_day_year_num(tm->tm_yday, b, e, err, ct);
- break;
- case 'm':
- get_month(tm->tm_mon, b, e, err, ct);
- break;
- case 'M':
- get_minute(tm->tm_min, b, e, err, ct);
- break;
- case 'n':
- case 't':
- get_white_space(b, e, err, ct);
- break;
- // case 'p':
- // get_am_pm(tm->tm_hour, b, e, err, ct);
- // break;
- case 'r':
- {
- const char_type fm[] = {'%', 'I', ':', '%', 'M', ':', '%', 'S', ' ', '%', 'p'};
- b = get(b, e, iob, err, tm, fm, fm + sizeof(fm)/sizeof(fm[0]));
- }
- break;
- case 'R':
- {
- const char_type fm[] = {'%', 'H', ':', '%', 'M'};
- b = get(b, e, iob, err, tm, fm, fm + sizeof(fm)/sizeof(fm[0]));
- }
- break;
- case 'S':
- get_second(tm->tm_sec, b, e, err, ct);
- break;
- case 'T':
- {
- const char_type fm[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
- b = get(b, e, iob, err, tm, fm, fm + sizeof(fm)/sizeof(fm[0]));
- }
- break;
- case 'w':
- {
- get_weekday(tm->tm_wday, b, e, err, ct);
- }
- break;
- case 'x':
- return that_.get_date(b, e, iob, err, tm);
- // case 'X':
- // return that_.get_time(b, e, iob, err, tm);
- // {
- // const string_type& fm = X();
- // b = that_.get(b, e, iob, err, tm, fm.data(), fm.data() + fm.size());
- // }
- // break;
- // case 'y':
- // get_year(tm->tm_year, b, e, err, ct);
- break;
- case 'Y':
- get_year4(tm->tm_year, b, e, err, ct);
- break;
- case '%':
- get_percent(b, e, err, ct);
- break;
- default:
- err |= std::ios_base::failbit;
- }
- return b;
- }
- InputIterator get(
- iter_type b, iter_type e,
- std::ios_base& iob,
- std::ios_base::iostate& err, std::tm* tm,
- const char_type* fmtb, const char_type* fmte) const
- {
- const std::ctype<char_type>& ct = std::use_facet<std::ctype<char_type> >(iob.getloc());
- err = std::ios_base::goodbit;
- while (fmtb != fmte && err == std::ios_base::goodbit)
- {
- if (b == e)
- {
- err = std::ios_base::failbit;
- break;
- }
- if (ct.narrow(*fmtb, 0) == '%')
- {
- if (++fmtb == fmte)
- {
- err = std::ios_base::failbit;
- break;
- }
- char cmd = ct.narrow(*fmtb, 0);
- char opt = '\0';
- if (cmd == 'E' || cmd == '0')
- {
- if (++fmtb == fmte)
- {
- err = std::ios_base::failbit;
- break;
- }
- opt = cmd;
- cmd = ct.narrow(*fmtb, 0);
- }
- b = get(b, e, iob, err, tm, cmd, opt);
- ++fmtb;
- }
- else if (ct.is(std::ctype_base::space, *fmtb))
- {
- for (++fmtb; fmtb != fmte && ct.is(std::ctype_base::space, *fmtb); ++fmtb)
- ;
- for ( ; b != e && ct.is(std::ctype_base::space, *b); ++b)
- ;
- }
- else if (ct.toupper(*b) == ct.toupper(*fmtb))
- {
- ++b;
- ++fmtb;
- }
- else
- err = std::ios_base::failbit;
- }
- if (b == e)
- err |= std::ios_base::eofbit;
- return b;
- }
- };
- template <class CharT>
- class time_manip: public manip<time_manip<CharT> >
- {
- std::basic_string<CharT> fmt_;
- timezone tz_;
- public:
- time_manip(timezone tz, std::basic_string<CharT> fmt)
- // todo move semantics
- :
- fmt_(fmt), tz_(tz)
- {
- }
- /**
- * Change the timezone and time format ios state;
- */
- void operator()(std::ios_base &ios) const
- {
- set_time_fmt<CharT> (ios, fmt_);
- set_timezone(ios, tz_);
- }
- };
- class time_man: public manip<time_man>
- {
- timezone tz_;
- public:
- time_man(timezone tz)
- // todo move semantics
- :
- tz_(tz)
- {
- }
- /**
- * Change the timezone and time format ios state;
- */
- void operator()(std::ios_base &ios) const
- {
- //set_time_fmt<typename out_stream::char_type>(ios, "");
- set_timezone(ios, tz_);
- }
- };
- }
- template <class CharT>
- inline detail::time_manip<CharT> time_fmt(timezone tz, const CharT* fmt)
- {
- return detail::time_manip<CharT>(tz, fmt);
- }
- template <class CharT>
- inline detail::time_manip<CharT> time_fmt(timezone tz, std::basic_string<CharT> fmt)
- {
- // todo move semantics
- return detail::time_manip<CharT>(tz, fmt);
- }
- inline detail::time_man time_fmt(timezone f)
- {
- return detail::time_man(f);
- }
- /**
- * time_fmt_io_saver i/o saver.
- *
- * See Boost.IO i/o state savers for a motivating compression.
- */
- template <typename CharT = char, typename Traits = std::char_traits<CharT> >
- struct time_fmt_io_saver
- {
- //! the type of the state to restore
- //typedef std::basic_ostream<CharT, Traits> state_type;
- typedef std::ios_base state_type;
- //! the type of aspect to save
- typedef std::basic_string<CharT, Traits> aspect_type;
- /**
- * Explicit construction from an i/o stream.
- *
- * Store a reference to the i/o stream and the value of the associated @c time format .
- */
- explicit time_fmt_io_saver(state_type &s) :
- s_save_(s), a_save_(get_time_fmt<CharT>(s_save_))
- {
- }
- /**
- * Construction from an i/o stream and a @c time format to restore.
- *
- * Stores a reference to the i/o stream and the value @c new_value to restore given as parameter.
- */
- time_fmt_io_saver(state_type &s, aspect_type new_value) :
- s_save_(s), a_save_(get_time_fmt<CharT>(s_save_))
- {
- set_time_fmt(s_save_, new_value);
- }
- /**
- * Destructor.
- *
- * Restores the i/o stream with the format to be restored.
- */
- ~time_fmt_io_saver()
- {
- this->restore();
- }
- /**
- * Restores the i/o stream with the time format to be restored.
- */
- void restore()
- {
- set_time_fmt(s_save_, a_save_);
- }
- private:
- state_type& s_save_;
- aspect_type a_save_;
- };
- /**
- * timezone_io_saver i/o saver.
- *
- * See Boost.IO i/o state savers for a motivating compression.
- */
- struct timezone_io_saver
- {
- //! the type of the state to restore
- typedef std::ios_base state_type;
- //! the type of aspect to save
- typedef timezone aspect_type;
- /**
- * Explicit construction from an i/o stream.
- *
- * Store a reference to the i/o stream and the value of the associated @c timezone.
- */
- explicit timezone_io_saver(state_type &s) :
- s_save_(s), a_save_(get_timezone(s_save_))
- {
- }
- /**
- * Construction from an i/o stream and a @c timezone to restore.
- *
- * Stores a reference to the i/o stream and the value @c new_value to restore given as parameter.
- */
- timezone_io_saver(state_type &s, aspect_type new_value) :
- s_save_(s), a_save_(get_timezone(s_save_))
- {
- set_timezone(s_save_, new_value);
- }
- /**
- * Destructor.
- *
- * Restores the i/o stream with the format to be restored.
- */
- ~timezone_io_saver()
- {
- this->restore();
- }
- /**
- * Restores the i/o stream with the timezone to be restored.
- */
- void restore()
- {
- set_timezone(s_save_, a_save_);
- }
- private:
- timezone_io_saver& operator=(timezone_io_saver const& rhs) ;
- state_type& s_save_;
- aspect_type a_save_;
- };
- /**
- *
- * @param os
- * @param tp
- * @Effects Behaves as a formatted output function. After constructing a @c sentry object, if the @ sentry
- * converts to true, calls to @c facet.put(os,os,os.fill(),tp) where @c facet is the @c time_point_put<CharT>
- * facet associated to @c os or a new created instance of the default @c time_point_put<CharT> facet.
- * @return @c os.
- */
- template <class CharT, class Traits, class Clock, class Duration>
- std::basic_ostream<CharT, Traits>&
- operator<<(std::basic_ostream<CharT, Traits>& os, const time_point<Clock, Duration>& tp)
- {
- bool failed = false;
- BOOST_TRY
- {
- std::ios_base::iostate err = std::ios_base::goodbit;
- BOOST_TRY
- {
- typename std::basic_ostream<CharT, Traits>::sentry opfx(os);
- if (bool(opfx))
- {
- if (!std::has_facet<time_point_put<CharT> >(os.getloc()))
- {
- if (time_point_put<CharT> ().put(os, os, os.fill(), tp) .failed())
- {
- err = std::ios_base::badbit;
- }
- }
- else
- {
- if (std::use_facet<time_point_put<CharT> >(os.getloc()) .put(os, os, os.fill(), tp).failed())
- {
- err = std::ios_base::badbit;
- }
- }
- os.width(0);
- }
- }
- BOOST_CATCH (...)
- {
- bool flag = false;
- BOOST_TRY
- {
- os.setstate(std::ios_base::failbit);
- }
- BOOST_CATCH (std::ios_base::failure )
- {
- flag = true;
- }
- BOOST_CATCH_END
- if (flag) throw;
- }
- BOOST_CATCH_END
- if (err) os.setstate(err);
- return os;
- }
- BOOST_CATCH (...)
- {
- failed = true;
- }
- BOOST_CATCH_END
- if (failed) os.setstate(std::ios_base::failbit | std::ios_base::badbit);
- return os;
- }
- template <class CharT, class Traits, class Clock, class Duration>
- std::basic_istream<CharT, Traits>&
- operator>>(std::basic_istream<CharT, Traits>& is, time_point<Clock, Duration>& tp)
- {
- std::ios_base::iostate err = std::ios_base::goodbit;
- BOOST_TRY
- {
- typename std::basic_istream<CharT, Traits>::sentry ipfx(is);
- if (bool(ipfx))
- {
- if (!std::has_facet<time_point_get<CharT> >(is.getloc()))
- {
- time_point_get<CharT> ().get(is, std::istreambuf_iterator<CharT, Traits>(), is, err, tp);
- }
- else
- {
- std::use_facet<time_point_get<CharT> >(is.getloc()).get(is, std::istreambuf_iterator<CharT, Traits>(), is,
- err, tp);
- }
- }
- }
- BOOST_CATCH (...)
- {
- bool flag = false;
- BOOST_TRY
- {
- is.setstate(std::ios_base::failbit);
- }
- BOOST_CATCH (std::ios_base::failure )
- {
- flag = true;
- }
- BOOST_CATCH_END
- if (flag) throw;
- }
- BOOST_CATCH_END
- if (err) is.setstate(err);
- return is;
- }
- namespace detail
- {
- //#if defined BOOST_CHRONO_INTERNAL_TIMEGM
- inline int32_t is_leap(int32_t year)
- {
- if(year % 400 == 0)
- return 1;
- if(year % 100 == 0)
- return 0;
- if(year % 4 == 0)
- return 1;
- return 0;
- }
- inline int32_t days_from_0(int32_t year)
- {
- year--;
- return 365 * year + (year / 400) - (year/100) + (year / 4);
- }
- inline int32_t days_from_1970(int32_t year)
- {
- static const int32_t days_from_0_to_1970 = days_from_0(1970);
- return days_from_0(year) - days_from_0_to_1970;
- }
- inline int32_t days_from_1jan(int32_t year,int32_t month,int32_t day)
- {
- static const int32_t days[2][12] =
- {
- { 0,31,59,90,120,151,181,212,243,273,304,334},
- { 0,31,60,91,121,152,182,213,244,274,305,335}
- };
- return days[is_leap(year)][month-1] + day - 1;
- }
- inline time_t internal_timegm(std::tm const *t)
- {
- int year = t->tm_year + 1900;
- int month = t->tm_mon;
- if(month > 11)
- {
- year += month/12;
- month %= 12;
- }
- else if(month < 0)
- {
- int years_diff = (-month + 11)/12;
- year -= years_diff;
- month+=12 * years_diff;
- }
- month++;
- int day = t->tm_mday;
- int day_of_year = days_from_1jan(year,month,day);
- int days_since_epoch = days_from_1970(year) + day_of_year ;
- time_t seconds_in_day = 3600 * 24;
- time_t result = seconds_in_day * days_since_epoch + 3600 * t->tm_hour + 60 * t->tm_min + t->tm_sec;
- return result;
- }
- //#endif
- /**
- * from_ymd could be made more efficient by using a table
- * day_count_table indexed by the y%400.
- * This table could contain the day_count
- * by*365 + by/4 - by/100 + by/400
- *
- * from_ymd = (by/400)*days_by_400_years+day_count_table[by%400] +
- * days_in_year_before[is_leap_table[by%400]][m-1] + d;
- */
- inline unsigned days_before_years(int32_t y)
- {
- return y * 365 + y / 4 - y / 100 + y / 400;
- }
- // Returns year/month/day triple in civil calendar
- // Preconditions: z is number of days since 1970-01-01 and is in the range:
- // [numeric_limits<Int>::min(), numeric_limits<Int>::max()-719468].
- template <class Int>
- //constexpr
- void
- inline civil_from_days(Int z, Int& y, unsigned& m, unsigned& d) BOOST_NOEXCEPT
- {
- BOOST_STATIC_ASSERT_MSG(std::numeric_limits<unsigned>::digits >= 18,
- "This algorithm has not been ported to a 16 bit unsigned integer");
- BOOST_STATIC_ASSERT_MSG(std::numeric_limits<Int>::digits >= 20,
- "This algorithm has not been ported to a 16 bit signed integer");
- z += 719468;
- const Int era = (z >= 0 ? z : z - 146096) / 146097;
- const unsigned doe = static_cast<unsigned>(z - era * 146097); // [0, 146096]
- const unsigned yoe = (doe - doe/1460 + doe/36524 - doe/146096) / 365; // [0, 399]
- y = static_cast<Int>(yoe) + era * 400;
- const unsigned doy = doe - (365*yoe + yoe/4 - yoe/100); // [0, 365]
- const unsigned mp = (5*doy + 2)/153; // [0, 11]
- d = doy - (153*mp+2)/5 + 1; // [1, 31]
- m = mp + (mp < 10 ? 3 : -9); // [1, 12]
- y += (m <= 2);
- --m;
- }
- inline std::tm * internal_gmtime(std::time_t const* t, std::tm *tm)
- {
- if (t==0) return 0;
- if (tm==0) return 0;
- #if 0
- static const unsigned char
- day_of_year_month[2][366] =
- {
- { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12 },
- { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12
- } };
- static const int32_t days_in_year_before[2][13] =
- {
- { -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 },
- { -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }
- };
- #endif
- const time_t seconds_in_day = 3600 * 24;
- int32_t days_since_epoch = static_cast<int32_t>(*t / seconds_in_day);
- int32_t hms = static_cast<int32_t>(*t - seconds_in_day*days_since_epoch);
- if (hms < 0) {
- days_since_epoch-=1;
- hms = seconds_in_day+hms;
- }
- #if 0
- int32_t x = days_since_epoch;
- int32_t y = static_cast<int32_t> (static_cast<long long> (x + 2) * 400
- / 146097);
- const int32_t ym1 = y - 1;
- int32_t doy = x - days_before_years(y);
- const int32_t doy1 = x - days_before_years(ym1);
- const int32_t N = std::numeric_limits<int>::digits - 1;
- const int32_t mask1 = doy >> N; // arithmetic rshift - not portable - but nearly universal
- const int32_t mask0 = ~mask1;
- doy = (doy & mask0) | (doy1 & mask1);
- y = (y & mask0) | (ym1 & mask1);
- //y -= 32767 + 2;
- y += 70;
- tm->tm_year=y;
- const int32_t leap = is_leap(y);
- tm->tm_mon = day_of_year_month[leap][doy]-1;
- tm->tm_mday = doy - days_in_year_before[leap][tm->tm_mon] ;
- #else
- int32_t y;
- unsigned m, d;
- civil_from_days(days_since_epoch, y, m, d);
- tm->tm_year=y-1900; tm->tm_mon=m; tm->tm_mday=d;
- #endif
- tm->tm_hour = hms / 3600;
- const int ms = hms % 3600;
- tm->tm_min = ms / 60;
- tm->tm_sec = ms % 60;
- tm->tm_isdst = -1;
- (void)mktime(tm);
- return tm;
- }
- } // detail
- #ifndef BOOST_CHRONO_NO_UTC_TIMEPOINT
- #if defined BOOST_CHRONO_PROVIDES_DATE_IO_FOR_SYSTEM_CLOCK_TIME_POINT
- template <class CharT, class Traits, class Duration>
- std::basic_ostream<CharT, Traits>&
- operator<<(std::basic_ostream<CharT, Traits>& os, const time_point<system_clock, Duration>& tp)
- {
- typename std::basic_ostream<CharT, Traits>::sentry ok(os);
- if (bool(ok))
- {
- bool failed = false;
- BOOST_TRY
- {
- const CharT* pb = 0; //nullptr;
- const CharT* pe = pb;
- std::basic_string<CharT> fmt = get_time_fmt<CharT> (os);
- pb = fmt.data();
- pe = pb + fmt.size();
- timezone tz = get_timezone(os);
- std::locale loc = os.getloc();
- time_t t = system_clock::to_time_t(time_point_cast<system_clock::duration>(tp));
- std::tm tm;
- std::memset(&tm, 0, sizeof(std::tm));
- if (tz == timezone::local)
- {
- #if defined BOOST_WINDOWS && ! defined(__CYGWIN__)
- #if BOOST_MSVC < 1400 // localtime_s doesn't exist in vc7.1
- std::tm *tmp = 0;
- if ((tmp=localtime(&t)) == 0)
- failed = true;
- else
- tm =*tmp;
- # else
- if (localtime_s(&tm, &t) != 0) failed = true;
- # endif
- #else
- if (localtime_r(&t, &tm) == 0) failed = true;
- #endif
- }
- else
- {
- #if defined BOOST_CHRONO_INTERNAL_GMTIME
- if (detail::internal_gmtime(&t, &tm) == 0) failed = true;
- #elif defined BOOST_WINDOWS && ! defined(__CYGWIN__)
- std::tm *tmp = 0;
- if((tmp = gmtime(&t)) == 0)
- failed = true;
- else
- tm = *tmp;
- #else
- if (gmtime_r(&t, &tm) == 0) failed = true;
- tm.tm_isdst = -1;
- (void)mktime(&tm);
- #endif
- }
- if (!failed)
- {
- const std::time_put<CharT>& tpf = std::use_facet<std::time_put<CharT> >(loc);
- if (pb == pe)
- {
- CharT pattern[] =
- { '%', 'Y', '-', '%', 'm', '-', '%', 'd', ' ', '%', 'H', ':', '%', 'M', ':' };
- pb = pattern;
- pe = pb + sizeof (pattern) / sizeof(CharT);
- failed = tpf.put(os, os, os.fill(), &tm, pb, pe).failed();
- if (!failed)
- {
- duration<fractional_seconds> d = tp - system_clock::from_time_t(t) + seconds(tm.tm_sec);
- if (d.count() < 10) os << CharT('0');
- //if (! os.good()) {
- // throw "exception";
- //}
- std::ios::fmtflags flgs = os.flags();
- os.setf(std::ios::fixed, std::ios::floatfield);
- //if (! os.good()) {
- //throw "exception";
- //}
- os.precision(9);
- os << d.count();
- //if (! os.good()) {
- //throw "exception";
- //}
- os.flags(flgs);
- if (tz == timezone::local)
- {
- CharT sub_pattern[] =
- { ' ', '%', 'z' };
- pb = sub_pattern;
- pe = pb + +sizeof (sub_pattern) / sizeof(CharT);
- failed = tpf.put(os, os, os.fill(), &tm, pb, pe).failed();
- }
- else
- {
- CharT sub_pattern[] =
- { ' ', '+', '0', '0', '0', '0', 0 };
- os << sub_pattern;
- }
- }
- }
- else
- {
- failed = tpf.put(os, os, os.fill(), &tm, pb, pe).failed();
- }
- }
- }
- BOOST_CATCH (...)
- {
- failed = true;
- }
- BOOST_CATCH_END
- if (failed)
- {
- os.setstate(std::ios_base::failbit | std::ios_base::badbit);
- }
- }
- return os;
- }
- #endif
- namespace detail
- {
- template <class CharT, class InputIterator>
- minutes extract_z(InputIterator& b, InputIterator e, std::ios_base::iostate& err, const std::ctype<CharT>& ct)
- {
- int min = 0;
- if (b != e)
- {
- char cn = ct.narrow(*b, 0);
- if (cn != '+' && cn != '-')
- {
- err |= std::ios_base::failbit;
- return minutes(0);
- }
- int sn = cn == '-' ? -1 : 1;
- int hr = 0;
- for (int i = 0; i < 2; ++i)
- {
- if (++b == e)
- {
- err |= std::ios_base::eofbit | std::ios_base::failbit;
- return minutes(0);
- }
- cn = ct.narrow(*b, 0);
- if (! ('0' <= cn && cn <= '9'))
- {
- err |= std::ios_base::failbit;
- return minutes(0);
- }
- hr = hr * 10 + cn - '0';
- }
- for (int i = 0; i < 2; ++i)
- {
- if (++b == e)
- {
- err |= std::ios_base::eofbit | std::ios_base::failbit;
- return minutes(0);
- }
- cn = ct.narrow(*b, 0);
- if (! ('0' <= cn && cn <= '9'))
- {
- err |= std::ios_base::failbit;
- return minutes(0);
- }
- min = min * 10 + cn - '0';
- }
- if (++b == e) {
- err |= std::ios_base::eofbit;
- }
- min += hr * 60;
- min *= sn;
- }
- else
- {
- err |= std::ios_base::eofbit | std::ios_base::failbit;
- }
- return minutes(min);
- }
- } // detail
- #if defined BOOST_CHRONO_PROVIDES_DATE_IO_FOR_SYSTEM_CLOCK_TIME_POINT
- template <class CharT, class Traits, class Duration>
- std::basic_istream<CharT, Traits>&
- operator>>(std::basic_istream<CharT, Traits>& is, time_point<system_clock, Duration>& tp)
- {
- typename std::basic_istream<CharT, Traits>::sentry ok(is);
- if (bool(ok))
- {
- std::ios_base::iostate err = std::ios_base::goodbit;
- BOOST_TRY
- {
- const CharT* pb = 0; //nullptr;
- const CharT* pe = pb;
- std::basic_string<CharT> fmt = get_time_fmt<CharT> (is);
- pb = fmt.data();
- pe = pb + fmt.size();
- timezone tz = get_timezone(is);
- std::locale loc = is.getloc();
- const std::time_get<CharT>& tg = std::use_facet<std::time_get<CharT> >(loc);
- const std::ctype<CharT>& ct = std::use_facet<std::ctype<CharT> >(loc);
- tm tm; // {0}
- std::memset(&tm, 0, sizeof(std::tm));
- typedef std::istreambuf_iterator<CharT, Traits> It;
- if (pb == pe)
- {
- CharT pattern[] =
- { '%', 'Y', '-', '%', 'm', '-', '%', 'd', ' ', '%', 'H', ':', '%', 'M', ':' };
- pb = pattern;
- pe = pb + sizeof (pattern) / sizeof(CharT);
- #if defined BOOST_CHRONO_USES_INTERNAL_TIME_GET
- const detail::time_get<CharT>& dtg(tg);
- dtg.get(is, 0, is, err, &tm, pb, pe);
- #else
- tg.get(is, 0, is, err, &tm, pb, pe);
- #endif
- if (err & std::ios_base::failbit) goto exit;
- fractional_seconds sec;
- CharT c = CharT();
- std::ios::fmtflags flgs = is.flags();
- is.setf(std::ios::fixed, std::ios::floatfield);
- is.precision(9);
- is >> sec;
- is.flags(flgs);
- if (is.fail())
- {
- err |= std::ios_base::failbit;
- goto exit;
- }
- It i(is);
- It eof;
- c = *i;
- if (++i == eof || c != ' ')
- {
- err |= std::ios_base::failbit;
- goto exit;
- }
- minutes min = detail::extract_z(i, eof, err, ct);
- if (err & std::ios_base::failbit) goto exit;
- time_t t;
- #if defined BOOST_CHRONO_INTERNAL_TIMEGM
- t = detail::internal_timegm(&tm);
- #else
- t = timegm(&tm);
- #endif
- tp = time_point_cast<Duration>(
- system_clock::from_time_t(t) - min + round<system_clock::duration> (duration<fractional_seconds> (sec))
- );
- }
- else
- {
- const CharT z[2] =
- { '%', 'z' };
- const CharT* fz = std::search(pb, pe, z, z + 2);
- #if defined BOOST_CHRONO_USES_INTERNAL_TIME_GET
- const detail::time_get<CharT>& dtg(tg);
- dtg.get(is, 0, is, err, &tm, pb, fz);
- #else
- tg.get(is, 0, is, err, &tm, pb, fz);
- #endif
- minutes minu(0);
- if (fz != pe)
- {
- if (err != std::ios_base::goodbit)
- {
- err |= std::ios_base::failbit;
- goto exit;
- }
- It i(is);
- It eof;
- minu = detail::extract_z(i, eof, err, ct);
- if (err & std::ios_base::failbit) goto exit;
- if (fz + 2 != pe)
- {
- if (err != std::ios_base::goodbit)
- {
- err |= std::ios_base::failbit;
- goto exit;
- }
- #if defined BOOST_CHRONO_USES_INTERNAL_TIME_GET
- const detail::time_get<CharT>& dtg(tg);
- dtg.get(is, 0, is, err, &tm, fz + 2, pe);
- #else
- tg.get(is, 0, is, err, &tm, fz + 2, pe);
- #endif
- if (err & std::ios_base::failbit) goto exit;
- }
- }
- tm.tm_isdst = -1;
- time_t t;
- if (tz == timezone::utc || fz != pe)
- {
- #if defined BOOST_CHRONO_INTERNAL_TIMEGM
- t = detail::internal_timegm(&tm);
- #else
- t = timegm(&tm);
- #endif
- }
- else
- {
- t = mktime(&tm);
- }
- tp = time_point_cast<Duration>(
- system_clock::from_time_t(t) - minu
- );
- }
- }
- BOOST_CATCH (...)
- {
- err |= std::ios_base::badbit | std::ios_base::failbit;
- }
- BOOST_CATCH_END
- exit: is.setstate(err);
- }
- return is;
- }
- #endif
- #endif //UTC
- } // chrono
- }
- #endif // header
|