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 import java.text.DecimalFormat;
21 import java.text.DecimalFormatSymbols;
22 import java.util.Locale;
23
24 import org.hipparchus.util.FastMath;
25 import org.orekit.utils.Constants;
26
27
28
29
30
31
32
33
34
35 public class DateTimeComponents implements Serializable, Comparable<DateTimeComponents> {
36
37
38
39
40
41
42 public static final DateTimeComponents JULIAN_EPOCH =
43 new DateTimeComponents(DateComponents.JULIAN_EPOCH, TimeComponents.H12);
44
45
46 private static final long serialVersionUID = 5061129505488924484L;
47
48
49 private final DateComponents date;
50
51
52 private final TimeComponents time;
53
54
55
56
57
58 public DateTimeComponents(final DateComponents date, final TimeComponents time) {
59 this.date = date;
60 this.time = time;
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74 public DateTimeComponents(final int year, final int month, final int day,
75 final int hour, final int minute, final double second)
76 throws IllegalArgumentException {
77 this.date = new DateComponents(year, month, day);
78 this.time = new TimeComponents(hour, minute, second);
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92 public DateTimeComponents(final int year, final Month month, final int day,
93 final int hour, final int minute, final double second)
94 throws IllegalArgumentException {
95 this.date = new DateComponents(year, month, day);
96 this.time = new TimeComponents(hour, minute, second);
97 }
98
99
100
101
102
103
104
105
106
107
108 public DateTimeComponents(final int year, final int month, final int day)
109 throws IllegalArgumentException {
110 this.date = new DateComponents(year, month, day);
111 this.time = TimeComponents.H00;
112 }
113
114
115
116
117
118
119
120
121
122
123 public DateTimeComponents(final int year, final Month month, final int day)
124 throws IllegalArgumentException {
125 this.date = new DateComponents(year, month, day);
126 this.time = TimeComponents.H00;
127 }
128
129
130
131
132
133
134 public DateTimeComponentstml#DateTimeComponents">DateTimeComponents(final DateTimeComponents reference,
135 final double offset) {
136
137
138 int day = reference.getDate().getJ2000Day();
139 double seconds = reference.getTime().getSecondsInLocalDay();
140
141
142 seconds += offset;
143
144
145 final int dayShift = (int) FastMath.floor(seconds / Constants.JULIAN_DAY);
146 seconds -= Constants.JULIAN_DAY * dayShift;
147 day += dayShift;
148 final TimeComponentsComponents">TimeComponents tmpTime = new TimeComponents(seconds);
149
150
151 this.date = new DateComponents(day);
152 this.time = new TimeComponents(tmpTime.getHour(), tmpTime.getMinute(), tmpTime.getSecond(),
153 reference.getTime().getMinutesFromUTC());
154
155 }
156
157
158
159
160
161
162
163
164
165
166
167 public static DateTimeComponents parseDateTime(final String string) {
168
169
170 final int tIndex = string.indexOf('T');
171 if (tIndex > 0) {
172 return new DateTimeComponents(DateComponents.parseDate(string.substring(0, tIndex)),
173 TimeComponents.parseTime(string.substring(tIndex + 1)));
174 }
175
176 return new DateTimeComponents(DateComponents.parseDate(string), TimeComponents.H00);
177
178 }
179
180
181
182
183
184
185
186 public double offsetFrom(final DateTimeComponents dateTime) {
187 final int dateOffset = date.getJ2000Day() - dateTime.date.getJ2000Day();
188 final double timeOffset = time.getSecondsInUTCDay() - dateTime.time.getSecondsInUTCDay();
189 return Constants.JULIAN_DAY * dateOffset + timeOffset;
190 }
191
192
193
194
195 public DateComponents getDate() {
196 return date;
197 }
198
199
200
201
202 public TimeComponents getTime() {
203 return time;
204 }
205
206
207 public int compareTo(final DateTimeComponents other) {
208 final int dateComparison = date.compareTo(other.date);
209 if (dateComparison < 0) {
210 return -1;
211 } else if (dateComparison > 0) {
212 return 1;
213 }
214 return time.compareTo(other.time);
215 }
216
217
218 public boolean equals(final Object other) {
219 try {
220 final DateTimeComponents/time/DateTimeComponents.html#DateTimeComponents">DateTimeComponents otherDateTime = (DateTimeComponents) other;
221 return (otherDateTime != null) &&
222 date.equals(otherDateTime.date) && time.equals(otherDateTime.time);
223 } catch (ClassCastException cce) {
224 return false;
225 }
226 }
227
228
229 public int hashCode() {
230 return (date.hashCode() << 16) ^ time.hashCode();
231 }
232
233
234
235
236
237 public String toString() {
238 return toString(60);
239 }
240
241
242
243
244
245
246
247 public String toString(final int minuteDuration) {
248 double second = time.getSecond();
249 final double wrap = minuteDuration - 0.0005;
250 if (second >= wrap) {
251
252 int minute = time.getMinute();
253 int hour = time.getHour();
254 int j2000 = date.getJ2000Day();
255 second = 0;
256 ++minute;
257 if (minute > 59) {
258 minute = 0;
259 ++hour;
260 if (hour > 23) {
261 hour = 0;
262 ++j2000;
263 }
264 }
265 return new DateComponents(j2000).toString() + 'T' + new TimeComponents(hour, minute, second).toString();
266 }
267 return date.toString() + 'T' + time.toString();
268 }
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285 public String toStringRfc3339() {
286 final DateComponents d = this.getDate();
287 final TimeComponents t = this.getTime();
288
289 final String dateString = String.format("%04d-%02d-%02dT",
290 d.getYear(), d.getMonth(), d.getDay());
291
292 final String timeString;
293 if (t.getSecondsInLocalDay() != 0) {
294 final DecimalFormat format = new DecimalFormat("00.##############", new DecimalFormatSymbols(Locale.US));
295 timeString = String.format("%02d:%02d:", t.getHour(), t.getMinute()) +
296 format.format(t.getSecond());
297 } else {
298
299 timeString = "00:00:00";
300 }
301
302 final int minutesFromUTC = t.getMinutesFromUTC();
303 final String timeZoneString;
304 if (minutesFromUTC == 0) {
305 timeZoneString = "Z";
306 } else {
307
308 final String sign = minutesFromUTC < 0 ? "-" : "+";
309 final int utcOffset = FastMath.abs(minutesFromUTC);
310 final int hourOffset = utcOffset / 60;
311 final int minuteOffset = utcOffset % 60;
312 timeZoneString = sign + String.format("%02d:%02d", hourOffset, minuteOffset);
313 }
314 return dateString + timeString + timeZoneString;
315 }
316
317 }
318