# # # add_file "dates.cc" # content [30bf4ede576ebc995553f8d464c33dd5f1f97227] # # add_file "dates.hh" # content [b9a3db81cbf8cccada51b1e337a60fbe30ae3d71] # # patch "HACKING" # from [c91db8a3f5c1fe57d6222cc0e24166dd12af3ea3] # to [156e40439898b2df8d1bfe980bbf63a9e19eac35] # # patch "Makefile.am" # from [097be60ed525c9598823d473b3812ba31e9b8d9d] # to [e94f2fecc60c89ec62340e044509dbdbe712b984] # # patch "annotate.cc" # from [ca0a82901ab666554fca69edd1cdb7776d77e3a7] # to [a152991b3936bd8b49e9392fd908e882a7c13c4b] # # patch "app_state.hh" # from [3d931fb6e471179233467cb29c86b4e0feccf7a5] # to [b556393474ffd1133a0d72c549e5ef4b0346d05f] # # patch "asciik.cc" # from [fb51227877ee84eeb285fc28a905a21830ac5b81] # to [04b5836b96ca7345a12f074373537b10f252e6a2] # # patch "asciik.hh" # from [07def1afcb4982caa97392fa9e16a4d82513819d] # to [d6dd7b177951094276a62ed2ee144de738c5a078] # # patch "cert.cc" # from [d1e372169720b4019e26e30f735d724205228027] # to [e25182cb2678de3798544a8112f95f10609cb67f] # # patch "cert.hh" # from [b20ae36eddabf64a73a7c90d00cfce219a880a42] # to [5d8777127b4bf1162d1f82d418e993195624ea68] # # patch "cmd_automate.cc" # from [4b0adb50546986e09c334e1da05e7c48bcba330d] # to [5ed92bb941b4c560f73a7c5b86899235e40b3f5c] # # patch "cmd_db.cc" # from [590012d40b93eac9677904fde3bb2c3717ba565d] # to [ee7051945ff187140d9890ac10fd8c84db6171b8] # # patch "cmd_diff_log.cc" # from [551ef105ac68c023c41e4cc942627827b3aa636f] # to [3cbde32f82993189490ae13350e2f8fccaf1413d] # # patch "cmd_files.cc" # from [4e72e60135d0b3771e0cd71f594b77a060214e1c] # to [0dfcce69efa1db3806309191c49c427428ca5eeb] # # patch "cmd_key_cert.cc" # from [1cb0915c43137a5211e390b869721f9931cbf437] # to [b9034c8d5e997a73555b61e717fd9998a37ad4da] # # patch "cmd_list.cc" # from [0a7d7a729f6a69eafcb274fc669afdb2b1918190] # to [87adfa5ac893ed93b4244093eb1c66052afae20a] # # patch "cmd_merging.cc" # from [5ce276748daf5e39207f2688526d45cb4afd1e53] # to [b38b867f654d55f49b0cf967c921f4180fa9e7d9] # # patch "cmd_packet.cc" # from [7f7e42fa66a1f0594d53372599b332960ab06808] # to [1e64bf637bd28cda2b019b229fadf93db3b206ee] # # patch "cmd_ws_commit.cc" # from [07f9c1998c3ae939b6ef4c2bbbadd363368b0417] # to [a58f8793fc0c9efff8699dd358922f6c7076f6d3] # # patch "commands.cc" # from [15a40992593942e0a8610be9edd311422903fae4] # to [6a8a053c6c687de7aeb91bb53f5beba7a7358c33] # # patch "configure.ac" # from [1ce1a2dcd30e16a040d2599b4b119b692e4ed9fd] # to [a25c3ddbcfadba2cad2219c840f3e8b30d12e8e4] # # patch "debian/control" # from [5b79d4f0d23b1d89cd5ccdd40bad697b7892d1bc] # to [21d7dcfbd3d9fe58481c43d44ac93e5ff57bd911] # # patch "keys.cc" # from [67eff829bfae24d2d204fff785594e89eefe0cdd] # to [02b3c0f70fb1f2a2123351590d5b3c42e9240fa9] # # patch "m4/boost.m4" # from [5fb051cce348601989d147249751d389c45d0b8e] # to [38f52769f29b3e6b8b0581df762f4de7cc42c907] # # patch "monotone.cc" # from [160073e2d0ac102d57db3e761f89ff10b5bfac5d] # to [68b3ece2703bb23d90d2df357b7448ef32a01658] # # patch "options.cc" # from [0a3e04d9f1fe88f510bf6cf00256ba7a512384b6] # to [fdb77217c55e97a3c36428f18a7a54cd82e6b149] # # patch "options.hh" # from [0c9da809849ceed8a68531ef3430de54a64ce9ad] # to [45ee02558ec8b1b5220f6d02dbbcc66965f07e48] # # patch "options_list.hh" # from [f60006e4726e78603263187b6e5eb2cab11987b4] # to [c232a5b2e9e84041361ba6a81689406db3110d39] # # patch "pch.hh" # from [e561236387f43aa08076cda7b35e4af7be163be9] # to [ac7f9f5edebc7ec83eb283e145d88dd6c1f46bad] # # patch "project.cc" # from [bad2c3f0707a4510e2790baaffe6cace0587ebf9] # to [758f4507d87155f3e1fe364a9fd2757bd5018f5b] # # patch "project.hh" # from [e240ddb603a1c6f453816264803987ad655cd2c7] # to [f2f26234c8f87afaf4a0c04524d77cac1142afe4] # # patch "rcs_import.cc" # from [625106ea8f4aa1fbfdcd16df5ba3d088c065b408] # to [93b52aa3c730c65e46c5e62f4566a3f6478c80bb] # # patch "selectors.cc" # from [89c9ef4a38bbad56a692229b389d698e34426aef] # to [d56d5fcd51a1f321224be53036bc52e18d536061] # ============================================================ --- dates.cc 30bf4ede576ebc995553f8d464c33dd5f1f97227 +++ dates.cc 30bf4ede576ebc995553f8d464c33dd5f1f97227 @@ -0,0 +1,568 @@ +// Copyright (C) 2007 Zack Weinberg +// +// This program is made available under the GNU GPL version 2.0 or +// greater. See the accompanying file COPYING for details. +// +// This program is distributed WITHOUT ANY WARRANTY; without even the +// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. + +#include "dates.hh" + +#include +#include +#include + +using std::string; + +// Writing a 64-bit constant is tricky. We cannot use the macros that +// provides in C99 (UINT64_C, or even UINT64_MAX) because those +// macros are not in C++'s version of . std::numeric_limits +// cannot be used directly, so we have to resort to #ifdef chains on the old +// skool C limits macros. BOOST_STATIC_ASSERT is defined in a way that +// doesn't let us use std::numeric_limits::max(), so we have to +// postpone checking it until runtime (date_t::from_unix_epoch), bleah. +// However, the check will be optimized out, and the unit tests exercise it. +#if defined ULONG_MAX && ULONG_MAX > UINT_MAX + #define PROBABLE_U64_MAX ULONG_MAX + #define u64_C(x) x##UL +#elif defined ULLONG_MAX && ULLONG_MAX > UINT_MAX + #define PROBABLE_U64_MAX ULLONG_MAX + #define u64_C(x) x##ULL +#elif defined ULONG_LONG_MAX && ULONG_LONG_MAX > UINT_MAX + #define PROBABLE_U64_MAX ULONG_LONG_MAX + #define u64_C(x) x##ULL +#else + #error "How do I write a constant of type u64?" +#endif + +const string & +date_t::as_iso_8601_extended() const +{ + I(this->valid()); + return d; +} + +std::ostream & +operator<< (std::ostream & o, date_t const & d) +{ + return o << d.as_iso_8601_extended(); +} + +template <> void +dump(date_t const & d, std::string & s) +{ + s = d.as_iso_8601_extended(); +} + +date_t +date_t::now() +{ + using std::time_t; + using std::time; + using std::tm; + using std::gmtime; + using std::strftime; + + time_t t = time(0); + struct tm b = *gmtime(&t); + + // in CE 10000, you will need to increase the size of 'buf'. + I(b.tm_year <= 9999); + + char buf[20]; + strftime(buf, sizeof buf, "%Y-%m-%dT%H:%M:%S", &b); + return date_t(string(buf)); +} + +// The Unix epoch is 1970-01-01T00:00:00 (in UTC). As we cannot safely +// assume that the system's epoch is the Unix epoch, we implement the +// conversion to broken-down time by hand instead of relying on gmtime(). +// The algorithm below has been tested on one value from every day in the +// range [1970-01-01T00:00:00, 36812-02-20T00:36:16) -- that is, [0, 2**40). +// +// Unix time_t values are a linear count of seconds since the epoch, +// and should be interpreted according to the Gregorian calendar: +// +// - There are 60 seconds in a minute, 3600 seconds in an hour, +// 86400 seconds in a day. +// - Years not divisible by 4 have 365 days, or 31536000 seconds. +// - Years divisible by 4 have 366 days, or 31622400 seconds, except ... +// - Years divisible by 100 have only 365 days, except ... +// - Years divisible by 400 have 366 days. +// +// The last two rules are the Gregorian correction to the Julian calendar. +// We make no attempt to handle leap seconds. + +unsigned int const MIN = 60; +unsigned int const HOUR = MIN * 60; +unsigned int const DAY = HOUR * 24; +unsigned int const YEAR = DAY * 365; +unsigned int const LEAP = DAY * 366; + +unsigned char const MONTHS[] = { + 31, // jan + 28, // feb (non-leap) + 31, // mar + 30, // apr + 31, // may + 30, // jun + 31, // jul + 31, // aug + 30, // sep + 31, // oct + 30, // nov + 31, // dec +}; + + +inline bool +is_leap_year(unsigned int year) +{ + return (year % 4 == 0 + && (year % 100 != 0 || year % 400 == 0)); +} +inline u32 +secs_in_year(unsigned int year) +{ + return is_leap_year(year) ? LEAP : YEAR; +} + +date_t +date_t::from_unix_epoch(u64 t) +{ + // these types hint to the compiler that narrowing divides are safe + u64 yearbeg; + u32 year; + u32 month; + u32 day; + u32 secofday; + u16 hour; + u16 secofhour; + u8 min; + u8 sec; + + // validate our assumptions about which basic type is u64 (see above). + I(PROBABLE_U64_MAX == std::numeric_limits::max()); + + // time_t values after this point will overflow a signed 32-bit year + // counter. 'year' above is unsigned, but the system's struct tm almost + // certainly uses a signed tm_year; it is best to be consistent. + I(t <= u64_C(67767976233532799)); + + // There are 31556952 seconds (365d 5h 43m 12s) in the average Gregorian + // year. This will therefore approximate the correct year (minus 1970). + // It may be off in either direction, but by no more than one year + // (empirically tested for every year from 1970 to 2**32 - 1). + year = t/31556952; + + // Given the above approximation, recalculate the _exact_ number of + // seconds to the beginning of that year. For this to work correctly + // (i.e. for the year/4, year/100, year/400 terms to increment exactly + // when they ought to) it is necessary to count years from 1601 (as if the + // Gregorian calendar had been in effect at that time) and then correct + // the final number of seconds back to the 1970 epoch. + year += 369; + + yearbeg = (widen(year)*365 + year/4 - year/100 + year/400)*DAY; + yearbeg -= (widen(369)*365 + 369/4 - 369/100 + 369/400)*DAY; + + // *now* we want year to have its true value. + year += 1601; + + // Linear search for the range of seconds that really contains t. + // At most one of these loops should iterate, and only once. + while (yearbeg > t) + yearbeg -= secs_in_year(--year); + while (yearbeg + secs_in_year(year) <= t) + yearbeg += secs_in_year(year++); + + t -= yearbeg; + + // Now, the months digit! + month = 0; + for (;;) + { + unsigned int this_month = MONTHS[month] * DAY; + if (month == 1 && is_leap_year(year)) + this_month += DAY; + if (t < this_month) + break; + + t -= this_month; + month++; + L(FL("from_unix_epoch: month >= %u, t now %llu") % month % t); + I(month < 12); + } + + // the rest is straightforward. + day = t / DAY; + secofday = t % DAY; + + hour = secofday / HOUR; + secofhour = secofday % HOUR; + + min = secofhour / MIN; + sec = secofhour % MIN; + + // the widen<>s here are necessary because boost::format *ignores the + // format specification* and prints u8s as characters. + return date_t((FL("%u-%02u-%02uT%02u:%02u:%02u") + % year % (month + 1) % (day + 1) + % hour % widen(min) % widen(sec)).str()); +} + +// We might want to consider teaching this routine more time formats. +// gnulib has a rather nice date parser, except that it requires Bison +// (not even just yacc). + +date_t +date_t::from_string(string const & s) +{ + try + { + string d = s; + size_t i = d.size() - 1; // last character of the array + + // seconds + N(d.at(i) >= '0' && d.at(i) <= '9' + && d.at(i-1) >= '0' && d.at(i-1) <= '5', + F("unrecognized date (monotone only understands ISO 8601 format)")); + i -= 2; + + // optional colon + if (d.at(i) == ':') + i--; + else + d.insert(i+1, 1, ':'); + + // minutes + N(d.at(i) >= '0' && d.at(i) <= '9' + && d.at(i-1) >= '0' && d.at(i-1) <= '5', + F("unrecognized date (monotone only understands ISO 8601 format)")); + i -= 2; + + // optional colon + if (d.at(i) == ':') + i--; + else + d.insert(i+1, 1, ':'); + + // hours + N((d.at(i-1) >= '0' && d.at(i-1) <= '1' + && d.at(i) >= '0' && d.at(i) <= '9') + || (d.at(i-1) == '2' && d.at(i) >= '0' && d.at(i) <= '3'), + F("unrecognized date (monotone only understands ISO 8601 format)")); + i -= 2; + + // 'T' is required at this point; we also accept a space + N(d.at(i) == 'T' || d.at(i) == ' ', + F("unrecognized date (monotone only understands ISO 8601 format)")); + + if (d.at(i) == ' ') + d.at(i) = 'T'; + i--; + + // day + u8 day; + N(d.at(i-1) >= '0' && d.at(i-1) <= '3' + && d.at(i) >= '0' && d.at(i) <= '9', + F("unrecognized date (monotone only understands ISO 8601 format)")); + day = (d.at(i-1) - '0')*10 + (d.at(i) - '0'); + i -= 2; + + // optional dash + if (d.at(i) == '-') + i--; + else + d.insert(i+1, 1, '-'); + + // month + u8 month; + N(d.at(i-1) >= '0' && d.at(i-1) <= '1' + && d.at(i) >= '0' && d.at(i) <= '9', + F("unrecognized date (monotone only understands ISO 8601 format)")); + month = (d.at(i-1) - '0')*10 + (d.at(i) - '0'); + N(month >= 1 && month <= 12, + F("month out of range in '%s'") % d); + i -= 2; + + // optional dash + if (d.at(i) == '-') + i--; + else + d.insert(i+1, 1, '-'); + + // year + N(i >= 3, + F("unrecognized date (monotone only understands ISO 8601 format)")); + + // this counts down through zero and stops when it wraps around + // (size_t being unsigned) + u32 year = 0; + u32 digit = 1; + while (i < d.size()) + { + N(d.at(i) >= '0' && d.at(i) <= '9', + F("unrecognized date (monotone only understands ISO 8601 format)")); + year += (d.at(i) - '0')*digit; + i--; + digit *= 10; + } + + N(year >= 1970, + F("date too early (monotone only goes back to 1970-01-01T00:00:00)")); + + u8 mdays; + if (month == 2 && is_leap_year(year)) + mdays = MONTHS[month-1] + 1; + else + mdays = MONTHS[month-1]; + + N(day >= 1 && day <= mdays, + F("day out of range for its month in '%s'") % d); + + return date_t(d); + } + catch (std::out_of_range) + { + N(false, + F("unrecognized date (monotone only understands ISO 8601 format)")); + } +} + +#ifdef BUILD_UNIT_TESTS +#include "unit_tests.hh" + +UNIT_TEST(date, from_string) +{ +#define OK(x,y) BOOST_CHECK(date_t::from_string(x).as_iso_8601_extended() \ + == (y)) +#define NO(x) BOOST_CHECK_THROW(date_t::from_string(x), informative_failure) + + // canonical format + OK("2007-03-01T18:41:13", "2007-03-01T18:41:13"); + // squashed format + OK("20070301T184113", "2007-03-01T18:41:13"); + // space between date and time + OK("2007-03-01 18:41:13", "2007-03-01T18:41:13"); + // squashed, space + OK("20070301 184113", "2007-03-01T18:41:13"); + // more than four digits in the year + OK("120070301T184113", "12007-03-01T18:41:13"); + + // inappropriate character at every possible position + NO("x007-03-01T18:41:13"); + NO("2x07-03-01T18:41:13"); + NO("20x7-03-01T18:41:13"); + NO("200x-03-01T18:41:13"); + NO("2007x03-01T18:41:13"); + NO("2007-x3-01T18:41:13"); + NO("2007-0x-01T18:41:13"); + NO("2007-03x01T18:41:13"); + NO("2007-03-x1T18:41:13"); + NO("2007-03-0xT18:41:13"); + NO("2007-03-01x18:41:13"); + NO("2007-03-01Tx8:41:13"); + NO("2007-03-01T1x:41:13"); + NO("2007-03-01T18x41:13"); + NO("2007-03-01T18:x1:13"); + NO("2007-03-01T18:4x:13"); + NO("2007-03-01T18:41x13"); + NO("2007-03-01T18:41:x3"); + NO("2007-03-01T18:41:1x"); + + NO("x0070301T184113"); + NO("2x070301T184113"); + NO("20x70301T184113"); + NO("200x0301T184113"); + NO("2007x301T184113"); + NO("20070x01T184113"); + NO("200703x1T184113"); + NO("2007030xT184113"); + NO("20070301x184113"); + NO("20070301Tx84113"); + NO("20070301T1x4113"); + NO("20070301T18x113"); + NO("20070301T184x13"); + NO("20070301T1841x3"); + NO("20070301T18411x"); + + // two digit years are not accepted + NO("07-03-01T18:41:13"); + + // components out of range + NO("1969-03-01T18:41:13"); + + NO("2007-00-01T18:41:13"); + NO("2007-13-01T18:41:13"); + + NO("2007-01-00T18:41:13"); + NO("2007-01-32T18:41:13"); + NO("2007-02-29T18:41:13"); + NO("2007-03-32T18:41:13"); + NO("2007-04-31T18:41:13"); + NO("2007-05-32T18:41:13"); + NO("2007-06-31T18:41:13"); + NO("2007-07-32T18:41:13"); + NO("2007-08-32T18:41:13"); + NO("2007-09-31T18:41:13"); + NO("2007-10-32T18:41:13"); + NO("2007-11-31T18:41:13"); + NO("2007-03-32T18:41:13"); + + NO("2007-03-01T24:41:13"); + NO("2007-03-01T18:60:13"); + NO("2007-03-01T18:41:60"); + + // leap year February + OK("2008-02-29T18:41:13", "2008-02-29T18:41:13"); + NO("2008-02-30T18:41:13"); + + // maybe we should support these, but we don't + NO("2007-03-01"); + NO("18:41"); + NO("18:41:13"); + NO("Thu Mar 1 18:41:13 PST 2007"); + NO("Thu, 01 Mar 2007 18:47:22"); + NO("Thu, 01 Mar 2007 18:47:22 -0800"); + NO("torsdag, mars 01, 2007, 18.50.10"); + // et cetera +#undef OK +#undef NO +} + +UNIT_TEST(date, from_unix_epoch) +{ +#define OK(x,y) do { \ + string s_ = date_t::from_unix_epoch(x).as_iso_8601_extended(); \ + L(FL("from_unix_epoch: %lu -> %s") % (x) % s_); \ + BOOST_CHECK(s_ == (y)); \ + } while (0) + + // every month boundary in 1970 + OK(0, "1970-01-01T00:00:00"); + OK(2678399, "1970-01-31T23:59:59"); + OK(2678400, "1970-02-01T00:00:00"); + OK(5097599, "1970-02-28T23:59:59"); + OK(5097600, "1970-03-01T00:00:00"); + OK(7775999, "1970-03-31T23:59:59"); + OK(7776000, "1970-04-01T00:00:00"); + OK(10367999, "1970-04-30T23:59:59"); + OK(10368000, "1970-05-01T00:00:00"); + OK(13046399, "1970-05-31T23:59:59"); + OK(13046400, "1970-06-01T00:00:00"); + OK(15638399, "1970-06-30T23:59:59"); + OK(15638400, "1970-07-01T00:00:00"); + OK(18316799, "1970-07-31T23:59:59"); + OK(18316800, "1970-08-01T00:00:00"); + OK(20995199, "1970-08-31T23:59:59"); + OK(20995200, "1970-09-01T00:00:00"); + OK(23587199, "1970-09-30T23:59:59"); + OK(23587200, "1970-10-01T00:00:00"); + OK(26265599, "1970-10-31T23:59:59"); + OK(26265600, "1970-11-01T00:00:00"); + OK(28857599, "1970-11-30T23:59:59"); + OK(28857600, "1970-12-01T00:00:00"); + OK(31535999, "1970-12-31T23:59:59"); + OK(31536000, "1971-01-01T00:00:00"); + + // every month boundary in 1972 (an ordinary leap year) + OK(63071999, "1971-12-31T23:59:59"); + OK(63072000, "1972-01-01T00:00:00"); + OK(65750399, "1972-01-31T23:59:59"); + OK(65750400, "1972-02-01T00:00:00"); + OK(68255999, "1972-02-29T23:59:59"); + OK(68256000, "1972-03-01T00:00:00"); + OK(70934399, "1972-03-31T23:59:59"); + OK(70934400, "1972-04-01T00:00:00"); + OK(73526399, "1972-04-30T23:59:59"); + OK(73526400, "1972-05-01T00:00:00"); + OK(76204799, "1972-05-31T23:59:59"); + OK(76204800, "1972-06-01T00:00:00"); + OK(78796799, "1972-06-30T23:59:59"); + OK(78796800, "1972-07-01T00:00:00"); + OK(81475199, "1972-07-31T23:59:59"); + OK(81475200, "1972-08-01T00:00:00"); + OK(84153599, "1972-08-31T23:59:59"); + OK(84153600, "1972-09-01T00:00:00"); + OK(86745599, "1972-09-30T23:59:59"); + OK(86745600, "1972-10-01T00:00:00"); + OK(89423999, "1972-10-31T23:59:59"); + OK(89424000, "1972-11-01T00:00:00"); + OK(92015999, "1972-11-30T23:59:59"); + OK(92016000, "1972-12-01T00:00:00"); + OK(94694399, "1972-12-31T23:59:59"); + OK(94694400, "1973-01-01T00:00:00"); + + // every month boundary in 2000 (a leap year per rule 5) + OK(946684799, "1999-12-31T23:59:59"); + OK(946684800, "2000-01-01T00:00:00"); + OK(949363199, "2000-01-31T23:59:59"); + OK(949363200, "2000-02-01T00:00:00"); + OK(951868799, "2000-02-29T23:59:59"); + OK(951868800, "2000-03-01T00:00:00"); + OK(954547199, "2000-03-31T23:59:59"); + OK(954547200, "2000-04-01T00:00:00"); + OK(957139199, "2000-04-30T23:59:59"); + OK(957139200, "2000-05-01T00:00:00"); + OK(959817599, "2000-05-31T23:59:59"); + OK(959817600, "2000-06-01T00:00:00"); + OK(962409599, "2000-06-30T23:59:59"); + OK(962409600, "2000-07-01T00:00:00"); + OK(965087999, "2000-07-31T23:59:59"); + OK(965088000, "2000-08-01T00:00:00"); + OK(967766399, "2000-08-31T23:59:59"); + OK(967766400, "2000-09-01T00:00:00"); + OK(970358399, "2000-09-30T23:59:59"); + OK(970358400, "2000-10-01T00:00:00"); + OK(973036799, "2000-10-31T23:59:59"); + OK(973036800, "2000-11-01T00:00:00"); + OK(975628799, "2000-11-30T23:59:59"); + OK(975628800, "2000-12-01T00:00:00"); + OK(978307199, "2000-12-31T23:59:59"); + OK(978307200, "2001-01-01T00:00:00"); + + // every month boundary in 2100 (a normal year per rule 4) + OK(u64_C(4102444800), "2100-01-01T00:00:00"); + OK(u64_C(4105123199), "2100-01-31T23:59:59"); + OK(u64_C(4105123200), "2100-02-01T00:00:00"); + OK(u64_C(4107542399), "2100-02-28T23:59:59"); + OK(u64_C(4107542400), "2100-03-01T00:00:00"); + OK(u64_C(4110220799), "2100-03-31T23:59:59"); + OK(u64_C(4110220800), "2100-04-01T00:00:00"); + OK(u64_C(4112812799), "2100-04-30T23:59:59"); + OK(u64_C(4112812800), "2100-05-01T00:00:00"); + OK(u64_C(4115491199), "2100-05-31T23:59:59"); + OK(u64_C(4115491200), "2100-06-01T00:00:00"); + OK(u64_C(4118083199), "2100-06-30T23:59:59"); + OK(u64_C(4118083200), "2100-07-01T00:00:00"); + OK(u64_C(4120761599), "2100-07-31T23:59:59"); + OK(u64_C(4120761600), "2100-08-01T00:00:00"); + OK(u64_C(4123439999), "2100-08-31T23:59:59"); + OK(u64_C(4123440000), "2100-09-01T00:00:00"); + OK(u64_C(4126031999), "2100-09-30T23:59:59"); + OK(u64_C(4126032000), "2100-10-01T00:00:00"); + OK(u64_C(4128710399), "2100-10-31T23:59:59"); + OK(u64_C(4128710400), "2100-11-01T00:00:00"); + OK(u64_C(4131302399), "2100-11-30T23:59:59"); + OK(u64_C(4131302400), "2100-12-01T00:00:00"); + OK(u64_C(4133980799), "2100-12-31T23:59:59"); + + // limit of a (signed) 32-bit year counter + OK(u64_C(67767976233532799), "2147483647-12-31T23:59:59"); + BOOST_CHECK_THROW(date_t::from_unix_epoch(u64_C(67768036191676800)), + std::logic_error); + +#undef OK +} + +#endif + +// Local Variables: +// mode: C++ +// fill-column: 76 +// c-file-style: "gnu" +// indent-tabs-mode: nil +// End: +// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s: ============================================================ --- dates.hh b9a3db81cbf8cccada51b1e337a60fbe30ae3d71 +++ dates.hh b9a3db81cbf8cccada51b1e337a60fbe30ae3d71 @@ -0,0 +1,62 @@ +// Copyright (C) 2007 Zack Weinberg +// +// This program is made available under the GNU GPL version 2.0 or +// greater. See the accompanying file COPYING for details. +// +// This program is distributed WITHOUT ANY WARRANTY; without even the +// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. + +#ifndef DATES_HH +#define DATES_HH + +// This file provides a straightforward wrapper class around the standard +// C time functions. Note that all operations are done in UTC, *not* the +// user's time zone. + +#include "numeric_vocab.hh" +#include "sanity.hh" +#include +#include + +struct date_t +{ + // For the benefit of the --date option. + date_t() : d("") {} + bool valid() const { return d != ""; } + + // Return the local system's idea of the current date. + static date_t now(); + + // Return the date corresponding to an unsigned 64-bit count of seconds + // since the Unix epoch (1970-01-01T00:00:00). + static date_t from_unix_epoch(u64); + + // Return the date corresponding to a string. Presently this recognizes + // only ISO 8601 "basic" and "extended" time formats. + static date_t from_string(std::string const &); + + // Write out date as a string. + std::string const & as_iso_8601_extended() const; + +private: + // For what we do with dates, it is most convenient to store them as + // strings in the ISO 8601 extended time format. + std::string d; + + // used by the above factory functions + date_t(std::string const & s) : d(s) {}; +}; + +std::ostream & operator<< (std::ostream & o, date_t const & d); +template <> void dump(date_t const & d, std::string & s); + +#endif // dates.hh + +// Local Variables: +// mode: C++ +// fill-column: 76 +// c-file-style: "gnu" +// indent-tabs-mode: nil +// End: +// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s: ============================================================ --- HACKING c91db8a3f5c1fe57d6222cc0e24166dd12af3ea3 +++ HACKING 156e40439898b2df8d1bfe980bbf63a9e19eac35 @@ -128,7 +128,8 @@ continued to adhere to the dialect we wr standard streams are initialized before their first use. therefore, do not include unless you actually refer to one of the standard streams (cin, cout, cerr, clog). use , - , , , etc instead, as appropriate. + , , , etc instead, as appropriate. do not, + under any circumstances, refer to the standard streams in a header file. - it is almost always a mistake to use std::endl instead of '\n'. std::endl writes a newline to the stream *and flushes it*. ============================================================ --- Makefile.am 097be60ed525c9598823d473b3812ba31e9b8d9d +++ Makefile.am e94f2fecc60c89ec62340e044509dbdbe712b984 @@ -37,7 +37,7 @@ MOST_SOURCES = \ outdated_indicator.cc outdated_indicator.hh \ database.cc database.hh \ key_store.cc key_store.hh \ - ssh_agent.cc ssh_agent.hh \ + ssh_agent.cc ssh_agent.hh \ keys.cc keys.hh \ packet.cc packet.hh \ rcs_file.cc rcs_file.hh \ @@ -72,6 +72,7 @@ MOST_SOURCES = \ sha1.hh sha1.cc sha1_engine.hh \ rev_height.cc rev_height.hh \ asciik.cc asciik.hh \ + dates.cc dates.hh \ \ lru_writeback_cache.hh \ \ ============================================================ --- annotate.cc ca0a82901ab666554fca69edd1cdb7776d77e3a7 +++ annotate.cc a152991b3936bd8b49e9392fd908e882a7c13c4b @@ -9,6 +9,7 @@ #include #include +#include #include #include ============================================================ --- app_state.hh 3d931fb6e471179233467cb29c86b4e0feccf7a5 +++ app_state.hh b556393474ffd1133a0d72c549e5ef4b0346d05f @@ -16,7 +16,6 @@ class lua_hooks; #include #include -#include #include #include "database.hh" ============================================================ --- asciik.cc fb51227877ee84eeb285fc28a905a21830ac5b81 +++ asciik.cc 04b5836b96ca7345a12f074373537b10f252e6a2 @@ -146,7 +146,7 @@ static revision_id ghost; // valid but e static revision_id ghost; // valid but empty revision_id to be used as ghost value -asciik::asciik(size_t min_width, ostream & os) +asciik::asciik(ostream & os, size_t min_width) : width(min_width), output(os) { } @@ -396,7 +396,7 @@ CMD(asciik, N_("debug"), N_("SELECTOR"), selectors::selector_type ty = selectors::sel_ident; selectors::complete_selector("", sels, ty, completions, app); - asciik graph(10); + asciik graph(std::cout, 10); set revs; for (set::const_iterator i = completions.begin(); i != completions.end(); ++i) ============================================================ --- asciik.hh 07def1afcb4982caa97392fa9e16a4d82513819d +++ asciik.hh d6dd7b177951094276a62ed2ee144de738c5a078 @@ -10,7 +10,7 @@ public: class asciik { public: - asciik(size_t min_width = 0, std::ostream & os = std::cout); + asciik(std::ostream & os, size_t min_width = 0); // Prints an ASCII-k chunk using the given revisions. // Multiple lines are supported in annotation (the graph will stretch // accordingly); empty newlines at the end will be removed. ============================================================ --- cert.cc d1e372169720b4019e26e30f735d724205228027 +++ cert.cc e25182cb2678de3798544a8112f95f10609cb67f @@ -12,7 +12,6 @@ #include #include -#include #include #include #include @@ -562,36 +561,15 @@ cert_revision_date_time(revision_id cons void cert_revision_date_time(revision_id const & m, - boost::posix_time::ptime t, + date_t const & t, app_state & app, packet_consumer & pc) { - cert_value val = cert_value(boost::posix_time::to_iso_extended_string(t)); + cert_value val = cert_value(t.as_iso_8601_extended()); put_simple_revision_cert(m, cert_name(date_cert_name), val, app, pc); } void -cert_revision_date_time(revision_id const & m, - time_t t, - app_state & app, - packet_consumer & pc) -{ - // make sure you do all your CVS conversions by 2038! - boost::posix_time::ptime tmp(boost::gregorian::date(1970,1,1), - boost::posix_time::seconds(static_cast(t))); - cert_revision_date_time(m, tmp, app, pc); -} - -void -cert_revision_date_now(revision_id const & m, - app_state & app, - packet_consumer & pc) -{ - cert_revision_date_time - (m, boost::posix_time::second_clock::universal_time(), app, pc); -} - -void cert_revision_author(revision_id const & m, string const & author, app_state & app, ============================================================ --- cert.hh b20ae36eddabf64a73a7c90d00cfce219a880a42 +++ cert.hh 5d8777127b4bf1162d1f82d418e993195624ea68 @@ -14,10 +14,8 @@ #include #include -#include -#include - #include "vocab.hh" +#include "dates.hh" // Certs associate an opaque name/value pair with a revision ID, and // are accompanied by an RSA public-key signature attesting to the @@ -123,23 +121,12 @@ void extern cert_name const testresult_cert_name; void -cert_revision_date_now(revision_id const & m, - app_state & app, - packet_consumer & pc); - -void cert_revision_date_time(revision_id const & m, - boost::posix_time::ptime t, + date_t const & t, app_state & app, packet_consumer & pc); void -cert_revision_date_time(revision_id const & m, - time_t time, - app_state & app, - packet_consumer & pc); - -void cert_revision_author(revision_id const & m, std::string const & author, app_state & app, ============================================================ --- cmd_automate.cc 4b0adb50546986e09c334e1da05e7c48bcba330d +++ cmd_automate.cc 5ed92bb941b4c560f73a7c5b86899235e40b3f5c @@ -7,7 +7,7 @@ // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // PURPOSE. -#include +#include #include #include "cmd.hh" ============================================================ --- cmd_db.cc 590012d40b93eac9677904fde3bb2c3717ba565d +++ cmd_db.cc ee7051945ff187140d9890ac10fd8c84db6171b8 @@ -7,6 +7,7 @@ // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // PURPOSE. +#include #include #include "charset.hh" ============================================================ --- cmd_diff_log.cc 551ef105ac68c023c41e4cc942627827b3aa636f +++ cmd_diff_log.cc 3cbde32f82993189490ae13350e2f8fccaf1413d @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -746,7 +747,8 @@ CMD(log, N_("informative"), N_("[FILE] . set seen; revision_t rev; - asciik graph; // it's instantiated even when not used, but it's lightweight + // this is instantiated even when not used, but it's lightweight + asciik graph(cout); while(! frontier.empty() && (last == -1 || last > 0) && (next == -1 || next > 0)) { ============================================================ --- cmd_files.cc 4e72e60135d0b3771e0cd71f594b77a060214e1c +++ cmd_files.cc 0dfcce69efa1db3806309191c49c427428ca5eeb @@ -7,6 +7,7 @@ // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // PURPOSE. +#include #include "annotate.hh" #include "cmd.hh" ============================================================ --- cmd_key_cert.cc 1cb0915c43137a5211e390b869721f9931cbf437 +++ cmd_key_cert.cc b9034c8d5e997a73555b61e717fd9998a37ad4da @@ -7,6 +7,7 @@ // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // PURPOSE. +#include #include #include ============================================================ --- cmd_list.cc 0a7d7a729f6a69eafcb274fc669afdb2b1918190 +++ cmd_list.cc 87adfa5ac893ed93b4244093eb1c66052afae20a @@ -10,6 +10,7 @@ #include #include #include +#include #include ============================================================ --- cmd_merging.cc 5ce276748daf5e39207f2688526d45cb4afd1e53 +++ cmd_merging.cc b38b867f654d55f49b0cf967c921f4180fa9e7d9 @@ -8,6 +8,8 @@ // PURPOSE. #include +#include +#include #include "cmd.hh" #include "diff_patch.hh" ============================================================ --- cmd_packet.cc 7f7e42fa66a1f0594d53372599b332960ab06808 +++ cmd_packet.cc 1e64bf637bd28cda2b019b229fadf93db3b206ee @@ -7,6 +7,7 @@ // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // PURPOSE. +#include #include #include "cmd.hh" ============================================================ --- cmd_ws_commit.cc 07f9c1998c3ae939b6ef4c2bbbadd363368b0417 +++ cmd_ws_commit.cc a58f8793fc0c9efff8699dd358922f6c7076f6d3 @@ -7,6 +7,7 @@ // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // PURPOSE. +#include #include #include "cmd.hh" ============================================================ --- commands.cc 15a40992593942e0a8610be9edd311422903fae4 +++ commands.cc 6a8a053c6c687de7aeb91bb53f5beba7a7358c33 @@ -9,6 +9,7 @@ #include #include +#include #include "transforms.hh" #include "simplestring_xform.hh" ============================================================ --- configure.ac 1ce1a2dcd30e16a040d2599b4b119b692e4ed9fd +++ configure.ac a25c3ddbcfadba2cad2219c840f3e8b30d12e8e4 @@ -127,7 +127,6 @@ MTN_BOOST_LIB_FILESYSTEM BOOST_VERSION_SPECIFIC_BUGS MTN_BOOST_LIB_FILESYSTEM -MTN_BOOST_LIB_DATE_TIME MTN_BOOST_LIB_REGEX MTN_BOOST_LIB_UNIT_TEST_FRAMEWORK ============================================================ --- debian/control 5b79d4f0d23b1d89cd5ccdd40bad697b7892d1bc +++ debian/control 21d7dcfbd3d9fe58481c43d44ac93e5ff57bd911 @@ -1,10 +1,10 @@ Build-Depends: cdbs (>= 0.4.28), debhelp Source: monotone Section: devel Priority: optional Maintainer: Shaun Jackman Build-Depends: cdbs (>= 0.4.28), debhelper (>= 4.0.0), autotools-dev, - libboost-date-time-dev, libboost-filesystem-dev, libboost-regex-dev, - libboost-test-dev, libboost-dev, libz-dev, + libboost-filesystem-dev, libboost-regex-dev, libboost-test-dev, libboost-dev, + libz-dev, Build-Depends-Indep: ps2eps, tetex-bin | texlive-base-bin, texinfo, xpdf-utils Standards-Version: 3.7.2.2 ============================================================ --- keys.cc 67eff829bfae24d2d204fff785594e89eefe0cdd +++ keys.cc 02b3c0f70fb1f2a2123351590d5b3c42e9240fa9 @@ -9,7 +9,7 @@ #include #include - +#include #include #include ============================================================ --- m4/boost.m4 5fb051cce348601989d147249751d389c45d0b8e +++ m4/boost.m4 38f52769f29b3e6b8b0581df762f4de7cc42c907 @@ -173,16 +173,6 @@ AC_DEFUN([MTN_BOOST_LIB_FILESYSTEM], exists(path("/boot")); ]])])]) -AC_DEFUN([MTN_BOOST_LIB_DATE_TIME], -[MTN_NEED_BOOST_LIB([date_time], - [AC_LANG_PROGRAM([[ - #include - #include - using namespace boost::posix_time; - ]],[[ - std::cout << to_iso_extended_string(second_clock::universal_time()); - ]])])]) - AC_DEFUN([MTN_BOOST_LIB_REGEX], [MTN_NEED_BOOST_LIB([regex], [AC_LANG_PROGRAM([[ ============================================================ --- monotone.cc 160073e2d0ac102d57db3e761f89ff10b5bfac5d +++ monotone.cc 68b3ece2703bb23d90d2df357b7448ef32a01658 @@ -10,6 +10,7 @@ #include "config.h" #include +#include #include #include #include ============================================================ --- options.cc 0a3e04d9f1fe88f510bf6cf00256ba7a512384b6 +++ options.cc fdb77217c55e97a3c36428f18a7a54cd82e6b149 @@ -1,9 +1,6 @@ #include -#include -#include - #include "charset.hh" #include "options.hh" #include "platform.hh" ============================================================ --- options.hh 0c9da809849ceed8a68531ef3430de54a64ce9ad +++ options.hh 45ee02558ec8b1b5220f6d02dbbcc66965f07e48 @@ -5,8 +5,8 @@ #include "option.hh" #include "paths.hh" +#include "dates.hh" - struct options { options(); ============================================================ --- options_list.hh f60006e4726e78603263187b6e5eb2cab11987b4 +++ options_list.hh c232a5b2e9e84041361ba6a81689406db3110d39 @@ -118,20 +118,13 @@ GOPT(conf_dir, "confdir", system_path, g } #endif -OPT(date, "date", boost::posix_time::ptime, , +OPT(date, "date", date_t, , gettext_noop("override date/time for commit")) #ifdef option_bodies { try { - // boost::posix_time can parse "basic" ISO times, of the form - // 20000101T120000, but not "extended" ISO times, of the form - // 2000-01-01T12:00:00. So convert one to the other. - string tmp = arg; - string::size_type pos = 0; - while ((pos = tmp.find_first_of("-:")) != string::npos) - tmp.erase(pos, 1); - date = boost::posix_time::from_iso_string(tmp); + date = date_t::from_string(arg); } catch (std::exception &e) { ============================================================ --- pch.hh e561236387f43aa08076cda7b35e4af7be163be9 +++ pch.hh ac7f9f5edebc7ec83eb283e145d88dd6c1f46bad @@ -18,8 +18,6 @@ #include #include #include -#include -#include #include #include #include ============================================================ --- project.cc bad2c3f0707a4510e2790baaffe6cace0587ebf9 +++ project.cc 758f4507d87155f3e1fe364a9fd2757bd5018f5b @@ -10,20 +10,10 @@ #include "revision.hh" #include "transforms.hh" -#include - using std::string; using std::set; using std::vector; -boost::posix_time::ptime time_from_time_t(time_t time) -{ - boost::gregorian::date the_epoch(1970, 1, 1); - boost::posix_time::seconds seconds(static_cast(time)); - return boost::posix_time::ptime(the_epoch, seconds); -} - - project_t::project_t(app_state & app) : app(app) {} @@ -243,7 +233,7 @@ project_t::put_standard_certs(revision_i project_t::put_standard_certs(revision_id const & id, branch_name const & branch, utf8 const & changelog, - boost::posix_time::ptime const & time, + date_t const & time, utf8 const & author, packet_consumer & pc) { @@ -265,7 +255,7 @@ project_t::put_standard_certs_from_optio put_standard_certs(id, branch, changelog, - app.opts.date_given?app.opts.date:now(), + app.opts.date_given ? app.opts.date : date_t::now(), app.opts.author, pc); } ============================================================ --- project.hh e240ddb603a1c6f453816264803987ad655cd2c7 +++ project.hh f2f26234c8f87afaf4a0c04524d77cac1142afe4 @@ -12,9 +12,6 @@ #include "outdated_indicator.hh" #include "vocab.hh" -#include - - class app_state; class packet_consumer; @@ -28,13 +25,6 @@ bool operator < (tag_t const & a, tag_t }; bool operator < (tag_t const & a, tag_t const & b); -inline boost::posix_time::ptime now() -{ - return boost::posix_time::second_clock::universal_time(); -} - -boost::posix_time::ptime time_from_time_t(time_t time); - class project_t { app_state & app; @@ -72,7 +62,7 @@ public: void put_standard_certs(revision_id const & id, branch_name const & branch, utf8 const & changelog, - boost::posix_time::ptime const & time, + date_t const & time, utf8 const & author, packet_consumer & pc); void put_standard_certs_from_options(revision_id const & id, ============================================================ --- rcs_import.cc 625106ea8f4aa1fbfdcd16df5ba3d088c065b408 +++ rcs_import.cc 93b52aa3c730c65e46c5e62f4566a3f6478c80bb @@ -1388,7 +1388,7 @@ cluster_consumer::store_auxiliary_certs( app.get_project().put_standard_certs(p.rid, branch_name(branchname), utf8(cvs.changelog_interner.lookup(p.changelog)), - time_from_time_t(p.time), + date_t::from_unix_epoch(p.time), utf8(cvs.author_interner.lookup(p.author)), dbw); } ============================================================ --- selectors.cc 89c9ef4a38bbad56a692229b389d698e34426aef +++ selectors.cc d56d5fcd51a1f321224be53036bc52e18d536061 @@ -12,6 +12,8 @@ #include "app_state.hh" #include "constants.hh" +#include + using std::make_pair; using std::pair; using std::set;