Commits

Kaya Kupferschmidt committed 838c26d

Fixed several datetime bugs
Added support for unix epoch

Comments (0)

Files changed (3)

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

 
 
 /*--------------------------------------------------------------------------*/
-/**
+/** Sets the total number of seconds since 1.1.1970 including leap seconds.
+ * Note that this is not the same as unix epoch, because the later does not
+ * include leap seconds
  */
 void DateTime::setTotalSecondsSince1970(int64 secs)
 {
-    setTotalSecondsSince1601(secs + 2440588LL * SecondsPerDay);
+    setTotalSeconds(secs + 2440588LL * SecondsPerDay);
 }
 
 
 {
     bool sign = seconds < 0;
     int julianDay = sign ? -int(-seconds / SecondsPerDay) : int(seconds / SecondsPerDay);
-    seconds -= julianDay * SecondsPerDay;
+    seconds -= julianDay * int64(SecondsPerDay);
 
     int64 curTicks = seconds * TicksPerSecond;
     setAbsolute(julianDay, curTicks);
 /*--------------------------------------------------------------------------*/
 /**
  */
+int64 DateTime::getUnixTime() const
+{
+    return getTotalSecondsSince1970() - getTotalLeapSeconds();
+}
+
+
+/*--------------------------------------------------------------------------*/
+/**
+ */
+void DateTime::setUnixTime(int64 time)
+{
+    int64 seconds = time + 2440588LL * SecondsPerDay;
+    bool sign = seconds < 0;
+    int julianDay = sign ? -int(-seconds / SecondsPerDay) : int(seconds / SecondsPerDay);
+    seconds -= julianDay * int64(SecondsPerDay);
+
+    int64 curTicks = seconds * TicksPerSecond;
+    m_Date.setJulianDay(julianDay);
+    m_Time.setTicks(curTicks);
+}
+
+
+/*--------------------------------------------------------------------------*/
+/**
+ */
 bool DateTime::isValid() const
 {
     return m_Date.isValid() && m_Time.isValid();
 
 
 /*--------------------------------------------------------------------------*/
-/**
+/** Sets an absolute date.
+ * @param julianDay - the julian day to set
+ * @param curTicks - the ticks of the day including any leap seconds
+ *                   since epoch
  */
 void DateTime::setAbsolute(int julianDay, int64 curTicks)
 {

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

     void setTotalSecondsSince1601(int64 secs);
     void setTotalSeconds(int64 secs);
 
+    //! Returns the total number of seconds since 1.1.1970 including leap seconds.
+    //! Note that this is not the same as the unix epoch, because the later does not
+    //! include leap seconds!
     inline int64 getTotalSecondsSince1970() const
         { return getTotalSeconds() - 2440588LL * SecondsPerDay; }
     inline int64 getTotalSecondsSince1601() const
         { return getTotalSeconds() - 2305814LL * SecondsPerDay; }
 
+    int64 getUnixTime() const;
+    void setUnixTime(int64 );
+
     inline int getDayOfYear() const
         { return m_Date.getDayOfYear(); }
     inline int getDayOfMonth() const

source/unittest/datetime/Test_DateTime.h

 {
     DateTime epoch1970(Date(1970,1,1), Time(0));
     TEST_ASSERT( epoch1970.getTotalSecondsSince1970() == epoch1970.getTotalLeapSeconds() );
+    TEST_ASSERT( epoch1970.getTotalSecondsSince1970() == 10 );
+    TEST_ASSERT( epoch1970.getTotalLeapSeconds() == 10 );
+    TEST_ASSERT( epoch1970.getUnixTime() == 0 );
+    TEST_ASSERT( epoch1970.getDate() == Date(1970,1,1) );
+    TEST_ASSERT( epoch1970.getTime() == Time(0,0,0) );
+
+    epoch1970.setUnixTime(0);
+    TEST_ASSERT( epoch1970.getTotalSecondsSince1970() == epoch1970.getTotalLeapSeconds() );
+    TEST_ASSERT( epoch1970.getTotalSecondsSince1970() == 10 );
+    TEST_ASSERT( epoch1970.getTotalLeapSeconds() == 10 );
+    TEST_ASSERT( epoch1970.getUnixTime() == 0 );
+    TEST_ASSERT( epoch1970.getDate() == Date(1970,1,1) );
+    TEST_ASSERT( epoch1970.getTime() == Time(0,0,0) );
+
+    DateTime other(Date(2013,2,21), Time(18,14,54));
+    TEST_ASSERT( other.getUnixTime() == 1361470494 );
+    TEST_ASSERT( other.getTotalSecondsSince1970() == 1361470494 + 35);
+    TEST_ASSERT( other.getTotalLeapSeconds() == 35 );
+
+    other.setTotalSecondsSince1970(1361470494 + 35);
+    TEST_ASSERT( other.getDate() == Date(2013,2,21) );
+    TEST_ASSERT( other.getTime() == Time(18,14,54) );
+    TEST_ASSERT( other.getUnixTime() == 1361470494 );
+    TEST_ASSERT( other.getTotalSecondsSince1970() == 1361470494 + 35);
+    TEST_ASSERT( other.getTotalLeapSeconds() == 35 );
+
+    other.setUnixTime(1361470494);
+    TEST_ASSERT( other.getDate() == Date(2013,2,21) );
+    TEST_ASSERT( other.getTime() == Time(18,14,54) );
+    TEST_ASSERT( other.getUnixTime() == 1361470494 );
+    TEST_ASSERT( other.getTotalSecondsSince1970() == 1361470494 + 35);
+    TEST_ASSERT( other.getTotalLeapSeconds() == 35 );
+
+    other.setTotalSecondsSince1970( epoch1970.getTotalSecondsSince1970() );
+    TEST_ASSERT( other == epoch1970 );
 
     DateTime epoch1601(Date(1601,1,1), Time(0));
     TEST_ASSERT( epoch1601.getTotalSecondsSince1601() == epoch1601.getTotalLeapSeconds() );
+    other.setTotalSecondsSince1601( epoch1601.getTotalSecondsSince1601() );
+    TEST_ASSERT( other == epoch1601 );
+
 }