StelUtils.cpp   StelUtils.cpp 
skipping to change at line 20 skipping to change at line 20
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, U SA. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, U SA.
*/ */
#include <config.h>
#include <cmath> // std::fmod #include <cmath> // std::fmod
#ifdef CYGWIN #ifdef CYGWIN
#include <malloc.h> #include <malloc.h>
#endif #endif
#include "StelUtils.hpp" #include "StelUtils.hpp"
#include "VecMath.hpp" #include "VecMath.hpp"
#include <QtOpenGL> #include <QtOpenGL>
#include <QString> #include <QString>
skipping to change at line 44 skipping to change at line 42
#include <QFile> #include <QFile>
#include <QDebug> #include <QDebug>
#include <QLocale> #include <QLocale>
#include <QRegExp> #include <QRegExp>
namespace StelUtils namespace StelUtils
{ {
//! Return the full name of stellarium, i.e. "stellarium 0.9.0" //! Return the full name of stellarium, i.e. "stellarium 0.9.0"
QString getApplicationName() QString getApplicationName()
{ {
#ifdef SVN_REVISION return QString("Stellarium")+" "+StelUtils::getApplicationVersion();
return QString("Stellarium")+" "+PACKAGE_VERSION+" (SVN r"+SVN_REVIS
ION+")";
#else
return QString("Stellarium")+" "+PACKAGE_VERSION;
#endif
} }
//! Return the version of stellarium, i.e. "0.9.0" //! Return the version of stellarium, i.e. "0.9.0"
QString getApplicationVersion() QString getApplicationVersion()
{ {
#ifdef SVN_REVISION #ifdef SVN_REVISION
return QString(PACKAGE_VERSION)+" (SVN r"+SVN_REVISION+")"; return QString(PACKAGE_VERSION)+" (SVN r"+SVN_REVISION+")";
#else #else
return QString(PACKAGE_VERSION); return QString(PACKAGE_VERSION);
#endif #endif
skipping to change at line 330 skipping to change at line 324
void rectToSphe(float *lng, float *lat, const Vec3d& v) void rectToSphe(float *lng, float *lat, const Vec3d& v)
{ {
double r = v.length(); double r = v.length();
*lat = asin(v[2]/r); *lat = asin(v[2]/r);
*lng = atan2(v[1],v[0]); *lng = atan2(v[1],v[0]);
} }
void rectToSphe(float *lng, float *lat, const Vec3f& v) void rectToSphe(float *lng, float *lat, const Vec3f& v)
{ {
double r = v.length(); float r = v.length();
*lat = asin(v[2]/r); *lat = asin(v[2]/r);
*lng = atan2(v[1],v[0]); *lng = atan2(v[1],v[0]);
} }
void rectToSphe(double *lng, double *lat, const Vec3f& v) void rectToSphe(double *lng, double *lat, const Vec3f& v)
{ {
double r = v.length(); double r = v.length();
*lat = asin(v[2]/r); *lat = asin(v[2]/r);
*lng = atan2(v[1],v[0]); *lng = atan2(v[1],v[0]);
} }
double getDecAngle(const QString& str) double getDecAngle(const QString& str)
{ {
QRegExp re1("^\\s*([\\+\\-])?\\s*(\\d+)\\s*([hHDd\xBA])\\s*(\\d+)\\s *['Mm]\\s*(\\d+(\\.\\d+)?)\\s*[\"Ss]\\s*([NSEWnsew])?\\s*$"); // DMS/HMS QRegExp re1("^\\s*([\\+\\-])?\\s*(\\d+)\\s*([hHDd\xBA])\\s*(\\d+)\\s *['Mm]\\s*(\\d+(\\.\\d+)?)\\s*[\"Ss]\\s*([NSEWnsew])?\\s*$"); // DMS/HMS
QRegExp re2("^\\s*([\\+\\-])?\\s*(\\d+(\\.\\d+)?).?([NSEWnsew])?\\s* $"); // Decimal QRegExp re2("^\\s*([\\+\\-])?\\s*(\\d+(\\.\\d+)?).?([NSEWnsew])?\\s* $"); // Decimal
if (re1.exactMatch(str)) if (re1.exactMatch(str))
{ {
bool neg = (re1.capturedTexts().at(1) == "-"); bool neg = (re1.capturedTexts().at(1) == "-");
double d = re1.capturedTexts().at(2).toDouble(); float d = re1.capturedTexts().at(2).toFloat();
double m = re1.capturedTexts().at(4).toDouble(); float m = re1.capturedTexts().at(4).toFloat();
double s = re1.capturedTexts().at(5).toDouble(); double s = re1.capturedTexts().at(5).toDouble();
if (re1.capturedTexts().at(3).toUpper() == "H") if (re1.capturedTexts().at(3).toUpper() == "H")
{ {
d *= 15; d *= 15;
m *= 15; m *= 15;
s *= 15; s *= 15;
} }
QString cardinal = re1.capturedTexts().at(7); QString cardinal = re1.capturedTexts().at(7);
double deg = d + (m/60) + (s/3600); double deg = d + (m/60) + (s/3600);
if (cardinal.toLower() == "s" || cardinal.toLower() == "w" | | neg) if (cardinal.toLower() == "s" || cardinal.toLower() == "w" | | neg)
skipping to change at line 473 skipping to change at line 467
void getTimeFromJulianDay(double julianDay, int *hour, int *minute, int *se cond) void getTimeFromJulianDay(double julianDay, int *hour, int *minute, int *se cond)
{ {
double frac = julianDay - (floor(julianDay)); double frac = julianDay - (floor(julianDay));
int s = (int)floor(frac * 24 * 60 * 60); int s = (int)floor(frac * 24 * 60 * 60);
*hour = ((s / (60 * 60))+12)%24; *hour = ((s / (60 * 60))+12)%24;
*minute = (s/(60))%60; *minute = (s/(60))%60;
*second = s % 60; *second = s % 60;
} }
QString sixIntsToIsoString( int year, int month, int day, int hour, int min ute, int second ) QString julianDayToISO8601String(double jd)
{ {
// formatting a negative doesnt work the way i expect int year, month, day, hour, minute, second;
getDateFromJulianDay(jd, &year, &month, &day);
getTimeFromJulianDay(jd, &hour, &minute, &second);
QString dt = QString("%1-%2-%3T%4:%5:%6") QString res = QString("%1-%2-%3T%4:%5:%6")
.arg((year >= 0 ? year : -1* year),4,10,QLa tin1Char('0')) .arg((year >= 0 ? year : -1* year),4,10,QLa tin1Char('0'))
.arg(month,2,10,QLatin1Char('0')) .arg(month,2,10,QLatin1Char('0'))
.arg(day,2,10,QLatin1Char('0')) .arg(day,2,10,QLatin1Char('0'))
.arg(hour,2,10,QLatin1Char('0')) .arg(hour,2,10,QLatin1Char('0'))
.arg(minute,2,10,QLatin1Char('0')) .arg(minute,2,10,QLatin1Char('0'))
.arg(second,2,10,QLatin1Char('0')); .arg(second,2,10,QLatin1Char('0'));
if (year < 0) if (year < 0)
{ {
dt.prepend("-"); res.prepend("-");
} }
return dt; return res;
}
QString jdToIsoString(double jd)
{
int year, month, day, hour, minute, second;
getDateFromJulianDay(jd, &year, &month, &day);
getTimeFromJulianDay(jd, &hour, &minute, &second);
return sixIntsToIsoString(year, month, day, hour, minute, second);
} }
// Format the date per the fmt. // Format the date per the fmt.
QString localeDateString(int year, int month, int day, int dayOfWeek, QStri ng fmt) QString localeDateString(int year, int month, int day, int dayOfWeek, QStri ng fmt)
{ {
/* we have to handle the year zero, and the years before qdatetime c an represent. */ /* we have to handle the year zero, and the years before qdatetime c an represent. */
const QLatin1Char quote('\''); const QLatin1Char quote('\'');
QString out; QString out;
int quotestartedat = -1; int quotestartedat = -1;
skipping to change at line 638 skipping to change at line 624
} }
else else
{ {
return localeDateString(year,month,day,dayOfWeek,QLocale().d ateFormat(QLocale::ShortFormat)); return localeDateString(year,month,day,dayOfWeek,QLocale().d ateFormat(QLocale::ShortFormat));
} }
} }
//! use QDateTime to get a Julian Date from the system's current time. //! use QDateTime to get a Julian Date from the system's current time.
//! this is an acceptable use of QDateTime because the system's current //! this is an acceptable use of QDateTime because the system's current
//! time is more than likely always going to be expressible by QDateTime. //! time is more than likely always going to be expressible by QDateTime.
double getJDFromSystem(void) double getJDFromSystem()
{ {
return qDateTimeToJd(QDateTime::currentDateTime().toUTC()); return qDateTimeToJd(QDateTime::currentDateTime().toUTC());
} }
double qTimeToJDFraction(const QTime& time) double qTimeToJDFraction(const QTime& time)
{ {
return (double)1./(24*60*60*1000)*QTime().msecsTo(time)-0.5; return (double)1./(24*60*60*1000)*QTime().msecsTo(time)-0.5;
} }
QTime jdFractionToQTime(const double jd) QTime jdFractionToQTime(double jd)
{ {
double decHours = std::fmod(jd+0.5, 1.0); double decHours = std::fmod(jd+0.5, 1.0);
int hours = (int)(decHours/0.041666666666666666666); int hours = (int)(decHours/0.041666666666666666666);
int mins = (int)((decHours-(hours*0.041666666666666666666))/0.000694 44444444444444444); int mins = (int)((decHours-(hours*0.041666666666666666666))/0.000694 44444444444444444);
return QTime::fromString(QString("%1.%2").arg(hours).arg(mins), "h.m "); return QTime::fromString(QString("%1.%2").arg(hours).arg(mins), "h.m ");
} }
// Use Qt's own sense of time and offset instead of platform specific code. // Use Qt's own sense of time and offset instead of platform specific code.
float getGMTShiftFromQT(double JD) float getGMTShiftFromQT(double JD)
{ {
int year, month, day, hour, minute, second; int year, month, day, hour, minute, second;
getDateFromJulianDay(JD, &year, &month, &day); getDateFromJulianDay(JD, &year, &month, &day);
getTimeFromJulianDay(JD, &hour, &minute, &second); getTimeFromJulianDay(JD, &hour, &minute, &second);
// as analogous to second statement in getJDFromDate, nkerr // as analogous to second statement in getJDFromDate, nkerr
if ( year <= 0 ) if ( year <= 0 )
{ {
year = year - 1; year = year - 1;
} }
QDateTime current(QDate(year, month, day), QTime(hour, minute, secon //getTime/DateFromJulianDay returns UTC time, not local time
d)); QDateTime universal(QDate(year, month, day), QTime(hour, minute, sec
if (! current.isValid()) ond), Qt::UTC);
if (! universal.isValid())
{ {
//qWarning() << "JD " << QString("%1").arg(JD) << " out of b ounds of QT help with GMT shift, using current datetime"; //qWarning() << "JD " << QString("%1").arg(JD) << " out of b ounds of QT help with GMT shift, using current datetime";
// Assumes the GMT shift was always the same before year -47 10 // Assumes the GMT shift was always the same before year -47 10
current = QDateTime(QDate(-4710, month, day), QTime(hour, mi nute, second)); universal = QDateTime(QDate(-4710, month, day), QTime(hour, minute, second), Qt::UTC);
} }
QDateTime local = universal.toLocalTime();
//Both timezones should be set to UTC because secsTo() converts both //Both timezones should be interpreted as UTC because secsTo() conve
rts both
//times to UTC if their zones have different daylight saving time ru les. //times to UTC if their zones have different daylight saving time ru les.
QDateTime local = current; local.setTimeSpec(Qt::UTC); local.setTimeSpec(Qt::UTC);
QDateTime universal = current.toUTC();
int shiftInSeconds = universal.secsTo(local); int shiftInSeconds = universal.secsTo(local);
float shiftInHours = shiftInSeconds / 3600.0f; float shiftInHours = shiftInSeconds / 3600.0f;
return shiftInHours; return shiftInHours;
} }
// UTC ! // UTC !
bool getJDFromDate(double* newjd, int y, int m, int d, int h, int min, int s) bool getJDFromDate(double* newjd, int y, int m, int d, int h, int min, int s)
{ {
double deltaTime = (h / 24.0) + (min / (24.0*60.0)) + (s / (24.0 * 6 0.0 * 60.0)) - 0.5; double deltaTime = (h / 24.0) + (min / (24.0*60.0)) + (s / (24.0 * 6 0.0 * 60.0)) - 0.5;
skipping to change at line 906 skipping to change at line 892
qDebug() << indent + key + "(list):"; qDebug() << indent + key + "(list):";
foreach(QVariant item, m.toList()) foreach(QVariant item, m.toList())
{ {
debugQVariantMap(item, indent + " "); debugQVariantMap(item, indent + " ");
} }
} }
else else
qDebug() << indent + key + " => " + m.toString(); qDebug() << indent + key + " => " + m.toString();
} }
QList<int> getIntsFromISO8601String(const QString & dt) double getJulianDayFromISO8601String(const QString& iso8601Date, bool* ok)
{ {
// Represents a valid, complete date string. int y, m, d, h, min;
static QRegExp finalRe("(-0*[1-9][0-9]{0,5}|0+|0*[1-9][0-9]{0,5})-(0 float s;
[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[10])[T ]([01][0-9]|2[0123]):([012345][0-9 *ok = getDateTimeFromISO8601String(iso8601Date, &y, &m, &d, &h, &min
]):([012345][0-9])"); , &s);
if (*ok)
QList<int> retval;
if (finalRe.exactMatch(dt))
{ {
QStringList number_strings = finalRe.capturedTexts(); double jd;
bool ok; if (!StelUtils::getJDFromDate(&jd, y, m, d, h, min, s))
int v;
for (int i = 1; i < number_strings.size(); i++)
{ {
qWarning() << ":: at capture " << i << " got a " << *ok = false;
number_strings[i]; return 0.0;
ok = true;
v = number_strings[i].toInt(&ok, 10);
qWarning() << " :: and it was a " << v << " " << ok
;
if (ok)
{
retval.push_back(v);
}
else
{
retval.clear();
qWarning() << "StelUtils::getIntsFromISO8601
String: input string failed to be an exact date at capture " << i << ", ret
urning nothing: " << dt;
break;
}
} }
return jd;
} }
else return 0.0;
}
bool getDateTimeFromISO8601String(const QString& iso8601Date, int* y, int*
m, int* d, int* h, int* min, float* s)
{
// Represents an ISO8601 complete date string.
QRegExp finalRe("^([+\\-]?\\d+)[:\\-](\\d\\d)[:\\-](\\d\\d)T(\\d?\\d
):(\\d\\d):(\\d\\d(?:\\.\\d*)?)$");
if (finalRe.exactMatch(iso8601Date) && finalRe.captureCount()==6)
{ {
qWarning() << "StelUtils::getIntsFromISO8601String: input st bool error = false;
ring failed to be an exact date, returning nothing: " << dt; bool ok;
*y = finalRe.capturedTexts().at(1).toInt(&ok);
error = error || !ok;
*m = finalRe.capturedTexts().at(2).toInt(&ok);
error = error || !ok;
*d = finalRe.capturedTexts().at(3).toInt(&ok);
error = error || !ok;
*h = finalRe.capturedTexts().at(4).toInt(&ok);
error = error || !ok;
*min = finalRe.capturedTexts().at(5).toInt(&ok);
error = error || !ok;
*s = finalRe.capturedTexts().at(6).toFloat(&ok);
error = error || !ok;
if (!error)
return true;
} }
return retval; return false;
} }
} // end of the StelUtils namespace } // end of the StelUtils namespace
 End of changes. 24 change blocks. 
70 lines changed or deleted 60 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/