1 // (C) Copyright Howard Hinnant
2 // (C) Copyright 2010-2011 Vicente J. Botet Escriba
3 // Use, modification and distribution are subject to the Boost Software License,
4 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt).
7 //===-------------------------- locale ------------------------------------===//
9 // The LLVM Compiler Infrastructure
11 // This file is dual licensed under the MIT and the University of Illinois Open
12 // Source Licenses. See LICENSE.TXT for details.
14 //===----------------------------------------------------------------------===//
16 // This code was adapted by Vicente from Howard Hinnant's experimental work
17 // on chrono i/o to Boost and some functions from libc++/locale to emulate the missing time_get::get()
19 #ifndef BOOST_CHRONO_IO_TIME_POINT_IO_HPP
20 #define BOOST_CHRONO_IO_TIME_POINT_IO_HPP
22 #include <boost/chrono/io/time_point_put.hpp>
23 #include <boost/chrono/io/time_point_get.hpp>
24 #include <boost/chrono/io/duration_io.hpp>
25 #include <boost/chrono/io/ios_base_state.hpp>
26 #include <boost/chrono/io/utility/manip_base.hpp>
27 #include <boost/chrono/time_point.hpp>
28 #include <boost/chrono/clock_string.hpp>
29 #include <boost/chrono/round.hpp>
30 #include <boost/chrono/detail/scan_keyword.hpp>
31 #include <boost/static_assert.hpp>
32 #include <boost/detail/no_exceptions_support.hpp>
37 #define BOOST_CHRONO_INTERNAL_TIMEGM \
38 ( defined BOOST_WINDOWS && ! defined(__CYGWIN__) ) \
39 || (defined(sun) || defined(__sun)) \
40 || (defined __IBMCPP__) \
41 || defined __ANDROID__ \
42 || defined __QNXNTO__ \
43 || (defined(_AIX) && defined __GNUC__)
45 #define BOOST_CHRONO_INTERNAL_GMTIME \
46 (defined BOOST_WINDOWS && ! defined(__CYGWIN__)) \
47 || ( (defined(sun) || defined(__sun)) && defined __GNUC__) \
48 || (defined __IBMCPP__) \
49 || defined __ANDROID__ \
50 || (defined(_AIX) && defined __GNUC__)
52 #define BOOST_CHRONO_USES_INTERNAL_TIME_GET
58 typedef double fractional_seconds;
63 template <class CharT, class InputIterator = std::istreambuf_iterator<CharT> >
66 std::time_get<CharT> const &that_;
67 time_get(std::time_get<CharT> const& that) : that_(that) {}
69 typedef std::time_get<CharT> facet;
70 typedef typename facet::iter_type iter_type;
71 typedef typename facet::char_type char_type;
72 typedef std::basic_string<char_type> string_type;
76 InputIterator& b, InputIterator e,
77 std::ios_base::iostate& err,
78 const std::ctype<CharT>& ct,
81 // Precondition: n >= 1
84 err |= std::ios_base::eofbit | std::ios_base::failbit;
89 if (!ct.is(std::ctype_base::digit, c))
91 err |= std::ios_base::failbit;
94 int r = ct.narrow(c, 0) - '0';
95 for (++b, --n; b != e && n > 0; ++b, --n)
99 if (!ct.is(std::ctype_base::digit, c))
101 r = r * 10 + ct.narrow(c, 0) - '0';
104 err |= std::ios_base::eofbit;
111 iter_type& b, iter_type e,
112 std::ios_base::iostate& err,
113 const std::ctype<char_type>& ct) const
115 int t = get_up_to_n_digits(b, e, err, ct, 2);
116 if (!(err & std::ios_base::failbit) && 1 <= t && t <= 31)
119 err |= std::ios_base::failbit;
124 iter_type& b, iter_type e,
125 std::ios_base::iostate& err,
126 const std::ctype<char_type>& ct) const
128 int t = get_up_to_n_digits(b, e, err, ct, 2);
129 if (!(err & std::ios_base::failbit) && 1 <= t && t <= 12)
132 err |= std::ios_base::failbit;
136 void get_year4(int& y,
137 iter_type& b, iter_type e,
138 std::ios_base::iostate& err,
139 const std::ctype<char_type>& ct) const
141 int t = get_up_to_n_digits(b, e, err, ct, 4);
142 if (!(err & std::ios_base::failbit))
148 iter_type& b, iter_type e,
149 std::ios_base::iostate& err,
150 const std::ctype<char_type>& ct) const
152 int t = get_up_to_n_digits(b, e, err, ct, 2);
153 if (!(err & std::ios_base::failbit) && t <= 23)
156 err |= std::ios_base::failbit;
161 iter_type& b, iter_type e,
162 std::ios_base::iostate& err,
163 const std::ctype<char_type>& ct) const
165 int t = get_up_to_n_digits(b, e, err, ct, 2);
166 if (!(err & std::ios_base::failbit) && t <= 59)
169 err |= std::ios_base::failbit;
172 void get_second(int& s,
173 iter_type& b, iter_type e,
174 std::ios_base::iostate& err,
175 const std::ctype<char_type>& ct) const
177 int t = get_up_to_n_digits(b, e, err, ct, 2);
178 if (!(err & std::ios_base::failbit) && t <= 60)
181 err |= std::ios_base::failbit;
184 void get_white_space(iter_type& b, iter_type e,
185 std::ios_base::iostate& err,
186 const std::ctype<char_type>& ct) const
188 for (; b != e && ct.is(std::ctype_base::space, *b); ++b)
191 err |= std::ios_base::eofbit;
194 void get_12_hour(int& h,
195 iter_type& b, iter_type e,
196 std::ios_base::iostate& err,
197 const std::ctype<char_type>& ct) const
199 int t = get_up_to_n_digits(b, e, err, ct, 2);
200 if (!(err & std::ios_base::failbit) && 1 <= t && t <= 12)
203 err |= std::ios_base::failbit;
206 void get_percent(iter_type& b, iter_type e,
207 std::ios_base::iostate& err,
208 const std::ctype<char_type>& ct) const
212 err |= std::ios_base::eofbit | std::ios_base::failbit;
215 if (ct.narrow(*b, 0) != '%')
216 err |= std::ios_base::failbit;
218 err |= std::ios_base::eofbit;
221 void get_day_year_num(int& d,
222 iter_type& b, iter_type e,
223 std::ios_base::iostate& err,
224 const std::ctype<char_type>& ct) const
226 int t = get_up_to_n_digits(b, e, err, ct, 3);
227 if (!(err & std::ios_base::failbit) && 1 <= t && t <= 366)
230 err |= std::ios_base::failbit;
235 iter_type& b, iter_type e,
236 std::ios_base::iostate& err,
237 const std::ctype<char_type>& ct) const
239 int t = get_up_to_n_digits(b, e, err, ct, 1);
240 if (!(err & std::ios_base::failbit) && t <= 6)
243 err |= std::ios_base::failbit;
249 iter_type& b, iter_type e,
250 std::ios_base::iostate& err,
251 const std::ctype<char_type>& ct) const
253 const string_type* ap = am_pm();
254 if (ap[0].size() + ap[1].size() == 0)
256 err |= ios_base::failbit;
259 ptrdiff_t i = detail::scan_keyword(b, e, ap, ap+2, ct, err, false) - ap;
260 if (i == 0 && h == 12)
262 else if (i == 1 && h < 12)
269 iter_type b, iter_type e,
271 std::ios_base::iostate& err,
273 char fmt, char) const
275 err = std::ios_base::goodbit;
276 const std::ctype<char_type>& ct = std::use_facet<std::ctype<char_type> >(iob.getloc());
284 std::memset(&tm2, 0, sizeof(std::tm));
285 that_.get_weekday(b, e, iob, err, &tm2);
286 //tm->tm_wday = tm2.tm_wday;
294 std::memset(&tm2, 0, sizeof(std::tm));
295 that_.get_monthname(b, e, iob, err, &tm2);
296 //tm->tm_mon = tm2.tm_mon;
301 // const string_type& fm = c();
302 // b = get(b, e, iob, err, tm, fm.data(), fm.data() + fm.size());
307 get_day(tm->tm_mday, b, e, err, ct);
311 const char_type fm[] = {'%', 'm', '/', '%', 'd', '/', '%', 'y'};
312 b = get(b, e, iob, err, tm, fm, fm + sizeof(fm)/sizeof(fm[0]));
317 const char_type fm[] = {'%', 'Y', '-', '%', 'm', '-', '%', 'd'};
318 b = get(b, e, iob, err, tm, fm, fm + sizeof(fm)/sizeof(fm[0]));
322 get_hour(tm->tm_hour, b, e, err, ct);
325 get_12_hour(tm->tm_hour, b, e, err, ct);
328 get_day_year_num(tm->tm_yday, b, e, err, ct);
331 get_month(tm->tm_mon, b, e, err, ct);
334 get_minute(tm->tm_min, b, e, err, ct);
338 get_white_space(b, e, err, ct);
341 // get_am_pm(tm->tm_hour, b, e, err, ct);
345 const char_type fm[] = {'%', 'I', ':', '%', 'M', ':', '%', 'S', ' ', '%', 'p'};
346 b = get(b, e, iob, err, tm, fm, fm + sizeof(fm)/sizeof(fm[0]));
351 const char_type fm[] = {'%', 'H', ':', '%', 'M'};
352 b = get(b, e, iob, err, tm, fm, fm + sizeof(fm)/sizeof(fm[0]));
356 get_second(tm->tm_sec, b, e, err, ct);
360 const char_type fm[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
361 b = get(b, e, iob, err, tm, fm, fm + sizeof(fm)/sizeof(fm[0]));
366 get_weekday(tm->tm_wday, b, e, err, ct);
370 return that_.get_date(b, e, iob, err, tm);
372 // return that_.get_time(b, e, iob, err, tm);
374 // const string_type& fm = X();
375 // b = that_.get(b, e, iob, err, tm, fm.data(), fm.data() + fm.size());
379 // get_year(tm->tm_year, b, e, err, ct);
382 get_year4(tm->tm_year, b, e, err, ct);
385 get_percent(b, e, err, ct);
388 err |= std::ios_base::failbit;
395 iter_type b, iter_type e,
397 std::ios_base::iostate& err, std::tm* tm,
398 const char_type* fmtb, const char_type* fmte) const
400 const std::ctype<char_type>& ct = std::use_facet<std::ctype<char_type> >(iob.getloc());
401 err = std::ios_base::goodbit;
402 while (fmtb != fmte && err == std::ios_base::goodbit)
406 err = std::ios_base::failbit;
409 if (ct.narrow(*fmtb, 0) == '%')
413 err = std::ios_base::failbit;
416 char cmd = ct.narrow(*fmtb, 0);
418 if (cmd == 'E' || cmd == '0')
422 err = std::ios_base::failbit;
426 cmd = ct.narrow(*fmtb, 0);
428 b = get(b, e, iob, err, tm, cmd, opt);
431 else if (ct.is(std::ctype_base::space, *fmtb))
433 for (++fmtb; fmtb != fmte && ct.is(std::ctype_base::space, *fmtb); ++fmtb)
435 for ( ; b != e && ct.is(std::ctype_base::space, *b); ++b)
438 else if (ct.toupper(*b) == ct.toupper(*fmtb))
444 err = std::ios_base::failbit;
447 err |= std::ios_base::eofbit;
454 template <class CharT>
455 class time_manip: public manip<time_manip<CharT> >
457 std::basic_string<CharT> fmt_;
461 time_manip(timezone tz, std::basic_string<CharT> fmt)
462 // todo move semantics
469 * Change the timezone and time format ios state;
471 void operator()(std::ios_base &ios) const
473 set_time_fmt<CharT> (ios, fmt_);
474 set_timezone(ios, tz_);
478 class time_man: public manip<time_man>
483 time_man(timezone tz)
484 // todo move semantics
491 * Change the timezone and time format ios state;
493 void operator()(std::ios_base &ios) const
495 //set_time_fmt<typename out_stream::char_type>(ios, "");
496 set_timezone(ios, tz_);
502 template <class CharT>
503 inline detail::time_manip<CharT> time_fmt(timezone tz, const CharT* fmt)
505 return detail::time_manip<CharT>(tz, fmt);
508 template <class CharT>
509 inline detail::time_manip<CharT> time_fmt(timezone tz, std::basic_string<CharT> fmt)
511 // todo move semantics
512 return detail::time_manip<CharT>(tz, fmt);
515 inline detail::time_man time_fmt(timezone f)
517 return detail::time_man(f);
521 * time_fmt_io_saver i/o saver.
523 * See Boost.IO i/o state savers for a motivating compression.
525 template <typename CharT = char, typename Traits = std::char_traits<CharT> >
526 struct time_fmt_io_saver
529 //! the type of the state to restore
530 //typedef std::basic_ostream<CharT, Traits> state_type;
531 typedef std::ios_base state_type;
533 //! the type of aspect to save
534 typedef std::basic_string<CharT, Traits> aspect_type;
537 * Explicit construction from an i/o stream.
539 * Store a reference to the i/o stream and the value of the associated @c time format .
541 explicit time_fmt_io_saver(state_type &s) :
542 s_save_(s), a_save_(get_time_fmt<CharT>(s_save_))
547 * Construction from an i/o stream and a @c time format to restore.
549 * Stores a reference to the i/o stream and the value @c new_value to restore given as parameter.
551 time_fmt_io_saver(state_type &s, aspect_type new_value) :
552 s_save_(s), a_save_(get_time_fmt<CharT>(s_save_))
554 set_time_fmt(s_save_, new_value);
560 * Restores the i/o stream with the format to be restored.
568 * Restores the i/o stream with the time format to be restored.
572 set_time_fmt(s_save_, a_save_);
580 * timezone_io_saver i/o saver.
582 * See Boost.IO i/o state savers for a motivating compression.
584 struct timezone_io_saver
587 //! the type of the state to restore
588 typedef std::ios_base state_type;
589 //! the type of aspect to save
590 typedef timezone aspect_type;
593 * Explicit construction from an i/o stream.
595 * Store a reference to the i/o stream and the value of the associated @c timezone.
597 explicit timezone_io_saver(state_type &s) :
598 s_save_(s), a_save_(get_timezone(s_save_))
603 * Construction from an i/o stream and a @c timezone to restore.
605 * Stores a reference to the i/o stream and the value @c new_value to restore given as parameter.
607 timezone_io_saver(state_type &s, aspect_type new_value) :
608 s_save_(s), a_save_(get_timezone(s_save_))
610 set_timezone(s_save_, new_value);
616 * Restores the i/o stream with the format to be restored.
624 * Restores the i/o stream with the timezone to be restored.
628 set_timezone(s_save_, a_save_);
631 timezone_io_saver& operator=(timezone_io_saver const& rhs) ;
641 * @Effects Behaves as a formatted output function. After constructing a @c sentry object, if the @ sentry
642 * converts to true, calls to @c facet.put(os,os,os.fill(),tp) where @c facet is the @c time_point_put<CharT>
643 * facet associated to @c os or a new created instance of the default @c time_point_put<CharT> facet.
646 template <class CharT, class Traits, class Clock, class Duration>
647 std::basic_ostream<CharT, Traits>&
648 operator<<(std::basic_ostream<CharT, Traits>& os, const time_point<Clock, Duration>& tp)
654 std::ios_base::iostate err = std::ios_base::goodbit;
657 typename std::basic_ostream<CharT, Traits>::sentry opfx(os);
660 if (!std::has_facet<time_point_put<CharT> >(os.getloc()))
662 if (time_point_put<CharT> ().put(os, os, os.fill(), tp) .failed())
664 err = std::ios_base::badbit;
669 if (std::use_facet<time_point_put<CharT> >(os.getloc()) .put(os, os, os.fill(), tp).failed())
671 err = std::ios_base::badbit;
682 os.setstate(std::ios_base::failbit);
684 BOOST_CATCH (std::ios_base::failure )
692 if (err) os.setstate(err);
700 if (failed) os.setstate(std::ios_base::failbit | std::ios_base::badbit);
704 template <class CharT, class Traits, class Clock, class Duration>
705 std::basic_istream<CharT, Traits>&
706 operator>>(std::basic_istream<CharT, Traits>& is, time_point<Clock, Duration>& tp)
708 std::ios_base::iostate err = std::ios_base::goodbit;
712 typename std::basic_istream<CharT, Traits>::sentry ipfx(is);
715 if (!std::has_facet<time_point_get<CharT> >(is.getloc()))
717 time_point_get<CharT> ().get(is, std::istreambuf_iterator<CharT, Traits>(), is, err, tp);
721 std::use_facet<time_point_get<CharT> >(is.getloc()).get(is, std::istreambuf_iterator<CharT, Traits>(), is,
731 is.setstate(std::ios_base::failbit);
733 BOOST_CATCH (std::ios_base::failure )
741 if (err) is.setstate(err);
749 //#if BOOST_CHRONO_INTERNAL_TIMEGM
751 inline int32_t is_leap(int32_t year)
761 inline int32_t days_from_0(int32_t year)
764 return 365 * year + (year / 400) - (year/100) + (year / 4);
766 inline int32_t days_from_1970(int32_t year)
768 static const int days_from_0_to_1970 = days_from_0(1970);
769 return days_from_0(year) - days_from_0_to_1970;
771 inline int32_t days_from_1jan(int32_t year,int32_t month,int32_t day)
773 static const int32_t days[2][12] =
775 { 0,31,59,90,120,151,181,212,243,273,304,334},
776 { 0,31,60,91,121,152,182,213,244,274,305,335}
779 return days[is_leap(year)][month-1] + day - 1;
782 inline time_t internal_timegm(std::tm const *t)
784 int year = t->tm_year + 1900;
785 int month = t->tm_mon;
793 int years_diff = (-month + 11)/12;
795 month+=12 * years_diff;
798 int day = t->tm_mday;
799 int day_of_year = days_from_1jan(year,month,day);
800 int days_since_epoch = days_from_1970(year) + day_of_year ;
802 time_t seconds_in_day = 3600 * 24;
803 time_t result = seconds_in_day * days_since_epoch + 3600 * t->tm_hour + 60 * t->tm_min + t->tm_sec;
810 * from_ymd could be made more efficient by using a table
811 * day_count_table indexed by the y%400.
812 * This table could contain the day_count
813 * by*365 + by/4 - by/100 + by/400
815 * from_ymd = (by/400)*days_by_400_years+day_count_table[by%400] +
816 * days_in_year_before[is_leap_table[by%400]][m-1] + d;
818 inline unsigned days_before_years(int32_t y)
820 return y * 365 + y / 4 - y / 100 + y / 400;
823 // Returns year/month/day triple in civil calendar
824 // Preconditions: z is number of days since 1970-01-01 and is in the range:
825 // [numeric_limits<Int>::min(), numeric_limits<Int>::max()-719468].
829 inline civil_from_days(Int z, Int& y, unsigned& m, unsigned& d) BOOST_NOEXCEPT
831 BOOST_STATIC_ASSERT_MSG(std::numeric_limits<unsigned>::digits >= 18,
832 "This algorithm has not been ported to a 16 bit unsigned integer");
833 BOOST_STATIC_ASSERT_MSG(std::numeric_limits<Int>::digits >= 20,
834 "This algorithm has not been ported to a 16 bit signed integer");
836 const Int era = (z >= 0 ? z : z - 146096) / 146097;
837 const unsigned doe = static_cast<unsigned>(z - era * 146097); // [0, 146096]
838 const unsigned yoe = (doe - doe/1460 + doe/36524 - doe/146096) / 365; // [0, 399]
839 y = static_cast<Int>(yoe) + era * 400;
840 const unsigned doy = doe - (365*yoe + yoe/4 - yoe/100); // [0, 365]
841 const unsigned mp = (5*doy + 2)/153; // [0, 11]
842 d = doy - (153*mp+2)/5 + 1; // [1, 31]
843 m = mp + (mp < 10 ? 3 : -9); // [1, 12]
847 inline std::tm * internal_gmtime(std::time_t const* t, std::tm *tm)
853 static const unsigned char
854 day_of_year_month[2][366] =
856 { 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 },
858 { 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
862 static const int32_t days_in_year_before[2][13] =
864 { -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 },
865 { -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }
869 const time_t seconds_in_day = 3600 * 24;
870 int32_t days_since_epoch = static_cast<int32_t>(*t / seconds_in_day);
871 int32_t hms = static_cast<int32_t>(*t - seconds_in_day*days_since_epoch);
874 hms = seconds_in_day+hms;
878 int32_t x = days_since_epoch;
879 int32_t y = static_cast<int32_t> (static_cast<long long> (x + 2) * 400
881 const int32_t ym1 = y - 1;
882 int32_t doy = x - days_before_years(y);
883 const int32_t doy1 = x - days_before_years(ym1);
884 const int32_t N = std::numeric_limits<int>::digits - 1;
885 const int32_t mask1 = doy >> N; // arithmetic rshift - not portable - but nearly universal
886 const int32_t mask0 = ~mask1;
887 doy = (doy & mask0) | (doy1 & mask1);
888 y = (y & mask0) | (ym1 & mask1);
892 const int32_t leap = is_leap(y);
893 tm->tm_mon = day_of_year_month[leap][doy]-1;
894 tm->tm_mday = doy - days_in_year_before[leap][tm->tm_mon] ;
898 civil_from_days(days_since_epoch, y, m, d);
899 tm->tm_year=y-1900; tm->tm_mon=m; tm->tm_mday=d;
902 tm->tm_hour = hms / 3600;
903 const int ms = hms % 3600;
904 tm->tm_min = ms / 60;
905 tm->tm_sec = ms % 60;
913 #ifndef BOOST_CHRONO_NO_UTC_TIMEPOINT
915 #if defined BOOST_CHRONO_PROVIDES_DATE_IO_FOR_SYSTEM_CLOCK_TIME_POINT
917 template <class CharT, class Traits, class Duration>
918 std::basic_ostream<CharT, Traits>&
919 operator<<(std::basic_ostream<CharT, Traits>& os, const time_point<system_clock, Duration>& tp)
921 typename std::basic_ostream<CharT, Traits>::sentry ok(os);
927 const CharT* pb = 0; //nullptr;
928 const CharT* pe = pb;
929 std::basic_string<CharT> fmt = get_time_fmt<CharT> (os);
931 pe = pb + fmt.size();
933 timezone tz = get_timezone(os);
934 std::locale loc = os.getloc();
935 time_t t = system_clock::to_time_t(time_point_cast<system_clock::duration>(tp));
937 std::memset(&tm, 0, sizeof(std::tm));
938 if (tz == timezone::local)
940 #if defined BOOST_WINDOWS && ! defined(__CYGWIN__)
942 if ((tmp=localtime(&t)) == 0)
947 if (localtime_r(&t, &tm) == 0) failed = true;
952 #if BOOST_CHRONO_INTERNAL_GMTIME
953 if (detail::internal_gmtime(&t, &tm) == 0) failed = true;
955 #elif defined BOOST_WINDOWS && ! defined(__CYGWIN__)
957 if((tmp = gmtime(&t)) == 0)
962 if (gmtime_r(&t, &tm) == 0) failed = true;
971 const std::time_put<CharT>& tpf = std::use_facet<std::time_put<CharT> >(loc);
975 { '%', 'Y', '-', '%', 'm', '-', '%', 'd', ' ', '%', 'H', ':', '%', 'M', ':' };
977 pe = pb + sizeof (pattern) / sizeof(CharT);
978 failed = tpf.put(os, os, os.fill(), &tm, pb, pe).failed();
981 duration<fractional_seconds> d = tp - system_clock::from_time_t(t) + seconds(tm.tm_sec);
982 if (d.count() < 10) os << CharT('0');
984 // throw "exception";
986 std::ios::fmtflags flgs = os.flags();
987 os.setf(std::ios::fixed, std::ios::floatfield);
997 if (tz == timezone::local)
999 CharT sub_pattern[] =
1002 pe = pb + +sizeof (sub_pattern) / sizeof(CharT);
1003 failed = tpf.put(os, os, os.fill(), &tm, pb, pe).failed();
1007 CharT sub_pattern[] =
1008 { ' ', '+', '0', '0', '0', '0', 0 };
1015 failed = tpf.put(os, os, os.fill(), &tm, pb, pe).failed();
1026 os.setstate(std::ios_base::failbit | std::ios_base::badbit);
1036 template <class CharT, class InputIterator>
1037 minutes extract_z(InputIterator& b, InputIterator e, std::ios_base::iostate& err, const std::ctype<CharT>& ct)
1042 char cn = ct.narrow(*b, 0);
1043 if (cn != '+' && cn != '-')
1045 err |= std::ios_base::failbit;
1048 int sn = cn == '-' ? -1 : 1;
1050 for (int i = 0; i < 2; ++i)
1054 err |= std::ios_base::eofbit | std::ios_base::failbit;
1057 cn = ct.narrow(*b, 0);
1058 if (! ('0' <= cn && cn <= '9'))
1060 err |= std::ios_base::failbit;
1063 hr = hr * 10 + cn - '0';
1065 for (int i = 0; i < 2; ++i)
1069 err |= std::ios_base::eofbit | std::ios_base::failbit;
1072 cn = ct.narrow(*b, 0);
1073 if (! ('0' <= cn && cn <= '9'))
1075 err |= std::ios_base::failbit;
1078 min = min * 10 + cn - '0';
1081 err |= std::ios_base::eofbit;
1088 err |= std::ios_base::eofbit | std::ios_base::failbit;
1090 return minutes(min);
1095 #if defined BOOST_CHRONO_PROVIDES_DATE_IO_FOR_SYSTEM_CLOCK_TIME_POINT
1097 template <class CharT, class Traits, class Duration>
1098 std::basic_istream<CharT, Traits>&
1099 operator>>(std::basic_istream<CharT, Traits>& is, time_point<system_clock, Duration>& tp)
1101 typename std::basic_istream<CharT, Traits>::sentry ok(is);
1104 std::ios_base::iostate err = std::ios_base::goodbit;
1107 const CharT* pb = 0; //nullptr;
1108 const CharT* pe = pb;
1109 std::basic_string<CharT> fmt = get_time_fmt<CharT> (is);
1111 pe = pb + fmt.size();
1113 timezone tz = get_timezone(is);
1114 std::locale loc = is.getloc();
1115 const std::time_get<CharT>& tg = std::use_facet<std::time_get<CharT> >(loc);
1116 const std::ctype<CharT>& ct = std::use_facet<std::ctype<CharT> >(loc);
1118 std::memset(&tm, 0, sizeof(std::tm));
1120 typedef std::istreambuf_iterator<CharT, Traits> It;
1124 { '%', 'Y', '-', '%', 'm', '-', '%', 'd', ' ', '%', 'H', ':', '%', 'M', ':' };
1126 pe = pb + sizeof (pattern) / sizeof(CharT);
1128 #if defined BOOST_CHRONO_USES_INTERNAL_TIME_GET
1129 const detail::time_get<CharT>& dtg(tg);
1130 dtg.get(is, 0, is, err, &tm, pb, pe);
1132 tg.get(is, 0, is, err, &tm, pb, pe);
1134 if (err & std::ios_base::failbit) goto exit;
1135 fractional_seconds sec;
1137 std::ios::fmtflags flgs = is.flags();
1138 is.setf(std::ios::fixed, std::ios::floatfield);
1144 err |= std::ios_base::failbit;
1150 if (++i == eof || c != ' ')
1152 err |= std::ios_base::failbit;
1155 minutes min = detail::extract_z(i, eof, err, ct);
1157 if (err & std::ios_base::failbit) goto exit;
1160 #if BOOST_CHRONO_INTERNAL_TIMEGM
1161 t = detail::internal_timegm(&tm);
1165 tp = time_point_cast<Duration>(
1166 system_clock::from_time_t(t) - min + round<system_clock::duration> (duration<fractional_seconds> (sec))
1173 const CharT* fz = std::search(pb, pe, z, z + 2);
1174 #if defined BOOST_CHRONO_USES_INTERNAL_TIME_GET
1175 const detail::time_get<CharT>& dtg(tg);
1176 dtg.get(is, 0, is, err, &tm, pb, fz);
1178 tg.get(is, 0, is, err, &tm, pb, fz);
1183 if (err != std::ios_base::goodbit)
1185 err |= std::ios_base::failbit;
1190 minu = detail::extract_z(i, eof, err, ct);
1191 if (err & std::ios_base::failbit) goto exit;
1194 if (err != std::ios_base::goodbit)
1196 err |= std::ios_base::failbit;
1199 #if defined BOOST_CHRONO_USES_INTERNAL_TIME_GET
1200 const detail::time_get<CharT>& dtg(tg);
1201 dtg.get(is, 0, is, err, &tm, fz + 2, pe);
1203 tg.get(is, 0, is, err, &tm, fz + 2, pe);
1205 if (err & std::ios_base::failbit) goto exit;
1210 if (tz == timezone::utc || fz != pe)
1212 #if BOOST_CHRONO_INTERNAL_TIMEGM
1213 t = detail::internal_timegm(&tm);
1222 tp = time_point_cast<Duration>(
1223 system_clock::from_time_t(t) - minu
1229 err |= std::ios_base::badbit | std::ios_base::failbit;
1232 exit: is.setstate(err);