~ubuntu-branches/debian/jessie/stellarium/jessie

« back to all changes in this revision

Viewing changes to src/stelutils/StelUtils.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cédric Delfosse
  • Date: 2009-03-13 20:07:22 UTC
  • mfrom: (1.1.8 upstream)
  • mto: (11.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: james.westby@ubuntu.com-20090313200722-gbgujsmzsa8a02ty
Import upstream version 0.10.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Stellarium
3
 
 * Copyright (C) 2002 Fabien Chereau
4
 
 * 
5
 
 * This program is free software; you can redistribute it and/or
6
 
 * modify it under the terms of the GNU General Public License
7
 
 * as published by the Free Software Foundation; either version 2
8
 
 * of the License, or (at your option) any later version.
9
 
 * 
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 * 
15
 
 * You should have received a copy of the GNU General Public License
16
 
 * along with this program; if not, write to the Free Software
17
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
 
 */
19
 
 
20
 
#include <config.h>
21
 
 
22
 
#include <cmath> // std::fmod
23
 
 
24
 
#ifdef CYGWIN
25
 
 #include <malloc.h>
26
 
#endif
27
 
 
28
 
#include "StelUtils.hpp"
29
 
#include "vecmath.h"
30
 
#include "GLee.h"
31
 
 
32
 
#include "fixx11h.h"
33
 
#include <QString>
34
 
#include <QStringList>
35
 
#include <QTextStream>
36
 
#include <QFile>
37
 
#include <QDebug>
38
 
#include <QLocale>
39
 
 
40
 
namespace StelUtils
41
 
{
42
 
 
43
 
double hmsToRad(unsigned int h, unsigned int m, double s )
44
 
{
45
 
        return (double)M_PI/24.*h*2.+(double)M_PI/12.*m/60.+s*M_PI/43200.;
46
 
}
47
 
 
48
 
double dmsToRad(int d, unsigned int m, double s)
49
 
{
50
 
        if (d>=0)
51
 
                return (double)M_PI/180.*d+(double)M_PI/10800.*m+s*M_PI/648000.;
52
 
        return (double)M_PI/180.*d-(double)M_PI/10800.*m-s*M_PI/648000.;
53
 
}
54
 
 
55
 
/*************************************************************************
56
 
 Convert an angle in radian to hms
57
 
*************************************************************************/
58
 
void radToHms(double angle, unsigned int& h, unsigned int& m, double& s)
59
 
{
60
 
        angle = std::fmod(angle,2.0*M_PI);
61
 
        if (angle < 0.0) angle += 2.0*M_PI; // range: [0..2.0*M_PI)
62
 
                
63
 
        angle *= 12./M_PI;
64
 
 
65
 
        h = (unsigned int)angle;
66
 
        m = (unsigned int)((angle-h)*60);
67
 
        s = (angle-h)*3600.-60.*m;
68
 
}
69
 
 
70
 
/*************************************************************************
71
 
 Convert an angle in radian to dms
72
 
*************************************************************************/
73
 
void radToDms(double angle, bool& sign, unsigned int& d, unsigned int& m, double& s)
74
 
{
75
 
        angle = std::fmod(angle,2.0*M_PI);
76
 
        sign=true;
77
 
        if (angle<0)
78
 
        {
79
 
                angle *= -1;
80
 
                sign = false;
81
 
        }
82
 
        angle *= 180./M_PI;
83
 
        
84
 
        d = (unsigned int)angle;
85
 
        m = (unsigned int)((angle - d)*60);
86
 
        s = (angle-d)*3600-60*m;
87
 
}
88
 
 
89
 
/*************************************************************************
90
 
 Convert an angle in radian to a hms formatted string
91
 
 If the minute and second part are null are too small, don't print them
92
 
*************************************************************************/
93
 
QString radToHmsStrAdapt(double angle)
94
 
{
95
 
        unsigned int h,m;
96
 
        double s;
97
 
        QString buf;
98
 
        QTextStream ts(&buf);
99
 
        StelUtils::radToHms(angle+0.005*M_PI/12/(60*60), h, m, s);
100
 
        ts << h << 'h';
101
 
        if (std::fabs(s*100-(int)s*100)>=1)
102
 
        {
103
 
                ts << m << 'm';
104
 
                ts.setRealNumberNotation(QTextStream::FixedNotation);
105
 
                ts.setPadChar('0');
106
 
                ts.setFieldWidth(4);
107
 
                ts.setRealNumberPrecision(1);
108
 
                ts << s;
109
 
                ts.reset();
110
 
                ts << 's';
111
 
        }
112
 
        else if ((int)s!=0)
113
 
        {
114
 
                ts << m << 'm' << (int)s << 's';
115
 
        }
116
 
        else if (m!=0)
117
 
        {
118
 
                ts << m << 'm';
119
 
        }
120
 
        return buf;
121
 
}
122
 
 
123
 
/*************************************************************************
124
 
 Convert an angle in radian to a hms formatted string
125
 
 If decimal is true,  output should be like this: "  16h29m55.3s"
126
 
 If decimal is true,  output should be like this: "  16h20m0.4s"
127
 
 If decimal is false, output should be like this: "0h26m5s"
128
 
*************************************************************************/
129
 
QString radToHmsStr(double angle, bool decimal)
130
 
{
131
 
        unsigned int h,m;
132
 
        double s;
133
 
        StelUtils::radToHms(angle+0.005*M_PI/12/(60*60), h, m, s);
134
 
        int width, precision;
135
 
        QString carry;
136
 
        if (decimal)
137
 
        {
138
 
                width=4;
139
 
                precision=1;
140
 
                carry="60.0";
141
 
        }
142
 
        else
143
 
        {
144
 
                width=2;
145
 
                precision=0;
146
 
                carry="60";
147
 
        }
148
 
 
149
 
        // handle carry case (when seconds are rounded up)
150
 
        if (QString("%1").arg(s, 0, 'f', precision) == carry)
151
 
        {
152
 
                s=0;
153
 
                m+=1;
154
 
        }
155
 
        if (m==60)
156
 
        {
157
 
                m=0;
158
 
                h+=1;
159
 
        }
160
 
        if (h==24 && m==0 && s==0) 
161
 
                h=0;
162
 
 
163
 
        return QString("%1h%2m%3s").arg(h, width).arg(m).arg(s, 0, 'f', precision);
164
 
}
165
 
 
166
 
/*************************************************************************
167
 
 Convert an angle in radian to a dms formatted string
168
 
 If the minute and second part are null are too small, don't print them
169
 
*************************************************************************/
170
 
QString radToDmsStrAdapt(double angle, bool useD)
171
 
{
172
 
        QChar degsign('d');
173
 
        if (!useD)
174
 
        {
175
 
                degsign = 0x00B0;
176
 
        }
177
 
        bool sign;
178
 
        unsigned int d,m;
179
 
        double s;
180
 
        StelUtils::radToDms(angle+0.005*M_PI/180/(60*60)*(angle<0?-1.:1.), sign, d, m, s);
181
 
        QString str;
182
 
        QTextStream os(&str);
183
 
        
184
 
        os << (sign?'+':'-') << d << degsign;
185
 
        if (std::fabs(s*100-(int)s*100)>=1)
186
 
        {
187
 
                os << m << '\'' << fixed << qSetRealNumberPrecision(2) << qSetFieldWidth(5) << qSetPadChar('0') << s << qSetFieldWidth(0) << '\"';
188
 
        }
189
 
        else if ((int)s!=0)
190
 
        {
191
 
                os << m << '\'' << (int)s << '\"';
192
 
        }
193
 
        else if (m!=0)
194
 
        {
195
 
                os << m << '\'';
196
 
        }
197
 
        //qDebug() << "radToDmsStrAdapt(" << angle << ", " << useD << ") = " << str;
198
 
        return str;
199
 
}
200
 
 
201
 
 
202
 
/*************************************************************************
203
 
 Convert an angle in radian to a dms formatted string
204
 
*************************************************************************/
205
 
QString radToDmsStr(double angle, bool decimal, bool useD)
206
 
{
207
 
        QChar degsign('d');
208
 
        if (!useD)
209
 
        {
210
 
                degsign = 0x00B0;
211
 
        }
212
 
        bool sign;
213
 
        unsigned int d,m;
214
 
        double s;
215
 
        StelUtils::radToDms(angle+0.005*M_PI/180/(60*60)*(angle<0?-1.:1.), sign, d, m, s);
216
 
        QString str;
217
 
        QTextStream os(&str);
218
 
        os << (sign?'+':'-') << d << degsign;
219
 
 
220
 
        int width = 2;
221
 
        if (decimal)
222
 
        {
223
 
                os << qSetRealNumberPrecision(1);
224
 
                width = 4;
225
 
        }
226
 
        else
227
 
        {
228
 
                os << qSetRealNumberPrecision(0);
229
 
                width = 2;
230
 
        }
231
 
                
232
 
        os << qSetFieldWidth(width) << m << qSetFieldWidth(0) << '\'' 
233
 
                << fixed << qSetFieldWidth(width) << qSetPadChar('0') << s 
234
 
                << qSetFieldWidth(0) << '\"';
235
 
 
236
 
        return str;
237
 
}
238
 
 
239
 
// Obtains a Vec3f from a string with the form x,y,z
240
 
Vec3f strToVec3f(const QStringList& s)
241
 
{
242
 
        if (s.size()<3)
243
 
                 return Vec3f(0.f,0.f,0.f);
244
 
 
245
 
        return Vec3f(s[0].toFloat(),s[1].toFloat(),s[2].toFloat());
246
 
}
247
 
 
248
 
Vec3f strToVec3f(const QString& s)
249
 
{
250
 
        return strToVec3f(s.split(","));
251
 
}
252
 
 
253
 
// Converts a Vec3f to HTML color notation.
254
 
QString vec3fToHtmlColor(const Vec3f& v)
255
 
{
256
 
        return QString("#%1%2%3")
257
 
                .arg(qMin(255, int(v[0] * 255)), 2, 16, QChar('0'))
258
 
                .arg(qMin(255, int(v[1] * 255)), 2, 16, QChar('0'))
259
 
                .arg(qMin(255, int(v[2] * 255)), 2, 16, QChar('0'));
260
 
}
261
 
 
262
 
void spheToRect(double lng, double lat, Vec3d& v)
263
 
{
264
 
        const double cosLat = cos(lat);
265
 
        v.set(cos(lng) * cosLat, sin(lng) * cosLat, sin(lat));
266
 
}
267
 
 
268
 
void spheToRect(float lng, float lat, Vec3f& v)
269
 
{
270
 
        const double cosLat = cos(lat);
271
 
        v.set(cos(lng) * cosLat, sin(lng) * cosLat, sin(lat));
272
 
}
273
 
 
274
 
void rectToSphe(double *lng, double *lat, const Vec3d& v)
275
 
{
276
 
        double r = v.length();
277
 
        *lat = asin(v[2]/r);
278
 
        *lng = atan2(v[1],v[0]);
279
 
}
280
 
 
281
 
void rectToSphe(float *lng, float *lat, const Vec3d& v)
282
 
{
283
 
        double r = v.length();
284
 
        *lat = asin(v[2]/r);
285
 
        *lng = atan2(v[1],v[0]);
286
 
}
287
 
 
288
 
void rectToSphe(float *lng, float *lat, const Vec3f& v)
289
 
{
290
 
        double r = v.length();
291
 
        *lat = asin(v[2]/r);
292
 
        *lng = atan2(v[1],v[0]);
293
 
}
294
 
 
295
 
void rectToSphe(double *lng, double *lat, const Vec3f& v)
296
 
{
297
 
        double r = v.length();
298
 
        *lat = asin(v[2]/r);
299
 
        *lng = atan2(v[1],v[0]);
300
 
}
301
 
 
302
 
double getDecAngle(const QString& str)
303
 
{
304
 
        QRegExp re1("^\\s*([\\+\\-])?\\s*(\\d+)\\s*[Dd\xBA]\\s*(\\d+)\\s*['Mm]\\s*(\\d+(\\.\\d+)?)\\s*[\"Ss]\\s*([NSEWnsew])?\\s*$");
305
 
        QRegExp re2("^\\s*([\\+\\-])?\\s*(\\d+(\\.\\d+)?).?([NSEWnsew])\\s*$");
306
 
 
307
 
        if (re1.exactMatch(str))
308
 
        {
309
 
                bool neg = (re1.capturedTexts().at(1) == "-");
310
 
                double d = re1.capturedTexts().at(2).toDouble();
311
 
                double m = re1.capturedTexts().at(3).toDouble();
312
 
                double s = re1.capturedTexts().at(4).toDouble();
313
 
                QString cardinal = re1.capturedTexts().at(6);
314
 
                double deg = d + (m/60) + (s/3600);
315
 
                if (cardinal.toLower() == "s" || cardinal.toLower() == "w" || neg)
316
 
                        deg *= -1.;
317
 
                return (deg * 2 * M_PI / 360.);
318
 
        }
319
 
        else if (re2.exactMatch(str))
320
 
        {
321
 
                bool neg = (re2.capturedTexts().at(1) == "-");
322
 
                double deg = re2.capturedTexts().at(2).toDouble();
323
 
                QString cardinal = re2.capturedTexts().at(4);
324
 
                if (cardinal.toLower() == "s" || cardinal.toLower() == "w" || neg)
325
 
                        deg *= -1.;
326
 
                return (deg * 2 * M_PI / 360.);
327
 
        }
328
 
 
329
 
        qDebug() << "getDecAngle failed to parse angle string:" << str;
330
 
        return -0.0;
331
 
 
332
 
}
333
 
 
334
 
// Check if a number is a power of 2
335
 
bool isPowerOfTwo(int value)
336
 
{
337
 
        return (value & -value) == value;
338
 
}
339
 
 
340
 
// Return the first power of two bigger than the given value 
341
 
int getBiggerPowerOfTwo(int value)
342
 
{
343
 
        int p=1;
344
 
        while (p<value)
345
 
                p<<=1;
346
 
        return p;
347
 
}
348
 
        
349
 
// Return the inverse sinus hyperbolic of z
350
 
double asinh(double z)
351
 
{
352
 
        return std::log(z+std::sqrt(z*z+1));
353
 
}
354
 
 
355
 
/*************************************************************************
356
 
 Convert a QT QDateTime class to julian day
357
 
*************************************************************************/
358
 
double qDateTimeToJd(const QDateTime& dateTime)
359
 
{
360
 
        return (double)(dateTime.date().toJulianDay())+(double)1./(24*60*60*1000)*QTime().msecsTo(dateTime.time())-0.5;
361
 
}
362
 
 
363
 
QDateTime jdToQDateTime(const double& jd)
364
 
{
365
 
        int year, month, day;
366
 
        getDateFromJulianDay(jd, &year, &month, &day);
367
 
        QDateTime result = QDateTime::fromString(QString("%1.%2.%3").arg(year, 4, 10, QLatin1Char('0')).arg(month).arg(day), "yyyy.M.d");
368
 
        result.setTime(jdFractionToQTime(jd));
369
 
        return result;
370
 
}
371
 
 
372
 
// based on QDateTime's original handling, but expanded to handle 0.0 and earlier.
373
 
void getDateFromJulianDay(double jd, int *year, int *month, int *day)
374
 
{
375
 
        int y, m, d;
376
 
 
377
 
        // put us in the right calendar day for the time of day.
378
 
        double fraction = jd - floor(jd);
379
 
        if (fraction >= .5)
380
 
        {
381
 
                jd += 1.0;
382
 
        }
383
 
 
384
 
        if (jd >= 2299161)
385
 
        {
386
 
                // Gregorian calendar starting from October 15, 1582
387
 
                // This algorithm is from Henry F. Fliegel and Thomas C. Van Flandern
388
 
                qulonglong ell, n, i, j;
389
 
                ell = qulonglong(floor(jd)) + 68569;
390
 
                n = (4 * ell) / 146097;
391
 
                ell = ell - (146097 * n + 3) / 4;
392
 
                i = (4000 * (ell + 1)) / 1461001;
393
 
                ell = ell - (1461 * i) / 4 + 31;
394
 
                j = (80 * ell) / 2447;
395
 
                d = ell - (2447 * j) / 80;
396
 
                ell = j / 11;
397
 
                m = j + 2 - (12 * ell);
398
 
                y = 100 * (n - 49) + i + ell;
399
 
        }
400
 
        else
401
 
        {
402
 
                // Julian calendar until October 4, 1582
403
 
                // Algorithm from Frequently Asked Questions about Calendars by Claus Toendering
404
 
                int julianDay = (int)floor(jd);
405
 
                julianDay += 32082;
406
 
                int dd = (4 * julianDay + 3) / 1461;
407
 
                int ee = julianDay - (1461 * dd) / 4;
408
 
                int mm = ((5 * ee) + 2) / 153;
409
 
                d = ee - (153 * mm + 2) / 5 + 1;
410
 
                m = mm + 3 - 12 * (mm / 10);
411
 
                y = dd - 4800 + (mm / 10);
412
 
        }
413
 
        *year = y;
414
 
        *month = m;
415
 
        *day = d;
416
 
}
417
 
 
418
 
void getTimeFromJulianDay(double julianDay, int *hour, int *minute, int *second)
419
 
{
420
 
        double frac = julianDay - (floor(julianDay));
421
 
        int s = (int)floor(frac * 24 * 60 * 60);
422
 
 
423
 
        *hour = ((s / (60 * 60))+12)%24;
424
 
        *minute = (s/(60))%60;
425
 
        *second = s % 60;
426
 
}
427
 
 
428
 
QString sixIntsToIsoString( int year, int month, int day, int hour, int minute, int second )
429
 
{
430
 
        // formatting a negative doesnt work the way i expect
431
 
 
432
 
        QString dt = QString("%1-%2-%3T%4:%5:%6")
433
 
                     .arg((year >= 0 ? year : -1* year),4,10,QLatin1Char('0'))
434
 
                     .arg(month,2,10,QLatin1Char('0'))
435
 
                     .arg(day,2,10,QLatin1Char('0'))
436
 
                     .arg(hour,2,10,QLatin1Char('0'))
437
 
                     .arg(minute,2,10,QLatin1Char('0'))
438
 
                     .arg(second,2,10,QLatin1Char('0'));
439
 
 
440
 
        if (year < 0)
441
 
        {
442
 
                dt.prepend("-");
443
 
        }
444
 
        return dt;
445
 
}
446
 
 
447
 
QString jdToIsoString(double jd)
448
 
{
449
 
        int year, month, day, hour, minute, second;
450
 
        getDateFromJulianDay(jd, &year, &month, &day);
451
 
        getTimeFromJulianDay(jd, &hour, &minute, &second);
452
 
 
453
 
        return sixIntsToIsoString(year, month, day, hour, minute, second);
454
 
}
455
 
 
456
 
// Format the date per the fmt.
457
 
QString localeDateString(int year, int month, int day, int dayOfWeek, QString fmt)
458
 
{
459
 
        /* we have to handle the year zero, and the years before qdatetime can represent. */
460
 
        const QLatin1Char quote('\'');
461
 
        QString out;
462
 
        int quotestartedat = -1;
463
 
 
464
 
        for (int i = 0; i < (int)fmt.length(); i++)
465
 
        {
466
 
                if (fmt.at(i) == quote)
467
 
                {
468
 
                        if (quotestartedat >= 0)
469
 
                        {
470
 
                                if ((quotestartedat+1) == i)
471
 
                                {
472
 
                                        out += quote;
473
 
                                        quotestartedat = -1;
474
 
                                }
475
 
                                else
476
 
                                {
477
 
                                        quotestartedat = -1;
478
 
                                }
479
 
                        }
480
 
                        else
481
 
                        {
482
 
                                quotestartedat = i;
483
 
                        }
484
 
                }
485
 
                else if (quotestartedat > 0)
486
 
                {
487
 
                        out += fmt.at(i);
488
 
                }
489
 
                else if (fmt.at(i) == QLatin1Char('d') ||
490
 
                         fmt.at(i) == QLatin1Char('M') ||
491
 
                         fmt.at(i) == QLatin1Char('y'))
492
 
                {
493
 
                        int j = i+1;
494
 
                        while ((j < fmt.length()) && (fmt.at(j) == fmt.at(i)) && (4 >= (j-i+1)))
495
 
                        {
496
 
                                j++;
497
 
                        }
498
 
 
499
 
                        QString frag = fmt.mid(i,(j-i));
500
 
 
501
 
                        if (frag == "d")
502
 
                        {
503
 
                                out += QString("%1").arg(day);
504
 
                        }
505
 
                        else if (frag == "dd")
506
 
                        {
507
 
                                out += QString("%1").arg(day, 2, 10, QLatin1Char('0'));
508
 
                        }
509
 
                        else if (frag == "ddd")
510
 
                        {
511
 
                                out += QDate::shortDayName(dayOfWeek+1);
512
 
                        }
513
 
                        else if (frag == "dddd")
514
 
                        {
515
 
                                out += QDate::longDayName(dayOfWeek+1);
516
 
                        }
517
 
                        else if (frag == "M")
518
 
                        {
519
 
                                out += QString("%1").arg(month);
520
 
                        }
521
 
                        else if (frag == "MM")
522
 
                        {
523
 
                                out += QString("%1").arg(month, 2, 10, QLatin1Char('0'));
524
 
                        }
525
 
                        else if (frag == "MMM")
526
 
                        {
527
 
                                out += QDate::shortMonthName(month);
528
 
                        }
529
 
                        else if (frag == "MMMM")
530
 
                        {
531
 
                                out += QDate::longMonthName(month);
532
 
                        }
533
 
                        else if (frag == "y")
534
 
                        {
535
 
                                out += frag;
536
 
                        }
537
 
                        else if (frag == "yy")
538
 
                        {
539
 
                                int dispyear = year % 100;
540
 
                                out += QString("%1").arg(dispyear,2,10,QLatin1Char('0'));
541
 
                        }
542
 
                        else if (frag == "yyy")
543
 
                        {
544
 
                                // assume greedy: understand yy before y.
545
 
                                int dispyear = year % 100;
546
 
                                out += QString("%1").arg(dispyear,2,10,QLatin1Char('0'));
547
 
                                out += QLatin1Char('y');
548
 
                        }
549
 
                        else if (frag == "yyyy")
550
 
                        {
551
 
                                int dispyear = (year >= 0 ? year : -1 * year);
552
 
                                if (year <  0)
553
 
                                {
554
 
                                        out += QLatin1Char('-');
555
 
                                }
556
 
                                out += QString("%1").arg(dispyear,4,10,QLatin1Char('0'));
557
 
                        }
558
 
 
559
 
                        i = j-1;
560
 
                }
561
 
                else
562
 
                {
563
 
                        out += fmt.at(i);
564
 
                }
565
 
 
566
 
 
567
 
        }
568
 
 
569
 
        return out;
570
 
}
571
 
 
572
 
//! try to get a reasonable locale date string from the system, trying to work around
573
 
//! limitations of qdatetime for large dates in the past.  see QDateTime::toString().
574
 
QString localeDateString(int year, int month, int day, int dayOfWeek)
575
 
{
576
 
 
577
 
        // try the QDateTime first
578
 
        QDate test(year, month, day);
579
 
 
580
 
        if (test.isValid() && !test.toString(Qt::LocaleDate).isEmpty())
581
 
        {
582
 
                return test.toString(Qt::LocaleDate);
583
 
        }
584
 
        else
585
 
        {
586
 
                return localeDateString(year,month,day,dayOfWeek,QLocale().dateFormat(QLocale::ShortFormat));
587
 
        }
588
 
}
589
 
 
590
 
 
591
 
//! use QDateTime to get a Julian Date from the system's current time.
592
 
//! this is an acceptable use of QDateTime because the system's current
593
 
//! time is more than likely always going to be expressible by QDateTime.
594
 
double getJDFromSystem(void)
595
 
{
596
 
        return qDateTimeToJd(QDateTime::currentDateTime().toUTC());
597
 
}
598
 
 
599
 
double qTimeToJDFraction(const QTime& time)
600
 
{
601
 
        return (double)1./(24*60*60*1000)*QTime().msecsTo(time)-0.5;
602
 
}
603
 
 
604
 
QTime jdFractionToQTime(const double jd)
605
 
{
606
 
        double decHours = std::fmod(jd+0.5, 1.0);
607
 
        int hours = (int)(decHours/0.041666666666666666666);
608
 
        int mins = (int)((decHours-(hours*0.041666666666666666666))/0.00069444444444444444444);
609
 
        return QTime::fromString(QString("%1.%2").arg(hours).arg(mins), "h.m");
610
 
}
611
 
 
612
 
// Use Qt's own sense of time and offset instead of platform specific code.
613
 
float getGMTShiftFromQT(double JD)
614
 
{
615
 
        int year, month, day, hour, minute, second;
616
 
        getDateFromJulianDay(JD, &year, &month, &day);
617
 
        getTimeFromJulianDay(JD, &hour, &minute, &second);
618
 
        QDateTime current(QDate(year, month, day), QTime(hour, minute, second));
619
 
        if (! current.isValid())
620
 
        {
621
 
                qWarning() << "JD " << QString("%1").arg(JD) << " out of bounds of QT help with GMT shift, using current datetime";
622
 
                current = QDateTime::currentDateTime();
623
 
        }
624
 
        QDateTime c1 = QDateTime::fromString(current.toString(Qt::ISODate),Qt::ISODate);
625
 
        QDateTime u1 = QDateTime::fromString(current.toUTC().toString(Qt::ISODate),Qt::ISODate);
626
 
 
627
 
        int secsto = u1.secsTo(c1);
628
 
        float hrsto = secsto / 3600.0f;
629
 
        return hrsto;
630
 
}
631
 
 
632
 
// UTC !
633
 
bool getJDFromDate(double* newjd, int y, int m, int d, int h, int min, int s)
634
 
{
635
 
        double deltaTime = (h / 24.0) + (min / (24.0*60.0)) + (s / (24.0 * 60.0 * 60.0)) - 0.5;
636
 
        QDate test((y <= 0 ? y-1 : y), m, d);
637
 
        // if QDate will oblige, do so.
638
 
        if ( test.isValid() )
639
 
        {
640
 
                double qdjd = (double)test.toJulianDay();
641
 
                qdjd += deltaTime;
642
 
                *newjd = qdjd;
643
 
                return true;
644
 
        } else
645
 
        {
646
 
                double jd = (double)((1461 * (y + 4800 + (m - 14) / 12)) / 4 + (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 - (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + d - 32075) - 38;
647
 
                jd += deltaTime;
648
 
                *newjd = jd;
649
 
                return true;
650
 
        }
651
 
        return false;
652
 
}
653
 
 
654
 
double getJDFromDate_alg2(int y, int m, int d, int h, int min, int s)
655
 
{
656
 
        double extra = (100.0* y) + m - 190002.5;
657
 
        double rjd = 367.0 * y;
658
 
        rjd -= floor(7.0*(y+floor((m+9.0)/12.0))/4.0);
659
 
        rjd += floor(275.0*m/9.0) ;
660
 
        rjd += d;
661
 
        rjd += (h + (min + s/60.0)/60.)/24.0;
662
 
        rjd += 1721013.5;
663
 
        rjd -= 0.5*extra/std::fabs(extra);
664
 
        rjd += 0.5;
665
 
        return rjd;
666
 
}
667
 
 
668
 
int numberOfDaysInMonthInYear(int month, int year)
669
 
{
670
 
        switch(month)
671
 
        {
672
 
                case 1:
673
 
                case 3:
674
 
                case 5:
675
 
                case 7:
676
 
                case 8:
677
 
                case 10:
678
 
                case 12:
679
 
                        return 31;
680
 
                        break;
681
 
                case 4:
682
 
                case 6:
683
 
                case 9:
684
 
                case 11:
685
 
                        return 30;
686
 
                        break;
687
 
                
688
 
                case 2:
689
 
                        if ( year > 1582 )
690
 
                        {
691
 
                                if ( year % 4 == 0 )
692
 
                                {
693
 
                                        if ( year % 100 == 0 )
694
 
                                        {
695
 
                                                if ( year % 400 == 0 )
696
 
                                                {
697
 
                                                        return 29;
698
 
                                                }
699
 
                                                else
700
 
                                                {
701
 
                                                        return 28;
702
 
                                                }
703
 
                                        }
704
 
                                        else
705
 
                                        {
706
 
                                                return 29;
707
 
                                        }
708
 
                                }
709
 
                                else
710
 
                                {
711
 
                                        return 28;
712
 
                                }
713
 
                        }
714
 
                        else
715
 
                        {
716
 
                                if ( year % 4 == 0 )
717
 
                                {
718
 
                                        return 29;
719
 
                                }
720
 
                                else
721
 
                                {
722
 
                                        return 28;
723
 
                                }
724
 
                        }
725
 
                        break;
726
 
                
727
 
                case 0:
728
 
                        return numberOfDaysInMonthInYear(12, year-1);
729
 
                        break;
730
 
                case 13:
731
 
                        return numberOfDaysInMonthInYear(1, year+1);
732
 
                        break;
733
 
                default:
734
 
                        break;
735
 
        }
736
 
        
737
 
        return 0;
738
 
}
739
 
 
740
 
//! given the submitted year/month/day hour:minute:second, try to
741
 
//! normalize into an actual year/month/day.  values can be positive, 0,
742
 
//! or negative.  start assessing from seconds to larger increments.
743
 
bool changeDateTimeForRollover(int oy, int om, int od, int oh, int omin, int os,
744
 
                                int* ry, int* rm, int* rd, int* rh, int* rmin, int* rs)
745
 
{
746
 
        bool change = false;
747
 
        
748
 
        while ( os > 59 ) {
749
 
                os -= 60;
750
 
                omin += 1;
751
 
                change = true;
752
 
        }
753
 
        while ( os < 0 ) {
754
 
                os += 60;
755
 
                omin -= 1;
756
 
                change = true;
757
 
        }
758
 
        
759
 
        while (omin > 59 ) {
760
 
                omin -= 60;
761
 
                oh += 1;
762
 
                change = true;
763
 
        }
764
 
        while (omin < 0 ) {
765
 
                omin += 60;
766
 
                oh -= 1;
767
 
                change = true;
768
 
        }
769
 
        
770
 
        while ( oh > 23 ) {
771
 
                oh -= 24;
772
 
                od += 1;
773
 
                change = true;
774
 
        }
775
 
        while ( oh < 0 ) {
776
 
                oh += 24;
777
 
                od -= 1;
778
 
                change = true;
779
 
        }
780
 
        
781
 
        while ( od > numberOfDaysInMonthInYear(om, oy) ) {
782
 
                od -= numberOfDaysInMonthInYear(om, oy);
783
 
                om++;
784
 
                if ( om > 12 ) {
785
 
                om -= 12;
786
 
                oy += 1;
787
 
                }
788
 
                change = true;
789
 
        }
790
 
        while ( od < 1 ) {
791
 
                od += numberOfDaysInMonthInYear(om-1,oy);
792
 
                om--;
793
 
                if ( om < 1 ) {
794
 
                om += 12;
795
 
                oy -= 1;
796
 
                }
797
 
                change = true;
798
 
        }
799
 
        
800
 
        while ( om > 12 ) {
801
 
                om -= 12;
802
 
                oy += 1;
803
 
                change = true;
804
 
        }
805
 
        while ( om < 1 ) {
806
 
                om += 12;
807
 
                oy -= 1;
808
 
                change = true;
809
 
        }
810
 
        
811
 
        // and the julian-gregorian epoch hole: round up to the 15th
812
 
        if ( oy == 1582 && om == 10 && ( od > 4 && od < 15 ) ) {
813
 
                od = 15;
814
 
                change = true;
815
 
        }
816
 
        
817
 
        if ( change ) {
818
 
                *ry = oy;
819
 
                *rm = om;
820
 
                *rd = od;
821
 
                *rh = oh;
822
 
                *rmin = omin;
823
 
                *rs = os;
824
 
        }
825
 
        return change;
826
 
}
827
 
 
828
 
 
829
 
} // end of the StelUtils namespace
830