KSComet::KSComet( KStarsData *_kd, QString _s, QString imfile, long double _JD, double _q, double _e, dms _i, dms _w, dms _Node, double Tp ) : KSPlanetBase(_kd, _s, imfile), kd(_kd), JD(_JD), q(_q), e(_e), i(_i), w(_w), N(_Node) { setType( 9 ); //Comet //Find the Julian Day of Perihelion from Tp //Tp is a double which encodes a date like: YYYYMMDD.DDDDD (e.g., 19730521.33333 int year = int( Tp/10000.0 ); int month = int( (int(Tp) % 10000)/100.0 ); int day = int( int(Tp) % 100 ); double Hour = 24.0 * ( Tp - int(Tp) ); int h = int( Hour ); int m = int( 60.0 * ( Hour - h ) ); int s = int( 60.0 * ( 60.0 * ( Hour - h) - m ) ); JDp = KStarsDateTime( ExtDate( year, month, day ), QTime( h, m, s ) ).djd(); //compute the semi-major axis, a: a = q/(1.0-e); //Compute the orbital Period from Kepler's 3rd law: P = 365.2568984 * pow(a, 1.5); //period in days //If the name contains a "/", make this name2 and make name a truncated version without the leading "P/" or "C/" if ( name().contains( "/" ) ) { setLongName( name() ); setName( name().mid( name().find("/") + 1 ) ); } }
//Try both DateFormat values ExtDate ExtDate::fromString( const QString& s ) { ExtDate dResult = ExtDate::fromString( s, Qt::TextDate ); if ( dResult.isValid() ) return dResult; dResult = ExtDate::fromString( s, Qt::ISODate ); if ( dResult.isValid() ) return dResult; else return ExtDate(); //invalid }
ExtDate ExtDate::addMonths( int months ) const { int a_month = month() + months%12; int a_year = year() + int(months/12); while ( a_month < 1 ) { a_month += 12; a_year--; } while ( a_month > 12 ) { a_month -= 12; a_year++; } return ExtDate(a_year, a_month, day()); }
ExtDate timeBox::createDate (bool */*ok*/) { QString entry = text().stripWhiteSpace(); // if entry is an empty string or invalid date use current date ExtDate date = ExtDate().fromString(entry); if ( !date.isValid() ) { kdDebug() << k_funcinfo << "Invalid date" << endl; showDate(ExtDate::currentDate()); entry = text().stripWhiteSpace(); return ExtDate::currentDate(); } else { return date; } }
ExtDateTime ExtDateTime::addSecs( int nsecs ) const { long int dd = d.jd(); int tt = MSECS_PER_HOUR*t.hour() + MSECS_PER_MIN*t.minute() + 1000*t.second() + t.msec(); tt += nsecs*1000; while ( tt < 0 ) { tt += MSECS_PER_DAY; --dd; } while ( tt > int(MSECS_PER_DAY) ) { tt -= MSECS_PER_DAY; ++dd; } ExtDateTime ret; ret.setTime( QTime().addMSecs( tt ) ); ret.setDate( ExtDate( dd ) ); return ret; }
ExtDate ExtDate::currentDate(Qt::TimeSpec ts) { time_t a_current_time; struct tm a_current_time_tm; time(&a_current_time); switch (ts) { case Qt::LocalTime : localtime_r(&a_current_time, &a_current_time_tm); break; case Qt::UTC : gmtime_r(&a_current_time, &a_current_time_tm); break; default : assert(0); break; } return ExtDate(a_current_time_tm.tm_year + 1900, a_current_time_tm.tm_mon + 1, a_current_time_tm.tm_mday); }
ExtDate ExtDate::fromString( const QString& s, Qt::DateFormat f ) { ExtDate dt = ExtDate(); //initialize invalid date if ( s.isEmpty() ) { return dt; } if ( f == Qt::LocalDate ) { //can't use LocalFormat #if defined(QT_CHECK_RANGE) qWarning( "QDate::fromString: Parameter out of range" ); #endif return dt; } switch( f ) { case Qt::ISODate : { int year( s.mid( 0, 4 ).toInt() ); int month( s.mid( 5, 2 ).toInt() ); int day( s.mid( 8, 2 ).toInt() ); if ( year && month && day ) return ExtDate( year, month, day ); } break; default : #ifndef QT_NO_TEXTDATE case Qt::TextDate : { //Three possible date formats: //dd mth yyyy; mth dd yyyy; wkd mth dd yyyy //"mth" is the word for the month (long or short form) QStringList ss = QStringList::split( " ", s ); bool ok = false; int month = -1; uint imonth = 0; uint iyear = 0; //If neither of the first two words is a number, then we'll assume //the first word is a superfluous "weekday" string int day = ss[0].toInt( &ok ); if ( ! ok ) { day = ss[1].toInt( &ok ); if ( ! ok ) { day = ss[2].toInt( &ok ); if ( !ok ) return dt; //could not find a valid day number in first three words imonth = 1; //the month must be the second word iyear = 3; //the year must be the fourth word } else { //the month is either the first word, or the third. imonth = 0; iyear = 2; } } else { //month is the second word imonth = 1; iyear = 2; } for ( uint i = 0; i < 12; i++ ) { if ( ss[imonth] == shortMonthName( i+1 ) || ss[imonth] == longMonthName( i+1 ) ) { month = i + 1; break; } } if ( month == -1 && imonth == 0 ) { //try the third word imonth = 2; iyear = 3; for ( uint i = 0; i < 12; i++ ) { if ( ss[imonth] == shortMonthName( i+1 ) || ss[imonth] == longMonthName( i+1 ) ) { month = i + 1; break; } } } if ( month > -1 ) ok = true; if ( ! ok ) return dt; //could not parse month; return invalid int year = ss[iyear].toInt( &ok ); if ( ! ok ) return dt; //could not parse year; return invalid return ExtDate( year, month, day ); break; } #else break; #endif //ifndef QT_NO_TEXTDATE } return dt; }
ExtDate ExtDate::addYears( int years ) const { return ExtDate(year() + years, month(), day()); }