我在我的項目中使用Boost's datetime library。當我發現它具有數小時,數天,數月,數年等的持續時間類型時,我感到非常高興,並且它們根據您將它們添加到的內容來更改它們的值(即,在日期的月份部分添加1個月預付款,它不只是增加30天或某些)。我認爲這屬性爲日式舉行,但我決定測試它,我將它投入生產之前...boost local_date_time數學錯誤?
local_date_time t1(date(2010, 3, 14), hours(1), easternTime, false); // 1am on DST transition date
{
CPPUNIT_ASSERT_EQUAL(greg_year(2010), t1.local_time().date().year());
CPPUNIT_ASSERT_EQUAL(greg_month(3), t1.local_time().date().month());
CPPUNIT_ASSERT_EQUAL(greg_day(14), t1.local_time().date().day());
CPPUNIT_ASSERT_EQUAL(1L, t1.local_time().time_of_day().hours());
CPPUNIT_ASSERT_EQUAL(0L, t1.local_time().time_of_day().minutes());
CPPUNIT_ASSERT_EQUAL(0L, t1.local_time().time_of_day().seconds());
}
t1 += days(1); // the time in EST should now be 1am on the 15th
{
CPPUNIT_ASSERT_EQUAL(greg_year(2010), t1.local_time().date().year());
CPPUNIT_ASSERT_EQUAL(greg_month(3), t1.local_time().date().month());
CPPUNIT_ASSERT_EQUAL(greg_day(15), t1.local_time().date().day());
CPPUNIT_ASSERT_EQUAL(1L, t1.local_time().time_of_day().hours()); // fails, returns 2
CPPUNIT_ASSERT_EQUAL(0L, t1.local_time().time_of_day().minutes());
CPPUNIT_ASSERT_EQUAL(0L, t1.local_time().time_of_day().seconds());
}
在上面,你會看到我的CppUnit的單元測試。如果days()僅僅增加了24個小時而不是1個邏輯日(因爲DST轉換導致2010-03-14在EST中持續23個小時),那麼它會在指定的行中失敗,這是我所期望的。
我做錯了什麼?這是一個錯誤?我是否完全誤解了圖書館關於這種數學的設計目標?
這是一個很好的例子,說明爲什麼我們應該全部編寫測試用例代碼 - 比較預期的實際值 – slf 2010-02-22 17:18:00