#include "wx/datetime.h"
+// to test Today() meaningfully we must be able to change the system date which
+// is not usually the case, but if we're under Win32 we can try it -- define
+// the macro below to do it
+//#define CHANGE_SYSTEM_DATE
+
+#ifndef __WINDOWS__
+ #undef CHANGE_SYSTEM_DATE
+#endif
+
+#ifdef CHANGE_SYSTEM_DATE
+
+class DateChanger
+{
+public:
+ DateChanger(int year, int month, int day, int hour, int min, int sec)
+ {
+ SYSTEMTIME st;
+ st.wDay = day;
+ st.wMonth = month;
+ st.wYear = year;
+ st.wHour = hour;
+ st.wMinute = min;
+ st.wSecond = sec;
+ st.wMilliseconds = 0;
+
+ ::GetSystemTime(&m_savedTime);
+ ::GetTimeZoneInformation(&m_tzi);
+
+ m_changed = ::SetSystemTime(&st) != 0;
+ }
+
+ ~DateChanger()
+ {
+ if ( m_changed )
+ {
+ ::SetSystemTime(&m_savedTime);
+ ::SetTimeZoneInformation(&m_tzi);
+ }
+ }
+
+private:
+ SYSTEMTIME m_savedTime;
+ TIME_ZONE_INFORMATION m_tzi;
+ bool m_changed;
+};
+
+#endif // CHANGE_SYSTEM_DATE
+
// ----------------------------------------------------------------------------
// broken down date representation used for testing
// ----------------------------------------------------------------------------
CPPUNIT_TEST( TestTimeTicks );
CPPUNIT_TEST( TestTimeParse );
CPPUNIT_TEST( TestTimeArithmetics );
+ CPPUNIT_TEST( TestDSTBug );
CPPUNIT_TEST_SUITE_END();
void TestLeapYears();
void TestTimeTicks();
void TestTimeParse();
void TestTimeArithmetics();
+ void TestDSTBug();
DECLARE_NO_COPY_CLASS(DateTimeTestCase)
};
{
const Date& d = testDates[n];
wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
- double jdn = dt.GetJulianDayNumber();
+
+ // JDNs must be computed for UTC times
+ double jdn = dt.FromUTC().GetJulianDayNumber();
CPPUNIT_ASSERT( jdn == d.jdn );
long ticks = (dt.GetValue() / 1000).ToLong();
CPPUNIT_ASSERT( ticks == d.ticks );
- dt = d.DT().ToTimezone(wxDateTime::GMT0);
+ dt = d.DT().FromTimezone(wxDateTime::GMT0);
ticks = (dt.GetValue() / 1000).ToLong();
CPPUNIT_ASSERT( ticks == d.gmticks );
}
static const struct ParseTestData
{
const wxChar *format;
- Date date;
+ Date date; // NB: this should be in UTC
bool good;
} parseTestDates[] =
{
- { _T("Sat, 18 Dec 1999 00:46:40 +0100"), { 18, wxDateTime::Dec, 1999, 00, 46, 40, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, true },
- { _T("Wed, 1 Dec 1999 05:17:20 +0300"), { 1, wxDateTime::Dec, 1999, 03, 17, 20, 0.0, wxDateTime::Inv_WeekDay, 0, 0 }, true },
+ {
+ _T("Sat, 18 Dec 1999 00:46:40 +0100"),
+ { 17, wxDateTime::Dec, 1999, 23, 46, 40, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
+ true
+ },
+ {
+ _T("Wed, 1 Dec 1999 05:17:20 +0300"),
+ { 1, wxDateTime::Dec, 1999, 2, 17, 20, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
+ true
+ },
+ {
+ _T("Sun, 28 Aug 2005 03:31:30 +0200"),
+ { 28, wxDateTime::Aug, 2005, 1, 31, 30, 0.0, wxDateTime::Inv_WeekDay, 0, 0 },
+ true
+ },
};
for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
{
CPPUNIT_ASSERT( parseTestDates[n].good );
- wxDateTime dtReal = parseTestDates[n].date.DT();
- //RN: We need this because the tests are based on
- //a non-GMT time zone
- dtReal.MakeTimezone(wxDateTime::WEST, true);
+ wxDateTime dtReal = parseTestDates[n].date.DT().FromUTC();
CPPUNIT_ASSERT( dt == dtReal );
}
else // failed to parse
}
}
+void DateTimeTestCase::TestDSTBug()
+{
+ /////////////////////////
+ // Test GetEndDST()
+ wxDateTime dt = wxDateTime::GetEndDST(2004);
+ CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
+ CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
+ CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
+ CPPUNIT_ASSERT_EQUAL(2, (int)dt.GetHour());
+ CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
+ CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
+ CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
+
+ /////////////////////////
+ // Test ResetTime()
+ dt.SetHour(5);
+ CPPUNIT_ASSERT_EQUAL(5, (int)dt.GetHour());
+ dt.ResetTime();
+ CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
+ CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
+ CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
+ CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetHour());
+ CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
+ CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
+ CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
+
+ dt.Set(1, 0, 0, 0);
+ CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
+
+ /////////////////////////
+ // Test Today()
+#ifdef CHANGE_SYSTEM_DATE
+ {
+ DateChanger change(2004, 10, 31, 5, 0, 0);
+ dt = wxDateTime::Today();
+ }
+
+ CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
+ CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
+ CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
+ CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetHour());
+ CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMinute());
+ CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
+ CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
+
+ /////////////////////////
+ // Test Set(hour, minute, second, milli)
+ wxDateTime dt2;
+ {
+ DateChanger change(2004, 10, 31, 5, 0, 0);
+ dt.Set(1, 30, 0, 0);
+ dt2.Set(5, 30, 0, 0);
+ }
+
+ CPPUNIT_ASSERT_EQUAL(31, (int)dt.GetDay());
+ CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt.GetMonth());
+ CPPUNIT_ASSERT_EQUAL(2004, (int)dt.GetYear());
+ CPPUNIT_ASSERT_EQUAL(1, (int)dt.GetHour());
+ CPPUNIT_ASSERT_EQUAL(30, (int)dt.GetMinute());
+ CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetSecond());
+ CPPUNIT_ASSERT_EQUAL(0, (int)dt.GetMillisecond());
+
+ CPPUNIT_ASSERT_EQUAL(31, (int)dt2.GetDay());
+ CPPUNIT_ASSERT_EQUAL(wxDateTime::Oct, dt2.GetMonth());
+ CPPUNIT_ASSERT_EQUAL(2004, (int)dt2.GetYear());
+ CPPUNIT_ASSERT_EQUAL(5, (int)dt2.GetHour());
+ CPPUNIT_ASSERT_EQUAL(30, (int)dt2.GetMinute());
+ CPPUNIT_ASSERT_EQUAL(0, (int)dt2.GetSecond());
+ CPPUNIT_ASSERT_EQUAL(0, (int)dt2.GetMillisecond());
+#endif // CHANGE_SYSTEM_DATE
+}
+