📄 jdate.cpp
字号:
//------------------------------------------------------------------------------// JDate::JDate//------------------------------------------------------------------------------JDate::JDate( int year, int month, int day, int hour, int minute, int second, int timeZone) : _JD(0),
_SN(0),
_TZ(0),
_DST(0){ // Initialize the class _initialize(); setDate(year, month, day, hour, minute, second, timeZone);} // JDate::JDate//------------------------------------------------------------------------------// JDate::JDate//------------------------------------------------------------------------------JDate::JDate( const JulianDayNumber& julianDayNumber){ // Initialize the class _initialize(); setDate(julianDayNumber);} // JDate::JDate//------------------------------------------------------------------------------// JDate::~JDate//------------------------------------------------------------------------------JDate::~JDate(){} // JDate::~JDate
//------------------------------------------------------------------------------
// JDate::setTimeOffsetFile
//------------------------------------------------------------------------------
void
JDate::setTimeOffsetFile(
const string &file)
{
timeOffsetFile = file.c_str();
} // JDate::setTimeOffsetFile
//------------------------------------------------------------------------------// JDate::getRealSecondsForVirtualSeconds//------------------------------------------------------------------------------unsigned longJDate::getRealSecondsForVirtualSeconds( const long virtualSecs){ unsigned long startTimeStampUTC, secondInterval; unsigned long res = virtualSecs; if (_getTimeOffsetParameters(startTimeStampUTC, secondInterval)) { res /= secondInterval; // The number of real seconds the virtual seconds are equal to :) } return res;} // JDate::getRealSecondsForVirtualSeconds
//------------------------------------------------------------------------------// JDate::isJulianLeapYear//------------------------------------------------------------------------------boolJDate::isJulianLeapYear( long astronomersYear){ // If the year is divisible by 4 it is a Julian Leap year unless, // there where no leap years between 9BC and 8AD (Not certain as some theories stat 4AD) or // between 45BC and 10BC every 3 years was a leap year. // -8 = 9BC, 8 = 8AD in astronomers standards if (astronomersYear >= -8 && astronomersYear <= 8) return false; // -44 = 45BC, -9 = 10BC in astronomers standards. if (astronomersYear >= -44 && astronomersYear <= -9) return !(astronomersYear % 3); // Default rule. return !(astronomersYear % 4);} // JDate::isJulianLeapYear//------------------------------------------------------------------------------// JDate::isGregorianLeapYear//------------------------------------------------------------------------------boolJDate::isGregorianLeapYear( long astronomersYear){ // If the year is divisible by 4 it is a Gregorian Leap year unless, // the year is divisible by 100, // but finaly if the year is divisible by 400 it is a leap year. // The first Gregorian calander was founded in 1582 if (astronomersYear < 1582) return false; if (astronomersYear % 400 == 0) return true; if ((astronomersYear % 4 == 0) && !(astronomersYear % 100 == 0)) return true; return false;} // JDate::isGregorianLeapYear //------------------------------------------------------------------------------// JDate::asString//------------------------------------------------------------------------------stringJDate::asString( const string &fmt){ string res = fmt; int y, m, d, hh, mm, ss; int pos; char tmp[16]; // Get the gregorian date from the JD _makeGregorianFromJD(y, m, d, hh, mm, ss); // Substitute the longest directive to the smallest. // %% if ((pos = res.find("%%")) != string::npos) { res.replace(pos, 2, "%"); } // %a if ((pos = res.find("%a")) != string::npos) { int dow = _makeDayOfWeek(y, m, d); res.replace(pos, 2, shortDayOfWeek[dow]); } // %A if ((pos = res.find("%A")) != string::npos) { int dow = _makeDayOfWeek(y, m, d); res.replace(pos, 2, dayOfWeek[dow]); } // %b if ((pos = res.find("%b")) != string::npos) { res.replace(pos, 2, shortNameOfMonth[m-1]); } // %B if ((pos = res.find("%B")) != string::npos) { res.replace(pos, 2, nameOfMonth[m-1]); } // %d if ((pos = res.find("%d")) != string::npos) { sprintf(tmp, "%02d", d); res.replace(pos, 2, tmp); } // %H if ((pos = res.find("%H")) != string::npos) { sprintf(tmp, "%02d", hh); res.replace(pos, 2, tmp); } // %m if ((pos = res.find("%m")) != string::npos) { sprintf(tmp, "%02d", m); res.replace(pos, 2, tmp); } // %M if ((pos = res.find("%M")) != string::npos) { sprintf(tmp, "%02d", mm); res.replace(pos, 2, tmp); } // %n if ((pos = res.find("%n")) != string::npos) { res.replace(pos, 2, (const char*)'\n'); } // %S if ((pos = res.find("%S")) != string::npos) { sprintf(tmp, "%02d", ss); res.replace(pos, 2, tmp); } // %t if ((pos = res.find("%t")) != string::npos) { res.replace(pos, 2, (const char*)'\t'); } // %w if ((pos = res.find("%w")) != string::npos) { int dow = _makeDayOfWeek(y, m, d); sprintf(tmp, "%d", dow); res.replace(pos, 2, tmp); } // %y if ((pos = res.find("%y")) != string::npos) { sprintf(tmp, "%02d", y % 100); res.replace(pos, 2, tmp); } // %Y if ((pos = res.find("%Y")) != string::npos) { sprintf(tmp, "%04d", y); res.replace(pos, 2, tmp); } return res;} // JDate::asString//------------------------------------------------------------------------------// JDate::asJulianDayNumber//------------------------------------------------------------------------------doubleJDate::asJulianDayNumber(){ // Build the floating point representation of a JD double res = _SN; res /= (1.0 * SECS_ONE_DAY); res += _JD; return res;} // JDate::asJulianDayNumber//------------------------------------------------------------------------------// JDate::asComponents//------------------------------------------------------------------------------voidJDate::asComponents( int& year, int& month, int& day, int& hour, int& minute, int& second){ _makeGregorianFromJD(year, month, day, hour, minute, second);} // JDate::asComponents //------------------------------------------------------------------------------// JDate::asComponent//------------------------------------------------------------------------------intJDate::asComponent( Units unit){ int year, month, day, hour, minute, second; asComponents(year, month, day, hour, minute, second); switch(unit) { case YEARS: return year; break; case MONTHS: return month; break; case DAYS: return day; break; case HOURS: return hour; break; case MINUTES: return minute; break; case SECONDS: return second; break; default: return -1; }} // JDate::asComponent//------------------------------------------------------------------------------// JDate::asJulianDayNumberStruct//------------------------------------------------------------------------------JDate::JulianDayNumberJDate::asJulianDayNumberStruct(){ // Build the floating point representation of a JD JulianDayNumber res; res.julianDay = _JD; res.secondsSinceMidday = _SN; return res;} // JDate::asJulianDayNumberStruct//------------------------------------------------------------------------------// JDate::asUnixTimeStamp//------------------------------------------------------------------------------time_tJDate::asUnixTimeStamp(){ // Check if the timestamp is within range double jd = asJulianDayNumber(); if (jd > MAX_JD_FOR_UNIXTIMESTAMP || jd < MIN_JD_FOR_UNIXTIMESTAMP) throw OutOfRangeError("The internal date is not within the range 1970-01-01 00:00:00 --> 2038-01-19 00:00:00"); // Convert to a unix timestamp as UTC. time_t res = 0; long days = _JD - (long)MIN_JD_FOR_UNIXTIMESTAMP; // Create the value from the number of days and seconds. res = days * SECS_ONE_DAY; res = res + _SN - 43200; return res; } // JDate::asUnixTimeStamp//------------------------------------------------------------------------------// JDate::setDate//------------------------------------------------------------------------------int convertDatePortion( const string &item, const string &dateStr, const string &fmt){ int pos; int res = INVALID_FUNCTION_RES; if ( (pos = fmt.find(item)) != string::npos) { char tmp[16]; char *offset, *endptr = NULL; memset(tmp, 0, sizeof(tmp)); offset = (char*)dateStr.c_str()+(pos * sizeof(char)); strncpy(tmp, offset, item.length()); res = strtol(tmp, &endptr, 10); // Check if a conversion error occured if (endptr[0] != '\0') { string err = "The "; err += item; err += " value is in the incorrect format"; throw JDate::ConversionError(err); } } return res; }//------------------------------------------------------------------------------// JDate::setDate//------------------------------------------------------------------------------voidJDate::setDate( const string &dateStr, const string &fmt){ bool yearNeg = false; string dateString = dateStr; // If the dateStr is 1 bigger than the formating string, make sure it is due to a -ve sign if (1 == dateString.length() - fmt.length()) { if ('-' == dateString.c_str()[0]) { yearNeg = true; // Strip the -ve sign from the dateString dateString = (dateString.c_str() + (1 * sizeof(char))); } else throw ConversionError("The date elements do not match the formatting string elements"); } else { // Make sure the strings are the same length if (dateString.length() != fmt.length()) throw ConversionError("The date elements do not match the formatting string elements"); } // Try and obtain the elements as decribed in the format string int tmpVal; int y = -4713, m = 1, d = 1, hh = 0, mm = 0, ss = 0, zzz = 0; // yyyy if (-4713 == y && (tmpVal = convertDatePortion("yyyy", dateString, fmt)) != INVALID_FUNCTION_RES) y = tmpVal; // yy if (-4713 == y && (tmpVal = convertDatePortion("yy", dateString, fmt)) != INVALID_FUNCTION_RES) y = tmpVal; // mm if ((tmpVal = convertDatePortion("mm", dateString, fmt)) != INVALID_FUNCTION_RES) m = tmpVal; // dd if ((tmpVal = convertDatePortion("dd", dateString, fmt)) != INVALID_FUNCTION_RES) d = tmpVal; // hh if ((tmpVal = convertDatePortion("hh", dateString, fmt)) != INVALID_FUNCTION_RES) hh = tmpVal; // nn if ((tmpVal = convertDatePortion("nn", dateString, fmt)) != INVALID_FUNCTION_RES) mm = tmpVal; // ss if ((tmpVal = convertDatePortion("ss", dateString, fmt)) != INVALID_FUNCTION_RES) ss = tmpVal; // zzz if ((tmpVal = convertDatePortion("zzz", dateString, fmt)) != INVALID_FUNCTION_RES) zzz = tmpVal; // Convert the year to negative if needed if (yearNeg) y *= -1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -