1 | /*************************************************************************** |
2 | kstarsdatetime.h - K Desktop Planetarium |
3 | ------------------- |
4 | begin : Tue 05 May 2004 |
5 | copyright : (C) 2001 by Jason Harris |
6 | email : jharris@30doradus.org |
7 | ***************************************************************************/ |
8 | |
9 | /*************************************************************************** |
10 | * * |
11 | * This program is free software; you can redistribute it and/or modify * |
12 | * it under the terms of the GNU General Public License as published by * |
13 | * the Free Software Foundation; either version 2 of the License, or * |
14 | * (at your option) any later version. * |
15 | * * |
16 | ***************************************************************************/ |
17 | |
18 | #ifndef KSTARSDATETIME_H_ |
19 | #define KSTARSDATETIME_H_ |
20 | |
21 | #define J2000 2451545.0 //Julian Date for noon on Jan 1, 2000 (epoch J2000) |
22 | #define B1950 2433282.4235 // Julian date for Jan 0.9235, 1950 |
23 | #define SIDEREALSECOND 1.002737909 //number of sidereal seconds in one solar second |
24 | |
25 | #include <kdatetime.h> |
26 | |
27 | class dms; |
28 | |
29 | /**@class KStarsDateTime |
30 | *@short Extension of KDateTime for KStars |
31 | *KStarsDateTime can represent the date/time as a Julian Day, using a long double, |
32 | *in which the fractional portion encodes the time of day to a precision of a less than a second. |
33 | *Also adds Greenwich Sidereal Time and "epoch", which is just the date expressed as a floating |
34 | *point number representing the year, with the fractional part representing the date and time |
35 | *(with poor time resolution; typically the Epoch is only taken to the hundredths place, which is |
36 | *a few days). |
37 | *@note Local time and Local sideral time are not handled here. Because they depend on the |
38 | *geographic location, they are part of the GeoLocation class. |
39 | *@sa GeoLocation::GSTtoLST() |
40 | *@sa GeoLocation::UTtoLT() |
41 | *@author Jason Harris |
42 | *@version 1.0 |
43 | */ |
44 | |
45 | class KStarsDateTime : public KDateTime |
46 | { |
47 | public: |
48 | /** |
49 | *@short Default constructor |
50 | *Creates a date/time at J2000 (noon on Jan 1, 200) |
51 | */ |
52 | KStarsDateTime(); |
53 | |
54 | /** |
55 | *@short Constructor |
56 | *Creates a date/time at the specified Julian Day. |
57 | *@p jd The Julian Day |
58 | */ |
59 | KStarsDateTime( long double djd ); |
60 | |
61 | /** |
62 | *@short Copy constructor |
63 | *@p kdt The KStarsDateTime object to copy. |
64 | */ |
65 | KStarsDateTime( const KStarsDateTime &kdt ); |
66 | |
67 | /** |
68 | *@short Copy constructor |
69 | *@p kdt The KDateTime object to copy. |
70 | */ |
71 | KStarsDateTime( const KDateTime &kdt ); |
72 | |
73 | /** |
74 | *@short Copy constructor |
75 | *@p qdt The QDateTime object to copy. |
76 | */ |
77 | KStarsDateTime( const QDateTime &qdt ); |
78 | |
79 | /** |
80 | *@short Constructor |
81 | *Create a KStarsDateTimne based on the specified Date and Time. |
82 | *@p _d The QDate to assign |
83 | *@p _t The QTime to assign |
84 | */ |
85 | KStarsDateTime( const QDate &_d, const QTime &_t ); |
86 | |
87 | /** |
88 | *Assign the (long double) Julian Day value, which includes the time of day |
89 | *encoded in the fractional portion. |
90 | *@p jd the Julian Day value to assign. |
91 | */ |
92 | void setDJD( long double jd ); |
93 | |
94 | /** |
95 | *Assign the Date according to a QDate object. |
96 | *@p d the QDate to assign |
97 | */ |
98 | void setDate( const QDate &d ); |
99 | |
100 | /** |
101 | *Assign the Time according to a QTime object. |
102 | *@p t the QTime to assign |
103 | */ |
104 | void setTime( const QTime &t ); |
105 | |
106 | /** |
107 | *@return a KStarsDateTime that is the given number of seconds later |
108 | *than this KStarsDateTime. |
109 | *@p s the number of seconds to add. The number can be negative. |
110 | */ |
111 | KStarsDateTime addSecs( double s ) const; |
112 | |
113 | /** |
114 | *Modify the Date/Time by adding a number of days. |
115 | *@p nd the number of days to add. The number can be negative. |
116 | */ |
117 | inline KStarsDateTime addDays( int nd ) const { return KStarsDateTime( djd() + (long double)nd ); } |
118 | |
119 | inline bool operator == ( const KStarsDateTime &d ) const { return DJD == d.djd(); } |
120 | inline bool operator != ( const KStarsDateTime &d ) const { return DJD != d.djd(); } |
121 | inline bool operator < ( const KStarsDateTime &d ) const { return DJD < d.djd(); } |
122 | inline bool operator <= ( const KStarsDateTime &d ) const { return DJD <= d.djd(); } |
123 | inline bool operator > ( const KStarsDateTime &d ) const { return DJD > d.djd(); } |
124 | inline bool operator >= ( const KStarsDateTime &d ) const { return DJD >= d.djd(); } |
125 | |
126 | /** |
127 | *@return the date and time according to the CPU clock |
128 | *@p ts a Qt::TimeSpec value that determines whether the date is |
129 | *computed from the Local Time or the Universal Time. |
130 | *@sa Qt::TimeSpec |
131 | */ |
132 | static KStarsDateTime currentDateTime(KDateTime::Spec ts = KDateTime::Spec::ClockTime()); |
133 | |
134 | /** |
135 | *@return a KStarsDateTime object parsed from the given string. |
136 | *@note This function is format-agnostic; it will try several formats |
137 | *when parsing the string. |
138 | *@param s the string expressing the date/time to be parsed. |
139 | */ |
140 | static KStarsDateTime fromString( const QString &s ); |
141 | |
142 | /** |
143 | *@return the julian day as a long double, including the time as the fractional portion. |
144 | */ |
145 | inline long double djd() const { return DJD; } |
146 | |
147 | /** |
148 | *@return The Greenwich Sidereal Time |
149 | *The Greenwich sidereal time is the Right Ascension coordinate that is currently transiting |
150 | *the Prime Meridian at the Royal Observatory in Greenwich, UK (longitude=0.0) |
151 | */ |
152 | dms gst() const; |
153 | |
154 | /** |
155 | *Convert a given Greenwich Sidereal Time to Universal Time (=Greenwich Mean Time). |
156 | *@p GST the Greenwich Sidereal Time to convert to Universal Time. |
157 | */ |
158 | QTime GSTtoUT( dms GST ) const; |
159 | |
160 | |
161 | /** |
162 | *@return the epoch value of the Date/Time. |
163 | *@note the epoch is shorthand for the date, expressed as a floating-point year value. |
164 | *@sa setFromEpoch() |
165 | */ |
166 | inline double epoch() const { return ( double( date().year() ) |
167 | + double( date().dayOfYear() )/double( date().daysInYear() ) ); } |
168 | |
169 | /** |
170 | *Set the Date/Time from an epoch value, represented as a double. |
171 | *@p e the epoch value |
172 | *@return true if date set successfully |
173 | *@sa epoch() |
174 | */ |
175 | bool setFromEpoch( double e ); |
176 | |
177 | /** |
178 | *Set the Date/Time from an epoch value, represented as a string. |
179 | *@p e the epoch value |
180 | *@return true if date set successfully |
181 | *@sa epoch() |
182 | */ |
183 | bool setFromEpoch( const QString &e ); |
184 | |
185 | |
186 | private: |
187 | /** |
188 | *@return the Greenwich Sidereal Time at 0h UT on this object's Date |
189 | *@note used internally by gst() and GSTtoUT() |
190 | */ |
191 | dms GSTat0hUT() const; |
192 | |
193 | long double DJD; |
194 | }; |
195 | |
196 | #endif //KSTARSDATETIME_H_ |
197 | |
198 | |