Merge pull request #1139 from OpenSpace/issue/1122

Leap year fix for epoch used in both satellites and small solar system bodies. Closes #1122.
This commit is contained in:
Alexander Bock
2020-04-24 11:09:36 +02:00
committed by GitHub
2 changed files with 14 additions and 46 deletions

View File

@@ -60,7 +60,7 @@ namespace {
};
// Find the position of the current year in the vector; its position in
// the vector gives the number of leap seconds
// the vector gives the number of leap seconds
struct LeapSecond {
int year;
int dayOfYear;
@@ -202,13 +202,20 @@ namespace {
return nLeapSeconds;
}
int daysIntoGivenYear(int month, int dayOfMonth) {
//month and dayCount are zero-based. Does NOT account for leap year.
int daysIntoGivenYear(int year, int month, int dayOfMonth) {
//month and dayCount are zero-based.
month -= 1;
int dayCount = dayOfMonth - 1;
const int February = 1;
const bool isInLeapYear =
std::find(LeapYears.begin(), LeapYears.end(), year)
!= LeapYears.end();
for (int m = 0; m < month; ++m) {
dayCount += DaysOfMonths[m];
if (m == February && isInLeapYear) {
dayCount += 1;
}
}
return dayCount;
}
@@ -246,9 +253,7 @@ double RenderableOrbitalKepler::epochFromSubstring(const std::string& epochStrin
// The main overview of this function:
// 1. Reconstruct the full year from the YY part
// 2. Calculate the number of seconds since the beginning of the year
// 2.a Get the number of full days since the beginning of the year
// 2.b If the year is a leap year, modify the number of days
// 2. Calculate the number of days since the beginning of the year
// 3. Convert the number of days to a number of seconds
// 4. Get the number of leap seconds since January 1st, 2000 and remove them
// 5. Adjust for the fact the epoch starts on 1st Januaray at 12:00:00, not
@@ -267,18 +272,8 @@ double RenderableOrbitalKepler::epochFromSubstring(const std::string& epochStrin
const int daysSince2000 = countDays(year);
// 2.
// 2.a
double daysInYear = std::atof(epochString.substr(2).c_str());
// 2.b
const bool isInLeapYear =
std::find(LeapYears.begin(), LeapYears.end(), year) != LeapYears.end();
if (isInLeapYear && daysInYear >= 60) {
// We are in a leap year, so we have an effective day more if we are
// beyond the end of february (= 31+29 days)
--daysInYear;
}
// 3
using namespace std::chrono;
const int SecondsPerDay = static_cast<int>(seconds(hours(24)).count());
@@ -309,9 +304,7 @@ double RenderableOrbitalKepler::epochFromYMDdSubstring(const std::string& epochS
// The main overview of this function:
// 1. Read the year value
// 2. Calculate the number of seconds since the beginning of the year
// 2.a Get the number of full days since the beginning of the year
// 2.b If the year is a leap year, modify the number of days
// 2. Calculate the number of days since the beginning of the year
// 3. Convert the number of days to a number of seconds
// 4. Get the number of leap seconds since January 1st, 2000 and remove them
// 5. Adjust for the fact the epoch starts on 1st January at 12:00:00, not
@@ -322,22 +315,12 @@ double RenderableOrbitalKepler::epochFromYMDdSubstring(const std::string& epochS
const int daysSince2000 = countDays(year);
// 2.
// 2.a
int monthNum = std::atoi(epochString.substr(4, 2).c_str());
int dayOfMonthNum = std::atoi(epochString.substr(6, 2).c_str());
int wholeDaysInto = daysIntoGivenYear(monthNum, dayOfMonthNum);
int wholeDaysInto = daysIntoGivenYear(year, monthNum, dayOfMonthNum);
double fractionOfDay = std::atof(epochString.substr(9, 7).c_str());
double daysInYear = static_cast<double>(wholeDaysInto) + fractionOfDay;
// 2.b
const bool isInLeapYear =
std::find(LeapYears.begin(), LeapYears.end(), year) != LeapYears.end();
if (isInLeapYear && daysInYear >= 60) {
// We are in a leap year, so we have an effective day more if we are
// beyond the end of february (= 31+29 days)
--daysInYear;
}
// 3
using namespace std::chrono;
const int SecondsPerDay = static_cast<int>(seconds(hours(24)).count());

View File

@@ -145,9 +145,7 @@ namespace {
// The main overview of this function:
// 1. Reconstruct the full year from the YY part
// 2. Calculate the number of seconds since the beginning of the year
// 2.a Get the number of full days since the beginning of the year
// 2.b If the year is a leap year, modify the number of days
// 2. Calculate the number of days since the beginning of the year
// 3. Convert the number of days to a number of seconds
// 4. Get the number of leap seconds since January 1st, 2000 and remove them
// 5. Adjust for the fact the epoch starts on 1st Januaray at 12:00:00, not
@@ -168,21 +166,8 @@ namespace {
const int daysSince2000 = countDays(year);
// 2.
// 2.a
double daysInYear = std::atof(epochString.substr(2).c_str());
// 2.b
const bool isInLeapYear = std::find(
LeapYears.begin(),
LeapYears.end(),
year
) != LeapYears.end();
if (isInLeapYear && daysInYear >= 60) {
// We are in a leap year, so we have an effective day more if we are
// beyond the end of february (= 31+29 days)
--daysInYear;
}
// 3
using namespace std::chrono;
const int SecondsPerDay = static_cast<int>(seconds(hours(24)).count());