1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.orekit.time;
18
19 import java.io.Serializable;
20
21 import org.hipparchus.util.FastMath;
22 import org.orekit.utils.Constants;
23
24
25
26
27
28
29
30
31
32 public class DateTimeComponents implements Serializable, Comparable<DateTimeComponents> {
33
34
35 private static final long serialVersionUID = 5061129505488924484L;
36
37
38 private final DateComponents date;
39
40
41 private final TimeComponents time;
42
43
44
45
46
47 public DateTimeComponents(final DateComponents date, final TimeComponents time) {
48 this.date = date;
49 this.time = time;
50 }
51
52
53
54
55
56
57
58
59
60
61
62
63 public DateTimeComponents(final int year, final int month, final int day,
64 final int hour, final int minute, final double second)
65 throws IllegalArgumentException {
66 this.date = new DateComponents(year, month, day);
67 this.time = new TimeComponents(hour, minute, second);
68 }
69
70
71
72
73
74
75
76
77
78
79
80
81 public DateTimeComponents(final int year, final Month month, final int day,
82 final int hour, final int minute, final double second)
83 throws IllegalArgumentException {
84 this.date = new DateComponents(year, month, day);
85 this.time = new TimeComponents(hour, minute, second);
86 }
87
88
89
90
91
92
93
94
95
96
97 public DateTimeComponents(final int year, final int month, final int day)
98 throws IllegalArgumentException {
99 this.date = new DateComponents(year, month, day);
100 this.time = TimeComponents.H00;
101 }
102
103
104
105
106
107
108
109
110
111
112 public DateTimeComponents(final int year, final Month month, final int day)
113 throws IllegalArgumentException {
114 this.date = new DateComponents(year, month, day);
115 this.time = TimeComponents.H00;
116 }
117
118
119
120
121
122
123 public DateTimeComponentstml#DateTimeComponents">DateTimeComponents(final DateTimeComponents reference,
124 final double offset) {
125
126
127 int day = reference.getDate().getJ2000Day();
128 double seconds = reference.getTime().getSecondsInLocalDay();
129
130
131 seconds += offset;
132
133
134 final int dayShift = (int) FastMath.floor(seconds / Constants.JULIAN_DAY);
135 seconds -= Constants.JULIAN_DAY * dayShift;
136 day += dayShift;
137 final TimeComponentsComponents">TimeComponents tmpTime = new TimeComponents(seconds);
138
139
140 this.date = new DateComponents(day);
141 this.time = new TimeComponents(tmpTime.getHour(), tmpTime.getMinute(), tmpTime.getSecond(),
142 reference.getTime().getMinutesFromUTC());
143
144 }
145
146
147
148
149
150
151
152
153
154
155
156 public static DateTimeComponents parseDateTime(final String string) {
157
158
159 final int tIndex = string.indexOf('T');
160 if (tIndex > 0) {
161 return new DateTimeComponents(DateComponents.parseDate(string.substring(0, tIndex)),
162 TimeComponents.parseTime(string.substring(tIndex + 1)));
163 }
164
165 return new DateTimeComponents(DateComponents.parseDate(string), TimeComponents.H00);
166
167 }
168
169
170
171
172
173
174
175 public double offsetFrom(final DateTimeComponents dateTime) {
176 final int dateOffset = date.getJ2000Day() - dateTime.date.getJ2000Day();
177 final double timeOffset = time.getSecondsInUTCDay() - dateTime.time.getSecondsInUTCDay();
178 return Constants.JULIAN_DAY * dateOffset + timeOffset;
179 }
180
181
182
183
184 public DateComponents getDate() {
185 return date;
186 }
187
188
189
190
191 public TimeComponents getTime() {
192 return time;
193 }
194
195
196 public int compareTo(final DateTimeComponents other) {
197 final int dateComparison = date.compareTo(other.date);
198 if (dateComparison < 0) {
199 return -1;
200 } else if (dateComparison > 0) {
201 return 1;
202 }
203 return time.compareTo(other.time);
204 }
205
206
207 public boolean equals(final Object other) {
208 try {
209 final DateTimeComponents/time/DateTimeComponents.html#DateTimeComponents">DateTimeComponents otherDateTime = (DateTimeComponents) other;
210 return (otherDateTime != null) &&
211 date.equals(otherDateTime.date) && time.equals(otherDateTime.time);
212 } catch (ClassCastException cce) {
213 return false;
214 }
215 }
216
217
218 public int hashCode() {
219 return (date.hashCode() << 16) ^ time.hashCode();
220 }
221
222
223
224
225
226 public String toString() {
227 return toString(60);
228 }
229
230
231
232
233
234
235
236 public String toString(final int minuteDuration) {
237 double second = time.getSecond();
238 final double wrap = minuteDuration - 0.0005;
239 if (second >= wrap) {
240
241 int minute = time.getMinute();
242 int hour = time.getHour();
243 int j2000 = date.getJ2000Day();
244 second = 0;
245 ++minute;
246 if (minute > 59) {
247 minute = 0;
248 ++hour;
249 if (hour > 23) {
250 hour = 0;
251 ++j2000;
252 }
253 }
254 return new DateComponents(j2000).toString() + 'T' + new TimeComponents(hour, minute, second).toString();
255 }
256 return date.toString() + 'T' + time.toString();
257 }
258
259 }
260