📄 jdate.cpp
字号:
// Build the JD _makeJDFromGregorian(y, m, d, hh, mm, ss); // If we have a timezone specified, convert to UTC from the timezone if (zzz != 0) { int changeHour = zzz * -1; // Eg for GMT +2 need to subtract 2 hours modifyDate(HOURS, changeHour); // Internal date is now UTC } _TZ = zzz;} // JDate::setDate//------------------------------------------------------------------------------// JDate::setDate//------------------------------------------------------------------------------voidJDate::setDate( const time_t unixTime, const int timeZone){ // Assign using the time of the unix time stamp struct tm ut; ut = *gmtime(&unixTime); _makeJDFromGregorian(ut.tm_year+1900, ut.tm_mon+1, ut.tm_mday, ut.tm_hour, ut.tm_min, ut.tm_sec); // If we have a timezone specified, convert to UTC from the timezone if (timeZone != 0) { int changeHour = timeZone * -1; // Eg for GMT +2 need to subtract 2 hours modifyDate(HOURS, changeHour); // Internal date is now UTC } _TZ = timeZone;} // JDate::setDate //------------------------------------------------------------------------------// JDate::setDate//------------------------------------------------------------------------------voidJDate::setDate( const double julianDayNumber){ _JD = (long)fabs(julianDayNumber); _SN = (long)fabs((julianDayNumber - _JD) * SECS_ONE_DAY); _TZ = 0;} // JDate::setDate//------------------------------------------------------------------------------// JDate::setDate//------------------------------------------------------------------------------voidJDate::setDate( int year, int month, int day, int hour, int minute, int second, int timeZone){ _makeJDFromGregorian(year, month, day, hour, minute, second); // If we have a timezone specified, convert to UTC from the timezone if (timeZone != 0) { int changeHour = timeZone * -1; // Eg for GMT +2 need to subtract 2 hours modifyDate(HOURS, changeHour); // Internal date is now UTC } _TZ = timeZone; } // JDate::setDate //------------------------------------------------------------------------------// JDate::setDate//------------------------------------------------------------------------------voidJDate::setDate( const JDate &dateObj){ _JD = dateObj._JD; _SN = dateObj._SN; _TZ = dateObj._TZ;} // JDate::setDate//------------------------------------------------------------------------------// JDate::setDate//------------------------------------------------------------------------------voidJDate::setDate( const JulianDayNumber& julianDayNumber){ _JD = julianDayNumber.julianDay; _SN = julianDayNumber.secondsSinceMidday; _TZ = 0;}//------------------------------------------------------------------------------// JDate::modifyDate//------------------------------------------------------------------------------void JDate::modifyDate( Units unit, const long numUnits){ long years, months, days, secs; int y, m, d, hh, mm, ss; switch (unit) { case SECONDS: // Add given seconds to the date. days = numUnits / SECS_ONE_DAY; secs = numUnits % SECS_ONE_DAY; // Make sure we are not out of bounds if (days > 0 && (unsigned long)_JD + (unsigned long)days > (unsigned long)MAX_JD_FOR_DATE) throw OutOfRangeError("Unable to convert seconds. Value increases date larger than year 9999."); if (days < 0 && days + _JD < 0) throw OutOfRangeError("Unable to convert seconds. Value decreases date smaller than year -4713."); // Offset the days. _JD += days; // Offset the seconds correctly. // > 1 day in seconds if (secs > 0 && secs + _SN >= SECS_ONE_DAY) { _JD++; _SN = (secs + _SN) - SECS_ONE_DAY; } else { // < 1 day in seconds if (secs < 0 && secs + _SN < 0) { _JD--; _SN = SECS_ONE_DAY + (secs + _SN); } else { // Normal, just offset the number of seconds _SN += secs; } } break; case MINUTES: if (numUnits > 0 && numUnits > LONG_MAX / SECS_ONE_MINUTE) throw OutOfRangeError("Unable to convert minutes to seconds. Value too large."); if (numUnits < 0 && numUnits < LONG_MIN / SECS_ONE_MINUTE) throw OutOfRangeError("Unable to convert minutes to seconds. Value too large."); modifyDate(SECONDS, numUnits * SECS_ONE_MINUTE); break; case HOURS: if (numUnits > 0 && numUnits > LONG_MAX / SECS_ONE_HOUR) throw OutOfRangeError("Unable to convert hours to seconds. Value too large."); if (numUnits < 0 && numUnits < LONG_MIN / SECS_ONE_HOUR) throw OutOfRangeError("Unable to convert hours to seconds. Value too large."); modifyDate(SECONDS, numUnits * SECS_ONE_HOUR); break; case DAYS: // Make sure the days does not exceed the range if (numUnits > 0 && (unsigned long)_JD + (unsigned long)numUnits > (unsigned long)MAX_JD_FOR_DATE) throw OutOfRangeError("Unable to convert days. Value increases date larger than year 9999."); if (numUnits < 0 && numUnits + _JD < 0) throw OutOfRangeError("Unable to convert days. Value decreases date smaller than year -4713."); _JD += numUnits; break; case MONTHS: // Convert to gregorian, change and convert back _makeGregorianFromJD(y, m, d, hh, mm, ss); years = numUnits / 12; months = numUnits % 12; // Offset the years correctly y += years; // Offset the months m += months; if (m > 12) { y++; m -= 12; } if (m <= 0) { y--; m = 12 + m; } if (y > 9999) throw OutOfRangeError("Unable to convert months. Value increases date larger than year 9999."); if (y < -4713) throw OutOfRangeError("Unable to convert months. Value decreases date smaller than year -4713."); // Check for a day overflow if (isGregorianLeapYear(y)) { if (d > maxDaysInMonthsLeap[m]) d = maxDaysInMonthsLeap[m]; } else { if (d > maxDaysInMonths[m]) d = maxDaysInMonths[m]; } _makeJDFromGregorian(y, m, d, hh, mm, ss); break; case YEARS: // Convert to gregorian, change and convert back _makeGregorianFromJD(y, m, d, hh, mm, ss); if (numUnits > 0 && numUnits + y > 9999) throw OutOfRangeError("Unable to convert years. Value increases date larger than year 9999."); if (numUnits < 0 && numUnits + y < -4713) throw OutOfRangeError("Unable to convert years. Value decreases date smaller than year -4713."); y += numUnits; _makeJDFromGregorian(y, m, d, hh, mm, ss); break; }} // JDate::incDate//------------------------------------------------------------------------------// JDate::operator=//------------------------------------------------------------------------------voidJDate::operator=( const char* dateStr){ setDate(dateStr); } // JDate::operator=//------------------------------------------------------------------------------// JDate::operator=//------------------------------------------------------------------------------voidJDate::operator=( const string &dateStr){ setDate(dateStr); } // JDate::operator= //------------------------------------------------------------------------------// JDate::operator=//------------------------------------------------------------------------------voidJDate::operator=( time_t unixTime){ setDate(unixTime);} // JDate::operator= //------------------------------------------------------------------------------// JDate::operator=//------------------------------------------------------------------------------voidJDate::operator=( const double julianDayNumber){ setDate(julianDayNumber);} // JDate::operator=//------------------------------------------------------------------------------// JDate::operator=//------------------------------------------------------------------------------voidJDate::operator=( const JulianDayNumber julianDayNumber){ setDate(julianDayNumber);}//------------------------------------------------------------------------------// JDate::operator+ //------------------------------------------------------------------------------JDateJDate::operator+( const JDate &x){ // Initialize z with the value of our current date JDate z; z._JD = _JD; z._SN = _SN; // Add x to the z date. z.modifyDate(DAYS, x._JD); z.modifyDate(SECONDS, x._SN); return z;} // JDate::operator+//------------------------------------------------------------------------------// JDate::operator-//------------------------------------------------------------------------------JDateJDate::operator-( const JDate &x){ // Initialize z with the value of our current date JDate z; z._JD = _JD; z._SN = _SN; // Subtract x from the z date. z.modifyDate(DAYS, -1 * x._JD); z.modifyDate(SECONDS, -1 * x._SN); return z;} // JDate::operator-//------------------------------------------------------------------------------// JDate::operator+=//------------------------------------------------------------------------------JDateJDate::operator+=( const JDate &x){ // Add x to our date modifyDate(DAYS, x._JD); modifyDate(SECONDS, x._SN); return *this;} // JDate::operator+=//------------------------------------------------------------------------------// JDate::operator-=//------------------------------------------------------------------------------JDateJDate::operator-=( const JDate &x){ // Subtract x from our date modifyDate(DAYS, -1 * x._JD); modifyDate(SECONDS, -1 * x._SN); return *this;} // JDate::operator-=//------------------------------------------------------------------------------// JDate::operator>//------------------------------------------------------------------------------ intJDate::operator>( const JDate &x){ if (_JD > x._JD) return 1; if (_JD == x._JD && _SN > x._SN) return 1; return 0;} //------------------------------------------------------------------------------// JDate::operator>=//------------------------------------------------------------------------------ intJDate::operator>=( const JDate &x){ if (_JD > x._JD) return 1; if (_JD == x._JD && _SN >= x._SN) return 1; return 0;}//------------------------------------------------------------------------------// JDate::operator<//------------------------------------------------------------------------------ intJDate::operator<( const JDate &x){ if (_JD < x._JD) return 1; if (_JD == x._JD && _SN < x._SN) return 1; return 0;}//------------------------------------------------------------------------------// JDate::operator<=//------------------------------------------------------------------------------ intJDate::operator<=( const JDate &x){ if (_JD < x._JD) return 1; if (_JD == x._JD && _SN <= x._SN) return 1; return 0;}//------------------------------------------------------------------------------// JDate::operator==//------------------------------------------------------------------------------ intJDate::operator==( const JDate &x){ return (_JD == x._JD && _SN == x._SN);}//------------------------------------------------------------------------------// JDate::operator!=//------------------------------------------------------------------------------ intJDate::operator!=( const JDate &x){ return (!(_JD == x._JD && _SN == x._SN));}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -