Commits

Kaya Kupferschmidt committed adff8d6

Added more tests
Added era to Date

Comments (0)

Files changed (6)

source/libs/magnum_core/source/magnum/datetime/Date.cpp

 }
 
 
-
-/*--------------------------------------------------------------------------*/
-/**
- */
-Date::Date(int year, int month, int day)
-{
-    m_JulianDay = julianDayFromDate(year, month, day);
-}
-
-
 /*--------------------------------------------------------------------------*/
 /**
  */
 /*--------------------------------------------------------------------------*/
 /**
  */
+void Date::setDate(Date::Era era, int year, int month, int day)
+{
+    switch(era) {
+        case BC:
+            year = -year;
+            break;
+        case AD:
+            break;
+    }
+
+    m_JulianDay = julianDayFromDate(year, month, day);
+}
+
+
+/*--------------------------------------------------------------------------*/
+/**
+ */
 void Date::getDate(int *year, int *month, int *day) const
 {
     dateFromJulianDay(m_JulianDay, year, month, day);

source/libs/magnum_core/source/magnum/datetime/Date.h

         : m_JulianDay(0) { }
     inline explicit Date(int jd)
         : m_JulianDay(jd) { }
-    Date(int year, int month, int day);
+    inline Date(int year, int month, int day)
+        { setDate(year, month, day); }
+    inline Date(Date::Era era, int year, int month, int day)
+        { setDate(era, year, month, day); }
 
 	void getCurrentDate();
 	
     void setYear(int );
 
     void setDate(int year, int month, int day);
+    void setDate(Date::Era era, int year, int month, int day);
     void getDate(int* year, int* month, int* day) const;
         
     void addDays(int days);

source/libs/magnum_core/source/magnum/datetime/SimpleTimeZone.cpp

 
 int SimpleTimeZone::getOffsetFromLocal(int era, int year, int month, int day, int dow, int time) const
 {
+    // No STD / DST time?
+    if (m_DstOffset == m_RawOffset)
+        return -m_RawOffset;
+
+    if (era != Date::AD && era != Date::BC)
+        throw IllegalArgumentException("Illegal era " + era);
+    if (era == Date::BC)
+        year = -year;
+
+    if (year < m_StartYear || year > m_EndYear) {
+        return -m_RawOffset;
+    }
+
     return 0;
 }
 

source/unittest/datetime/Test_Date.h

 }
 
 
+void test_era()
+{
+    Date date;
+    TEST_THROWS_NOTHING( date.setDate(Date::AD, 1, 1, 1) );
+    TEST_ASSERT( date.getYear() == 1 );
+    TEST_ASSERT( date.getMonth() == 1 );
+    TEST_ASSERT( date.getDayOfMonth() == 1 );
+
+    TEST_THROWS_NOTHING( date.addDays(-1) );
+    TEST_ASSERT( date.getYear() == -1 );
+    TEST_ASSERT( date.getMonth() == 12 );
+    TEST_ASSERT( date.getDayOfMonth() == 31 );
+
+    TEST_THROWS_NOTHING( date.addDays(1) );
+    TEST_ASSERT( date.getYear() == 1 );
+    TEST_ASSERT( date.getMonth() == 1 );
+    TEST_ASSERT( date.getDayOfMonth() == 1 );
+
+    TEST_THROWS_NOTHING( date.setDate(Date::AD, -1, 1, 1) );
+    TEST_ASSERT( date.getYear() == -1 );
+    TEST_ASSERT( date.getMonth() == 1 );
+    TEST_ASSERT( date.getDayOfMonth() == 1 );
+
+    TEST_THROWS_NOTHING( date.setDate(Date::BC, 1, 1, 1) );
+    TEST_ASSERT( date.getYear() == -1 );
+    TEST_ASSERT( date.getMonth() == 1 );
+    TEST_ASSERT( date.getDayOfMonth() == 1 );
+}
+
+
 void test_epoch()
 {
     Date date(1972,6,30);
 Test_Date()
 {
     TEST_ADD(Test_Date::test_basics);
+    TEST_ADD(Test_Date::test_era);
     TEST_ADD(Test_Date::test_epoch);
     TEST_ADD(Test_Date::test_arithmetic);
     TEST_ADD(Test_Date::test_comparison);

source/unittest/datetime/Test_SimpleTimeZone.h

     TEST_ASSERT( tz->getRawOffset() == 3600 );
     TEST_ASSERT( tz->hasDaylightSaving() );
     TEST_ASSERT( tz->getDaylightSavings() == 3600 );
+
+    TEST_ASSERT( !tz->isDaylightSaving(Date::AD, 2012, 3, 25, Date::Sunday, 3599) );
+    TEST_ASSERT( tz->isDaylightSaving(Date::AD, 2012, 3, 25, Date::Sunday, 3600) );
+    TEST_ASSERT( tz->getOffsetFromUtc(Date::AD, 2012, 3, 24, Date::Saturday, 0) == 3600 );
+    TEST_ASSERT( tz->getOffsetFromUtc(Date::AD, 2012, 3, 25, Date::Sunday, 0) == 3600 );
+    TEST_ASSERT( tz->getOffsetFromUtc(Date::AD, 2012, 3, 25, Date::Sunday, 3599) == 3600 );
+    TEST_ASSERT( tz->getOffsetFromUtc(Date::AD, 2012, 3, 25, Date::Sunday, 3600) == 7200 );
+    TEST_ASSERT( tz->getOffsetFromUtc(Date::AD, 2012, 3, 26, Date::Monday, 0) == 7200 );
+
+    TEST_ASSERT( tz->isDaylightSaving(Date::AD, 2012, 10, 28, Date::Sunday, 3599) );
+    TEST_ASSERT( !tz->isDaylightSaving(Date::AD, 2012, 10, 28, Date::Sunday, 3600) );
     TEST_ASSERT( tz->getOffsetFromUtc(Date::AD, 2012, 10, 27, Date::Saturday, 0) == 7200 );
     TEST_ASSERT( tz->getOffsetFromUtc(Date::AD, 2012, 10, 28, Date::Sunday, 0) == 7200 );
     TEST_ASSERT( tz->getOffsetFromUtc(Date::AD, 2012, 10, 28, Date::Sunday, 3599) == 7200 );

source/unittest/datetime/Test_TimeZone.h

     TEST_ASSERT( tz->getRawOffset() == 3600 );
     TEST_ASSERT( tz->hasDaylightSaving() );
     TEST_ASSERT( tz->getDaylightSavings() == 3600 );
+
+    TEST_ASSERT( !tz->isDaylightSaving(Date::AD, 2012, 3, 25, Date::Sunday, 3599) );
+    TEST_ASSERT( tz->isDaylightSaving(Date::AD, 2012, 3, 25, Date::Sunday, 3600) );
+    TEST_ASSERT( tz->getOffsetFromUtc(Date::AD, 2012, 3, 24, Date::Saturday, 0) == 3600 );
+    TEST_ASSERT( tz->getOffsetFromUtc(Date::AD, 2012, 3, 25, Date::Sunday, 0) == 3600 );
+    TEST_ASSERT( tz->getOffsetFromUtc(Date::AD, 2012, 3, 25, Date::Sunday, 3599) == 3600 );
+    TEST_ASSERT( tz->getOffsetFromUtc(Date::AD, 2012, 3, 25, Date::Sunday, 3600) == 7200 );
+    TEST_ASSERT( tz->getOffsetFromUtc(Date::AD, 2012, 3, 26, Date::Monday, 0) == 7200 );
+
+    TEST_ASSERT( tz->isDaylightSaving(Date::AD, 2012, 10, 28, Date::Sunday, 3599) );
+    TEST_ASSERT( !tz->isDaylightSaving(Date::AD, 2012, 10, 28, Date::Sunday, 3600) );
     TEST_ASSERT( tz->getOffsetFromUtc(Date::AD, 2012, 10, 27, Date::Saturday, 0) == 7200 );
     TEST_ASSERT( tz->getOffsetFromUtc(Date::AD, 2012, 10, 27, Date::Saturday, 24*60*60-1) == 7200 );
     TEST_ASSERT( tz->getOffsetFromUtc(Date::AD, 2012, 10, 28, Date::Sunday, 0) == 7200 );