Added support for date strings for Epoch, Beginning, and Ending fields in .ssc files.

ver1_5_1
Chris Laurel 2002-10-28 05:21:16 +00:00
parent 62c9991290
commit ad765ed38d
3 changed files with 75 additions and 3 deletions

View File

@ -367,6 +367,53 @@ astro::Date::operator double() const
}
bool astro::parseDate(const string& s, astro::Date& date)
{
int year = 0;
unsigned int month = 1;
unsigned int day = 1;
unsigned int hour = 0;
unsigned int minute = 0;
unsigned int second = 0;
if (sscanf(s.c_str(), " %d %u %u %u:%u:%u ",
&year, &month, &day, &hour, &minute, &second) == 6 ||
sscanf(s.c_str(), " %d %u %u %u:%u ",
&year, &month, &day, &hour, &minute, &second) == 5 ||
sscanf(s.c_str(), " %d %u %u ", &year, &month, &day) == 3)
{
if (month < 1 || month > 12)
return false;
if (hour > 23 || minute > 59 || second > 59)
return false;
// Cheesy days/month calculation . . .
int maxDay = 31 - ((0xa50 >> month) & 0x1);
if (month == 2)
{
// Check for a leap year
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
maxDay = 29;
else
maxDay = 28;
}
if (day > maxDay || day < 1)
return false;
date.year = year;
date.month = month;
date.day = day;
date.hour = hour;
date.minute = minute;
date.seconds = second;
return true;
}
return false;
}
ostream& operator<<(ostream& s, const astro::Date d)
{
s << d.year << ' ' << setw(2) << setfill('0') << d.month << ' ';

View File

@ -40,6 +40,8 @@ namespace astro
double seconds;
};
bool parseDate(const std::string&, Date&);
enum CoordinateSystem
{
Universal = 0,

View File

@ -9,6 +9,7 @@
#include <cassert>
// #include <limits>
#include <cstdio>
#ifndef _WIN32
#ifndef MACOSX_PB
@ -19,6 +20,7 @@
#include <celutil/debug.h>
#include <celmath/mathlib.h>
#include <celutil/util.h>
#include <cstdio>
#include "astro.h"
#include "parser.h"
#include "customorbit.h"
@ -31,6 +33,27 @@
using namespace std;
bool getDate(Hash* hash, const string& name, double& jd)
{
// Check first for a number value representing a Julian date
if (hash->getNumber(name, jd))
return true;
string dateString;
if (hash->getString(name, dateString))
{
astro::Date date(1, 1, 1);
if (astro::parseDate(dateString, date))
{
jd = (double) date;
return true;
}
}
return false;
}
static Surface* CreateSurface(Hash* surfaceData)
{
Surface* surface = new Surface();
@ -145,7 +168,7 @@ static EllipticalOrbit* CreateEllipticalOrbit(Hash* orbitData,
}
double epoch = astro::J2000;
orbitData->getNumber("Epoch", epoch);
getDate(orbitData, "Epoch", epoch);
// Accept either the mean anomaly or mean longitude--use mean anomaly
// if both are specified.
@ -322,8 +345,8 @@ static Body* CreatePlanet(PlanetarySystem* system,
// double ending = numeric_limits<double>::infinity();
double beginning = -1.0e+50;
double ending = 1.0e+50;
planetData->getNumber("Beginning", beginning);
planetData->getNumber("Ending", ending);
getDate(planetData, "Beginning", beginning);
getDate(planetData, "Ending", ending);
body->setLifespan(beginning, ending);
string infoURL;