Skip to content
Browse files

ZonedDateTime epoch second factory takes nano-of-second

  • Loading branch information...
1 parent b161f07 commit 35c6d32b1f18cd87b0d2fc43c6f291fad05f0544 @jodastephen jodastephen committed Nov 30, 2012
Showing with 19 additions and 13 deletions.
  1. +12 −6 src/main/java/javax/time/ZonedDateTime.java
  2. +7 −7 src/tck/java/javax/time/TCKZonedDateTime.java
View
18 src/main/java/javax/time/ZonedDateTime.java
@@ -448,16 +448,22 @@ public static ZonedDateTime ofInstant(OffsetDateTime instantDateTime, ZoneId zon
* Obtains an instance of {@code ZonedDateTime} using seconds from the
* epoch of 1970-01-01T00:00:00Z.
* <p>
- * The nanosecond field is set to zero.
+ * This allows the {@link ChronoField#INSTANT_SECONDS epoch-second} field
+ * to be converted to a zoned date-time. This is primarily intended for
+ * low-level conversions rather than general application usage.
+ * <p>
+ * The epoch-second is equivalent to an instant and there is only one valid
+ * offset for each instant.
*
* @param epochSecond the number of seconds from the epoch of 1970-01-01T00:00:00Z
- * @param zone the time-zone, not null
+ * @param nanoOfSecond the nanosecond within the second, from 0 to 999,999,999
+ * @param zoneId the zone ID, not null
* @return the zoned date-time, not null
- * @throws DateTimeException if the result exceeds the supported date range
+ * @throws DateTimeException if the result exceeds the supported range
*/
- public static ZonedDateTime ofEpochSecond(long epochSecond, ZoneId zone) {
- Objects.requireNonNull(zone, "zone");
- return create(epochSecond, 0, zone);
+ public static ZonedDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneId zoneId) {
+ Objects.requireNonNull(zoneId, "zone");
+ return create(epochSecond, nanoOfSecond, zoneId);
}
/**
View
14 src/tck/java/javax/time/TCKZonedDateTime.java
@@ -624,8 +624,8 @@ public void factory_ofInstant_OffsetDateTime_nullZone() {
@Test(groups={"tck"})
public void factory_ofEpochSecond_longOffset_afterEpoch() {
for (int i = 0; i < 100000; i++) {
- ZonedDateTime test = ZonedDateTime.ofEpochSecond(i, ZONE_0200);
- OffsetDateTime odt = OffsetDateTime.of(1970, 1, 1, 0, 0, ZoneOffset.UTC).withOffsetSameInstant(OFFSET_0200).plusSeconds(i);
+ ZonedDateTime test = ZonedDateTime.ofEpochSecond(i, 45, ZONE_0200);
+ OffsetDateTime odt = OffsetDateTime.of(1970, 1, 1, 0, 0, 0, 45, ZoneOffset.UTC).withOffsetSameInstant(OFFSET_0200).plusSeconds(i);
assertEquals(test.getOffsetDateTime(), odt);
assertEquals(test.getZone(), ZONE_0200);
}
@@ -634,26 +634,26 @@ public void factory_ofEpochSecond_longOffset_afterEpoch() {
@Test(groups={"tck"})
public void factory_ofEpochSecond_longOffset_beforeEpoch() {
for (int i = 0; i < 100000; i++) {
- ZonedDateTime test = ZonedDateTime.ofEpochSecond(-i, ZONE_0200);
- OffsetDateTime odt = OffsetDateTime.of(1970, 1, 1, 0, 0, ZoneOffset.UTC).withOffsetSameInstant(OFFSET_0200).minusSeconds(i);
+ ZonedDateTime test = ZonedDateTime.ofEpochSecond(-i, 45, ZONE_0200);
+ OffsetDateTime odt = OffsetDateTime.of(1970, 1, 1, 0, 0, 0, 45, ZoneOffset.UTC).withOffsetSameInstant(OFFSET_0200).minusSeconds(i);
assertEquals(test.getOffsetDateTime(), odt);
assertEquals(test.getZone(), ZONE_0200);
}
}
@Test(expectedExceptions=DateTimeException.class, groups={"tck"})
public void factory_ofEpochSecond_longOffset_tooBig() {
- ZonedDateTime.ofEpochSecond(Long.MAX_VALUE, ZONE_PARIS); // TODO: better test
+ ZonedDateTime.ofEpochSecond(Long.MAX_VALUE, 999_999_999, ZONE_PARIS); // TODO: better test
}
@Test(expectedExceptions=DateTimeException.class, groups={"tck"})
public void factory_ofEpochSecond_longOffset_tooSmall() {
- ZonedDateTime.ofEpochSecond(Long.MIN_VALUE, ZONE_PARIS); // TODO: better test
+ ZonedDateTime.ofEpochSecond(Long.MIN_VALUE, 0, ZONE_PARIS); // TODO: better test
}
@Test(expectedExceptions=NullPointerException.class, groups={"tck"})
public void factory_ofEpochSecond_longOffset_nullOffset() {
- ZonedDateTime.ofEpochSecond(0L, null);
+ ZonedDateTime.ofEpochSecond(0L, 0, null);
}
//-----------------------------------------------------------------------

0 comments on commit 35c6d32

Please sign in to comment.
Something went wrong with that request. Please try again.