int32 TimeStamp::compare( const TimeStamp &ts ) const { if ( m_year < ts.m_year ) return -1; if ( m_year > ts.m_year ) return 1; if ( m_month < ts.m_month ) return -1; if ( m_month > ts.m_month ) return 1; if ( m_day < ts.m_day ) return -1; if ( m_day > ts.m_day ) return 1; if ( ts.m_timezone == m_timezone || ts.m_timezone == tz_NONE || m_timezone == tz_NONE ) { if ( m_hour < ts.m_hour ) return -1; if ( m_hour > ts.m_hour ) return 1; if ( m_day < ts.m_day ) return -1; if ( m_day > ts.m_day ) return 1; } else { int16 hdisp=0, mdisp=0; int16 ts_hdisp=0, ts_mdisp=0; \ getTZDisplacement( hdisp, mdisp ); ts.getTZDisplacement( ts_hdisp, ts_mdisp ); if ( m_hour + hdisp < ts.m_hour + ts_hdisp ) return -1; if ( m_hour + hdisp > ts.m_hour + ts_hdisp ) return 1; if ( m_day + mdisp < ts.m_day + ts_mdisp ) return -1; if ( m_day + mdisp > ts.m_day + ts_mdisp ) return 1; } if ( m_minute < ts.m_minute ) return -1; if ( m_minute > ts.m_minute ) return 1; if ( m_second < ts.m_second ) return -1; if ( m_second > ts.m_second ) return 1; if ( m_msec < ts.m_msec ) return -1; if ( m_msec > ts.m_msec ) return 1; return 0; }
void TimeStamp::add( const TimeStamp &ts ) { m_day += ts.m_day; m_hour += ts.m_hour; m_minute += ts.m_minute; m_second += ts.m_second; m_msec += ts.m_msec; if ( m_timezone != ts.m_timezone && m_timezone != tz_NONE && ts.m_timezone != tz_NONE ) { int16 hours=0, mins=0, ts_hours=0, ts_mins=0; ts.getTZDisplacement( ts_hours, ts_mins ); getTZDisplacement( hours, mins ); m_hour += hours - ts_hours; m_minute += hours - ts_mins; } rollOver(); if ( m_timezone == tz_NONE ) m_timezone = ts.m_timezone; }
void TimeStamp::distance( const TimeStamp &ts ) { int days = 0; // first decide which date is bigger. const TimeStamp *startDate, *endDate; int comparation = this->compare( ts ); if (comparation == 0 ) { // the same date, means no distance. m_msec = m_second = m_minute = m_hour = m_day = m_month = m_year = 0; return; } if ( comparation > 0 ) { startDate = &ts; endDate = this; } else { startDate = this; endDate = &ts; } // If year is different: if( startDate->m_year != endDate->m_year ) { // calculate the number of days in the in-between years for ( int baseYear = startDate->m_year + 1; baseYear < endDate->m_year; baseYear++ ) days += i_isLeapYear( baseYear ) ? 366 : 365; // calculate the number of days from start day to the end of the year. int doy = ( startDate->isLeapYear() ? 366 : 365 ) - startDate->dayOfYear(); days += doy; // and add the days in the year of the target date days += endDate->dayOfYear(); } else { days += endDate->dayOfYear() - startDate->dayOfYear(); } m_year = 0; m_month = 0; //int tday = days; int thour = endDate->m_hour; int tminute = endDate->m_minute; int tsecond = endDate->m_second; int tmsec = endDate->m_msec; if ( m_timezone != ts.m_timezone && m_timezone != tz_NONE && ts.m_timezone != tz_NONE ) { int16 hours=0, mins=0, ts_hours=0, ts_mins=0; ts.getTZDisplacement( ts_hours, ts_mins ); getTZDisplacement( hours, mins ); // if ts bigger (positive distance) we must add the difference between TS timezone and us if ( comparation < 0 ) { thour += ts_hours - hours; tminute += ts_mins - mins; } else { // else we got to subtract it thour += ts_hours - hours; tminute += ts_mins - mins; } } cplxSub( tmsec, startDate->m_msec, 1000, tsecond ); cplxSub( tsecond, startDate->m_second, 60, tminute ); cplxSub( tminute, startDate->m_minute, 60, thour ); cplxSub( thour, startDate->m_hour, 24, days ); m_day = days; m_hour = thour; m_minute = tminute; m_second = tsecond; m_msec = tmsec; if( comparation > 0 ) { // the negative sign goes on the first non-zero unit if ( m_day != 0 ) m_day = -m_day; else if ( m_hour != 0 ) m_hour = -m_hour; else if ( m_minute != 0 ) m_minute = -m_minute; else if ( m_second != 0 ) m_second = -m_second; else m_msec = -m_msec; } m_timezone = tz_NONE; }