]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/datetime/datetimetest.cpp
define _HPUX_SOURCE under HP-UX, otherwise many things are not defined in standard...
[wxWidgets.git] / tests / datetime / datetimetest.cpp
index 60fcf0c43bbb9f66872cc2c2b5bef2d504635992..29209eef6c7feedf0f957d093356b348ef069c53 100644 (file)
 
 #include "wx/datetime.h"
 
 
 #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
 // ----------------------------------------------------------------------------
 // ----------------------------------------------------------------------------
 // broken down date representation used for testing
 // ----------------------------------------------------------------------------
@@ -129,6 +177,7 @@ private:
         CPPUNIT_TEST( TestTimeTicks );
         CPPUNIT_TEST( TestTimeParse );
         CPPUNIT_TEST( TestTimeArithmetics );
         CPPUNIT_TEST( TestTimeTicks );
         CPPUNIT_TEST( TestTimeParse );
         CPPUNIT_TEST( TestTimeArithmetics );
+        CPPUNIT_TEST( TestDSTBug );
     CPPUNIT_TEST_SUITE_END();
 
     void TestLeapYears();
     CPPUNIT_TEST_SUITE_END();
 
     void TestLeapYears();
@@ -141,6 +190,7 @@ private:
     void TestTimeTicks();
     void TestTimeParse();
     void TestTimeArithmetics();
     void TestTimeTicks();
     void TestTimeParse();
     void TestTimeArithmetics();
+    void TestDSTBug();
 
     DECLARE_NO_COPY_CLASS(DateTimeTestCase)
 };
 
     DECLARE_NO_COPY_CLASS(DateTimeTestCase)
 };
@@ -207,7 +257,9 @@ void DateTimeTestCase::TestTimeJDN()
     {
         const Date& d = testDates[n];
         wxDateTime dt(d.day, d.month, d.year, d.hour, d.min, d.sec);
     {
         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 );
 
 
         CPPUNIT_ASSERT( jdn == d.jdn );
 
@@ -612,7 +664,7 @@ void DateTimeTestCase::TestTimeTicks()
         long ticks = (dt.GetValue() / 1000).ToLong();
         CPPUNIT_ASSERT( ticks == d.ticks );
 
         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 );
     }
         ticks = (dt.GetValue() / 1000).ToLong();
         CPPUNIT_ASSERT( ticks == d.gmticks );
     }
@@ -624,12 +676,25 @@ void DateTimeTestCase::TestTimeParse()
     static const struct ParseTestData
     {
         const wxChar *format;
     static const struct ParseTestData
     {
         const wxChar *format;
-        Date date;
+        Date date;              // NB: this should be in UTC
         bool good;
     } parseTestDates[] =
     {
         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++ )
     };
 
     for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
@@ -641,10 +706,7 @@ void DateTimeTestCase::TestTimeParse()
         {
             CPPUNIT_ASSERT( parseTestDates[n].good );
 
         {
             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
             CPPUNIT_ASSERT( dt == dtReal );
         }
         else // failed to parse
@@ -681,3 +743,75 @@ void DateTimeTestCase::TestTimeArithmetics()
     }
 }
 
     }
 }
 
+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
+}
+