Commit a0f700d1 authored by Luc Maisonobe's avatar Luc Maisonobe

Added parsing and displaying of local time.

The support is compliant with ISO-8601 standard.
parent 9ad7fd3b
......@@ -18,6 +18,7 @@ package org.orekit.time;
import java.io.Serializable;
import java.util.Date;
import java.util.TimeZone;
import org.apache.commons.math3.util.FastMath;
import org.apache.commons.math3.util.MathArrays;
......@@ -220,7 +221,8 @@ public class AbsoluteDate
final long dl = (long) FastMath.floor(sum);
offset = (sum - dl) + residual;
epoch = 60l * ((date.getJ2000Day() * 24l + time.getHour()) * 60l + time.getMinute() - 720l) + dl;
epoch = 60l * ((date.getJ2000Day() * 24l + time.getHour()) * 60l +
time.getMinute() - time.getMinutesFromUTC() - 720l) + dl;
}
......@@ -837,6 +839,40 @@ public class AbsoluteDate
}
/** Split the instance into date/time components for a local time.
* @param minutesFromUTC offset in <em>minutes</em> from UTC (positive Eastwards UTC,
* negative Westward UTC)
* @return date/time components
* @exception OrekitException if UTC time scale cannot be retrieved
* @since 7.2
*/
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());
// 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.
* @param timeZone time zone
* @return date/time components
* @exception OrekitException if UTC time scale cannot be retrieved
* @since 7.2
*/
public DateTimeComponents getComponents(final TimeZone timeZone)
throws OrekitException {
final long milliseconds = FastMath.round(1000 * durationFrom(JAVA_EPOCH));
return getComponents(timeZone.getOffset(milliseconds) / 60000);
}
/** Compare the instance with another date.
* @param date other date to compare the instance to
* @return a negative integer, zero, or a positive integer as this date
......@@ -905,4 +941,31 @@ public class AbsoluteDate
return getComponents(timeScale).toString(inLeap);
}
/** Get a String representation of the instant location for a local time.
* @param minutesFromUTC offset in <em>minutes</em> from UTC (positive Eastwards UTC,
* negative Westward UTC).
* @return string representation of the instance,
* in ISO-8601 format with milliseconds accuracy
* @exception OrekitException if UTC time scale cannot be retrieved
* @since 7.2
*/
public String toString(final int minutesFromUTC)
throws OrekitException {
final boolean inLeap = TimeScalesFactory.getUTC().insideLeap(this);
return getComponents(minutesFromUTC).toString(inLeap);
}
/** Get a String representation of the instant location for a time zone.
* @param timeZone time zone
* @return string representation of the instance,
* in ISO-8601 format with milliseconds accuracy
* @exception OrekitException if UTC time scale cannot be retrieved
* @since 7.2
*/
public String toString(final TimeZone timeZone)
throws OrekitException {
final boolean inLeap = TimeScalesFactory.getUTC().insideLeap(this);
return getComponents(timeZone).toString(inLeap);
}
}
......@@ -22,6 +22,9 @@
<body>
<release version="7.2" date="TBC"
description="">
<action dev="luc" type="add">
Added parsing and displaying of local time according to ISO-8601 standard.
</action>
<action dev="luc" type="fix">
Added some protections against malformed SP3 files.
</action>
......
......@@ -18,6 +18,7 @@ package org.orekit.time;
import java.util.Date;
import java.util.TimeZone;
import org.apache.commons.math3.util.FastMath;
import org.apache.commons.math3.util.Precision;
......@@ -133,6 +134,23 @@ public class AbsoluteDateTest {
new AbsoluteDate("1958-001", TimeScalesFactory.getTAI()));
}
@Test
public void testLocaltime() 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));
Assert.assertEquals(new AbsoluteDate("2011-12-31T23:00:00", utc),
new AbsoluteDate("2012-01-01T03:30:00+0430", utc));
Assert.assertEquals(new AbsoluteDate("2011-12-31T23:30:00", utc),
new AbsoluteDate("2012-01-01T03:30:00+04", utc));
Assert.assertEquals(new AbsoluteDate("2012-01-01T05:17:00", utc),
new AbsoluteDate("2011-12-31T22:17:00-07:00", utc));
Assert.assertEquals(new AbsoluteDate("2012-01-01T05:17:00", utc),
new AbsoluteDate("2011-12-31T22:17:00-0700", utc));
Assert.assertEquals(new AbsoluteDate("2012-01-01T05:17:00", utc),
new AbsoluteDate("2011-12-31T22:17:00-07", utc));
}
@Test
public void testParseLeap() throws OrekitException {
TimeScale utc = TimeScalesFactory.getUTC();
......@@ -151,6 +169,25 @@ public class AbsoluteDateTest {
AbsoluteDate.J2000_EPOCH.shiftedBy(3661.0).toString(tt));
}
@Test
public void testLocalTime() 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());
Assert.assertEquals("2000-01-01T11:01:01.000+10:00", date.toString( 600));
Assert.assertEquals("1999-12-31T23:01:01.000-02:00", date.toString(-120));
// winter time, Europe is one hour ahead of UTC
final TimeZone tz = TimeZone.getTimeZone("Europe/Paris");
Assert.assertEquals("2001-01-22T11:30:00.000+01:00",
new AbsoluteDate("2001-01-22T10:30:00", utc).toString(tz));
// summer time, Europe is two hours ahead of UTC
Assert.assertEquals("2001-06-23T11:30:00.000+02:00",
new AbsoluteDate("2001-06-23T09:30:00", utc).toString(tz));
}
@Test
public void testJ2000() {
Assert.assertEquals("2000-01-01T12:00:00.000",
......
......@@ -91,9 +91,10 @@ public class DateTimeComponentsTest {
DateTimeComponents.parseDateTime("2000-02-30T03:04:05.000+00:00");
}
@Test(expected=IllegalArgumentException.class)
public void testBadZone() {
DateTimeComponents.parseDateTime("2000-02-29T03:04:05.000+00:01");
@Test
public void testLocalTime() {
final DateTimeComponents dtc = DateTimeComponents.parseDateTime("2000-02-29T03:04:05.000+00:01");
Assert.assertEquals(1, dtc.getTime().getMinutesFromUTC());
}
}
......@@ -103,6 +103,11 @@ public class TimeComponentsTest {
Assert.assertEquals("12:00:00.000", new TimeComponents(43200).toString());
Assert.assertEquals("18:00:00.000", new TimeComponents(64800).toString());
Assert.assertEquals("23:59:59.900", new TimeComponents(86399.9).toString());
Assert.assertEquals("00:00:00.000+10:00", new TimeComponents( 0, 0, 0, 600).toString());
Assert.assertEquals("06:00:00.000+10:00", new TimeComponents( 6, 0, 0, 600).toString());
Assert.assertEquals("12:00:00.000-04:30", new TimeComponents(12, 0, 0, -270).toString());
Assert.assertEquals("18:00:00.000-04:30", new TimeComponents(18, 0, 0, -270).toString());
Assert.assertEquals("23:59:59.900-04:30", new TimeComponents(23, 59, 59.9, -270).toString());
}
@Test
......@@ -112,9 +117,9 @@ public class TimeComponentsTest {
Assert.assertEquals(86399.9, TimeComponents.parseTime("23:59:59,900").getSecondsInDay(), 1.0e-10);
Assert.assertEquals(86399.9, TimeComponents.parseTime("235959.900Z").getSecondsInDay(), 1.0e-10);
Assert.assertEquals(86399.9, TimeComponents.parseTime("23:59:59.900Z").getSecondsInDay(), 1.0e-10);
Assert.assertEquals(86399.9, TimeComponents.parseTime("235959.900+00").getSecondsInDay(), 1.0e-10);
Assert.assertEquals(86399.9, TimeComponents.parseTime("235959.900+10").getSecondsInDay(), 1.0e-10);
Assert.assertEquals(86399.9, TimeComponents.parseTime("23:59:59.900+00").getSecondsInDay(), 1.0e-10);
Assert.assertEquals(86399.9, TimeComponents.parseTime("235959.900+00:00").getSecondsInDay(), 1.0e-10);
Assert.assertEquals(86399.9, TimeComponents.parseTime("235959.900-00:12").getSecondsInDay(), 1.0e-10);
Assert.assertEquals(86399.9, TimeComponents.parseTime("23:59:59.900+00:00").getSecondsInDay(), 1.0e-10);
}
......@@ -123,9 +128,9 @@ public class TimeComponentsTest {
TimeComponents.parseTime("23h59m59s");
}
@Test(expected=IllegalArgumentException.class)
public void testBadZone() {
TimeComponents.parseTime("23:59:59+01:00");
@Test
public void testLocalTime() {
Assert.assertEquals(60, TimeComponents.parseTime("23:59:59+01:00").getMinutesFromUTC());
}
@Test
......
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