Commit f3200d7d authored by Luc Maisonobe's avatar Luc Maisonobe

Fixed display of leap seconds in time zones shifted from UTC.

parent ed8f9adf
......@@ -846,16 +846,38 @@ public class AbsoluteDate
public DateTimeComponents getComponents(final int minutesFromUTC)
throws OrekitException {
// shift the date according to UTC offset
final AbsoluteDate shifted = shiftedBy(60 * minutesFromUTC);
final DateTimeComponents localComponents = shifted.getComponents(TimeScalesFactory.getUTC());
final DateTimeComponents utcComponents = getComponents(TimeScalesFactory.getUTC());
// shift the date according to UTC offset, but WITHOUT touching the seconds,
// as they may exceed 60.0 during a leap seconds introduction,
// and we want to preserve these special cases
final double seconds = utcComponents.getTime().getSecond();
int minute = utcComponents.getTime().getMinute() + minutesFromUTC;
final int hourShift;
if (minute < 0) {
hourShift = (minute - 59) / 60;
} else if (minute > 59) {
hourShift = minute / 60;
} else {
hourShift = 0;
}
minute -= 60 * hourShift;
int hour = utcComponents.getTime().getHour() + hourShift;
final int dayShift;
if (hour < 0) {
dayShift = (hour - 23) / 24;
} else if (hour > 23) {
dayShift = hour / 24;
} else {
dayShift = 0;
}
hour -= 24 * dayShift;
return new DateTimeComponents(new DateComponents(utcComponents.getDate(), dayShift),
new TimeComponents(hour, minute, seconds, minutesFromUTC));
// register the offset that was applied
return new DateTimeComponents(localComponents.getDate(),
new TimeComponents(localComponents.getTime().getHour(),
localComponents.getTime().getMinute(),
localComponents.getTime().getSecond(),
minutesFromUTC));
}
/** Split the instance into date/time components for a time zone.
......@@ -866,7 +888,7 @@ public class AbsoluteDate
*/
public DateTimeComponents getComponents(final TimeZone timeZone)
throws OrekitException {
final long milliseconds = FastMath.round(1000 * durationFrom(JAVA_EPOCH));
final long milliseconds = FastMath.round(1000 * offsetFrom(JAVA_EPOCH, TimeScalesFactory.getUTC()));
return getComponents(timeZone.getOffset(milliseconds) / 60000);
}
......
......@@ -17,6 +17,7 @@
package org.orekit.time;
import java.io.IOException;
import java.util.Date;
import java.util.TimeZone;
......@@ -135,7 +136,7 @@ public class AbsoluteDateTest {
}
@Test
public void testLocaltime() throws OrekitException {
public void testLocalTimeParsing() throws OrekitException {
TimeScale utc = TimeScalesFactory.getUTC();
Assert.assertEquals(new AbsoluteDate("2011-12-31T23:00:00", utc),
new AbsoluteDate("2012-01-01T03:30:00+04:30", utc));
......@@ -152,25 +153,7 @@ public class AbsoluteDateTest {
}
@Test
public void testParseLeap() throws OrekitException {
TimeScale utc = TimeScalesFactory.getUTC();
AbsoluteDate beforeLeap = new AbsoluteDate("2012-06-30T23:59:59.8", utc);
AbsoluteDate inLeap = new AbsoluteDate("2012-06-30T23:59:60.5", utc);
Assert.assertEquals(0.7, inLeap.durationFrom(beforeLeap), 1.0e-12);
Assert.assertEquals("2012-06-30T23:59:60.500", inLeap.toString(utc));
}
@Test
public void testOutput() {
TimeScale tt = TimeScalesFactory.getTT();
Assert.assertEquals("1950-01-01T01:01:01.000",
AbsoluteDate.FIFTIES_EPOCH.shiftedBy(3661.0).toString(tt));
Assert.assertEquals("2000-01-01T13:01:01.000",
AbsoluteDate.J2000_EPOCH.shiftedBy(3661.0).toString(tt));
}
@Test
public void testLocalTime() throws OrekitException {
public void testTimeZoneDisplay() throws OrekitException {
final TimeScale utc = TimeScalesFactory.getUTC();
final AbsoluteDate date = new AbsoluteDate("2000-01-01T01:01:01.000", utc);
Assert.assertEquals("2000-01-01T01:01:01.000", date.toString());
......@@ -188,6 +171,64 @@ public class AbsoluteDateTest {
}
@Test
public void testLocalTimeLeapSecond() throws OrekitException, IOException {
TimeScale utc = TimeScalesFactory.getUTC();
AbsoluteDate beforeLeap = new AbsoluteDate("2012-06-30T23:59:59.8", utc);
AbsoluteDate inLeap = new AbsoluteDate("2012-06-30T23:59:60.5", utc);
Assert.assertEquals(0.7, inLeap.durationFrom(beforeLeap), 1.0e-12);
for (int minutesFromUTC = -1500; minutesFromUTC < 1500; ++minutesFromUTC) {
DateTimeComponents dtcBeforeLeap = beforeLeap.getComponents(minutesFromUTC);
DateTimeComponents dtcInsideLeap = inLeap.getComponents(minutesFromUTC);
Assert.assertEquals(dtcBeforeLeap.getDate(), dtcInsideLeap.getDate());
Assert.assertEquals(dtcBeforeLeap.getTime().getHour(), dtcInsideLeap.getTime().getHour());
Assert.assertEquals(dtcBeforeLeap.getTime().getMinute(), dtcInsideLeap.getTime().getMinute());
Assert.assertEquals(minutesFromUTC, dtcBeforeLeap.getTime().getMinutesFromUTC());
Assert.assertEquals(minutesFromUTC, dtcInsideLeap.getTime().getMinutesFromUTC());
Assert.assertEquals(59.8, dtcBeforeLeap.getTime().getSecond(), 1.0e-10);
Assert.assertEquals(60.5, dtcInsideLeap.getTime().getSecond(), 1.0e-10);
}
}
@Test
public void testTimeZoneLeapSecond() throws OrekitException {
TimeScale utc = TimeScalesFactory.getUTC();
final TimeZone tz = TimeZone.getTimeZone("Europe/Paris");
AbsoluteDate localBeforeMidnight = new AbsoluteDate("2012-06-30T21:59:59.800", utc);
Assert.assertEquals("2012-06-30T23:59:59.800+02:00",
localBeforeMidnight.toString(tz));
Assert.assertEquals("2012-07-01T00:00:00.800+02:00",
localBeforeMidnight.shiftedBy(1.0).toString(tz));
AbsoluteDate beforeLeap = new AbsoluteDate("2012-06-30T23:59:59.8", utc);
AbsoluteDate inLeap = new AbsoluteDate("2012-06-30T23:59:60.5", utc);
Assert.assertEquals(0.7, inLeap.durationFrom(beforeLeap), 1.0e-12);
Assert.assertEquals("2012-07-01T01:59:59.800+02:00", beforeLeap.toString(tz));
Assert.assertEquals("2012-07-01T01:59:60.500+02:00", inLeap.toString(tz));
}
@Test
public void testParseLeap() throws OrekitException {
TimeScale utc = TimeScalesFactory.getUTC();
AbsoluteDate beforeLeap = new AbsoluteDate("2012-06-30T23:59:59.8", utc);
AbsoluteDate inLeap = new AbsoluteDate("2012-06-30T23:59:60.5", utc);
Assert.assertEquals(0.7, inLeap.durationFrom(beforeLeap), 1.0e-12);
Assert.assertEquals("2012-06-30T23:59:60.500", inLeap.toString(utc));
}
@Test
public void testOutput() {
TimeScale tt = TimeScalesFactory.getTT();
Assert.assertEquals("1950-01-01T01:01:01.000",
AbsoluteDate.FIFTIES_EPOCH.shiftedBy(3661.0).toString(tt));
Assert.assertEquals("2000-01-01T13:01:01.000",
AbsoluteDate.J2000_EPOCH.shiftedBy(3661.0).toString(tt));
}
@Test
public void testJ2000() {
Assert.assertEquals("2000-01-01T12:00:00.000",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment