Permalink
Browse files

Better set of OffsetDateTime factories

  • Loading branch information...
1 parent 06574a4 commit 342f782f70d03d9e3fabe8c7ab0f61d0c7ea1d06 @jodastephen jodastephen committed Dec 1, 2012
@@ -1128,7 +1128,7 @@ public LocalDate minusDays(long daysToSubtract) {
* @return the offset date-time formed from this date and the specified time, not null
*/
public OffsetDateTime atTime(OffsetTime offsetTime) {
- return OffsetDateTime.of(this, offsetTime);
+ return OffsetDateTime.of(this, offsetTime.getTime(), offsetTime.getOffset());
}
/**
@@ -31,6 +31,8 @@
*/
package javax.time;
+import static javax.time.calendrical.ChronoField.NANO_OF_SECOND;
+
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
@@ -295,6 +297,8 @@ public static OffsetDateTime of(
//-----------------------------------------------------------------------
/**
* Obtains an instance of {@code OffsetDateTime} from a date, time and offset.
+ * <p>
+ * This creates an offset date-time with the specified local date, time and offset.
*
* @param date the local date, not null
* @param time the local time, not null
@@ -307,19 +311,9 @@ public static OffsetDateTime of(LocalDate date, LocalTime time, ZoneOffset offse
}
/**
- * Obtains an instance of {@code OffsetDateTime} from a local date and offset time.
- *
- * @param date the local date, not null
- * @param offsetTime the offset time to use, not null
- * @return the offset date-time, not null
- */
- public static OffsetDateTime of(LocalDate date, OffsetTime offsetTime) {
- LocalDateTime dt = LocalDateTime.of(date, offsetTime.getTime());
- return new OffsetDateTime(dt, offsetTime.getOffset());
- }
-
- /**
* Obtains an instance of {@code OffsetDateTime} from a date-time and offset.
+ * <p>
+ * This creates an offset date-time with the specified local date-time and offset.
*
* @param dateTime the local date-time, not null
* @param offset the zone offset, not null
@@ -329,56 +323,38 @@ public static OffsetDateTime of(LocalDateTime dateTime, ZoneOffset offset) {
return new OffsetDateTime(dateTime, offset);
}
- //-----------------------------------------------------------------------
/**
- * Obtains an instance of {@code OffsetDateTime} from an {@code Instant}
- * using the UTC offset.
+ * Obtains an instance of {@code OffsetDateTime} from a {@code ZonedDateTime}.
* <p>
- * The resulting date-time represents exactly the same instant on the time-line.
- * Calling {@link #toInstant()} will return an instant equal to the one used here.
+ * This creates an offset date-time with the same local date-time and offset as
+ * the zoned date-time. The result will have the same instant as the input.
*
- * @param instant the instant to create a date-time from, not null
- * @return the offset date-time in UTC, not null
+ * @param zonedDateTime the zoned date-time to convert from, not null
+ * @return the offset date-time, not null
* @throws DateTimeException if the result exceeds the supported range
*/
- public static OffsetDateTime ofInstantUTC(Instant instant) {
- return ofInstant(instant, ZoneOffset.UTC);
+ public static OffsetDateTime of(ZonedDateTime zonedDateTime) {
+ Objects.requireNonNull(zonedDateTime, "zonedDateTime");
+ return new OffsetDateTime(zonedDateTime.getDateTime(), zonedDateTime.getOffset());
}
+ //-----------------------------------------------------------------------
/**
- * Obtains an instance of {@code OffsetDateTime} from an {@code Instant} and offset.
+ * Obtains an instance of {@code OffsetDateTime} from an {@code Instant} and zone ID.
* <p>
- * The resulting date-time represents exactly the same instant on the time-line.
- * Calling {@link #toInstant()} will return an instant equal to the one used here.
+ * This creates an offset date-time with the same instant as that specified.
+ * The result will have the same instant as the input.
*
* @param instant the instant to create the date-time from, not null
- * @param offset the zone offset to use, not null
+ * @param offset the zone offset, not null
* @return the offset date-time, not null
* @throws DateTimeException if the result exceeds the supported range
*/
public static OffsetDateTime ofInstant(Instant instant, ZoneOffset offset) {
Objects.requireNonNull(instant, "instant");
Objects.requireNonNull(offset, "offset");
- return create(instant.getEpochSecond(), instant.getNano(), offset);
- }
-
- /**
- * Obtains an instance of {@code OffsetDateTime} from an {@code Instant} and time-zone.
- * <p>
- * The resulting date-time represents exactly the same instant on the time-line.
- * Calling {@link #toInstant()} will return an instant equal to the one used here.
- * Converting an instant to a zoned date-time is simple as there is only one valid
- * offset for each instant.
- *
- * @param instant the instant to create the date-time from, not null
- * @param zone the time-zone to use, not null
- * @return the offset date-time, not null
- * @throws DateTimeException if the result exceeds the supported range
- */
- public static OffsetDateTime ofInstant(Instant instant, ZoneId zone) {
- Objects.requireNonNull(instant, "instant");
- Objects.requireNonNull(zone, "zone");
- return create(instant.getEpochSecond(), instant.getNano(), zone.getRules().getOffset(instant));
+ LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
+ return new OffsetDateTime(ldt, offset);
}
//-----------------------------------------------------------------------
@@ -389,9 +365,6 @@ public static OffsetDateTime ofInstant(Instant instant, ZoneId zone) {
* This allows the {@link ChronoField#INSTANT_SECONDS epoch-second} field
* to be converted to an offset 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 nanoOfSecond the nanosecond within the second, from 0 to 999,999,999
@@ -400,22 +373,8 @@ public static OffsetDateTime ofInstant(Instant instant, ZoneId zone) {
* @throws DateTimeException if the result exceeds the supported range
*/
public static OffsetDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) {
- Objects.requireNonNull(offset, "offset");
- return create(epochSecond, nanoOfSecond, offset);
- }
-
- /**
- * Obtains an instance of {@code OffsetDateTime} using seconds from the
- * epoch of 1970-01-01T00:00:00Z.
- *
- * @param epochSecond the number of seconds from the epoch of 1970-01-01T00:00:00Z
- * @param nanoOfSecond the nanosecond within the second, from 0 to 999,999,999
- * @return the offset date-time, not null
- * @throws DateTimeException if the result exceeds the supported range
- */
- static OffsetDateTime create(long epochSecond, int nanoOfSecond, ZoneOffset offset) {
- LocalDateTime ldt = LocalDateTime.ofEpochSecond(epochSecond, nanoOfSecond, offset);
- return new OffsetDateTime(ldt, offset);
+ NANO_OF_SECOND.checkValidValue(nanoOfSecond);
+ return ofInstant(Instant.ofEpochSecond(epochSecond, nanoOfSecond), offset);
}
//-----------------------------------------------------------------------
@@ -495,7 +495,7 @@ public static ZonedDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, Zo
private static ZonedDateTime create(long epochSecond, int nanoOfSecond, ZoneId zone) {
ZoneRules rules = zone.getRules(); // latest rules version
Instant instant = Instant.ofEpochSecond(epochSecond, nanoOfSecond); // TODO: rules should be queryable by epochSeconds
- OffsetDateTime offsetDT = OffsetDateTime.create(epochSecond, nanoOfSecond, rules.getOffset(instant));
+ OffsetDateTime offsetDT = OffsetDateTime.ofInstant(instant, rules.getOffset(instant));
return new ZonedDateTime(offsetDT, zone);
}
@@ -2241,7 +2241,7 @@ public String toString() {
public boolean print(DateTimePrintContext context, StringBuilder buf) {
// TODO: implement this from INSTANT_SECONDS, handling big numbers
Instant instant = Instant.from(context.getDateTime());
- OffsetDateTime odt = OffsetDateTime.ofInstantUTC(instant);
+ OffsetDateTime odt = OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
buf.append(odt);
return true;
}
@@ -399,27 +399,6 @@ public void factory_of_LocalDateTimeZoneOffset_nullOffset() {
}
//-----------------------------------------------------------------------
- @Test(groups={"tck"})
- public void factory_of_LocalDateOffsetTime() {
- LocalDate date = LocalDate.of(2008, 6, 30);
- OffsetTime time = OffsetTime.of(11, 30, 10, 500, OFFSET_PONE);
- OffsetDateTime test = OffsetDateTime.of(date, time);
- check(test, 2008, 6, 30, 11, 30, 10, 500, OFFSET_PONE);
- }
-
- @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
- public void factory_of_LocalDateOffsetTime_nullLocalDate() {
- OffsetTime time = OffsetTime.of(11, 30, 10, 500, OFFSET_PONE);
- OffsetDateTime.of((LocalDate) null, time);
- }
-
- @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
- public void factory_of_LocalDateOffsetTime_nullOffsetTime() {
- LocalDate date = LocalDate.of(2008, 6, 30);
- OffsetDateTime.of(date, (OffsetTime) null);
- }
-
- //-----------------------------------------------------------------------
// ofEpochSecond()
//-----------------------------------------------------------------------
@Test(groups={"tck"})
@@ -46,30 +46,6 @@
private static final ZoneOffset OFFSET_MIN = ZoneOffset.ofHours(-18);
//-----------------------------------------------------------------------
- private void check(OffsetDateTime test, int y, int mo, int d, int h, int m, int s, int n, ZoneOffset offset) {
- assertEquals(test.getYear(), y);
- assertEquals(test.getMonth().getValue(), mo);
- assertEquals(test.getDayOfMonth(), d);
- assertEquals(test.getHour(), h);
- assertEquals(test.getMinute(), m);
- assertEquals(test.getSecond(), s);
- assertEquals(test.getNano(), n);
- assertEquals(test.getOffset(), offset);
- }
-
- //-----------------------------------------------------------------------
- public void factory_ofInstantUTC() {
- Instant instant = Instant.ofEpochSecond(86400 + 5 * 3600 + 10 * 60 + 20);
- OffsetDateTime test = OffsetDateTime.ofInstantUTC(instant);
- check(test, 1970, 1, 2, 5, 10, 20, 0, ZoneOffset.UTC);
- }
-
- @Test(expectedExceptions=NullPointerException.class)
- public void factory_ofInstantUTC_nullInstant() {
- OffsetDateTime.ofInstantUTC((Instant) null);
- }
-
- //-----------------------------------------------------------------------
@Test(expectedExceptions=NullPointerException.class)
public void factory_ofInstant_nullInstant() {
OffsetDateTime.ofInstant((Instant) null, OFFSET_PONE);

0 comments on commit 342f782

Please sign in to comment.