Skip to content
Browse files

Fix to handle cross-time-zone issues properly rather than always usin…

…g UTC

MutableDateTime.setDate(ReadableInstant)
  • Loading branch information...
1 parent 136667c commit cd24d5cb2a981ed5af3a3060399a13e010c33a6f @jodastephen jodastephen committed Oct 11, 2012
View
3 RELEASE-NOTES.txt
@@ -61,6 +61,9 @@ Bug fixes since 2.1
This used to fail before 1970 when the input had a milli-of-second field
Fixed to not throw an exception
+- MutableDateTime.setDate(ReadableInstant)
+ Fixed to handle cross-time-zone issues properly rather than always using UTC
+
- DateTimeFormatter.parseInto() [3522138]
The v2.0 changes to handle parsing of month/day on their own (for Feb 29th) cause
parseInto() to lose the year. This fix reverts behaviour to v1.x so that parseInto()
View
14 src/main/java/org/joda/time/MutableDateTime.java
@@ -896,16 +896,22 @@ public void setDate(final long instant) {
/**
* Set the date from another instant.
* The time part of this object will be unaffected.
+ * <p>
+ * If the input is a {@code ReadableDateTime} then it is converted to the
+ * same time-zone as this object before using the instant millis.
*
* @param instant an instant to copy the date from, time part ignored
* @throws IllegalArgumentException if the object is invalid
*/
public void setDate(final ReadableInstant instant) {
long instantMillis = DateTimeUtils.getInstantMillis(instant);
- Chronology instantChrono = DateTimeUtils.getInstantChronology(instant);
- DateTimeZone zone = instantChrono.getZone();
- if (zone != null) {
- instantMillis = zone.getMillisKeepLocal(DateTimeZone.UTC, instantMillis);
+ if (instant instanceof ReadableDateTime) {
+ ReadableDateTime rdt = (ReadableDateTime) instant;
+ Chronology instantChrono = DateTimeUtils.getChronology(rdt.getChronology());
+ DateTimeZone zone = instantChrono.getZone();
+ if (zone != null) {
+ instantMillis = zone.getMillisKeepLocal(getZone(), instantMillis);
+ }
}
setDate(instantMillis);
}
View
39 src/test/java/org/joda/time/TestMutableDateTime_Sets.java
@@ -275,6 +275,45 @@ public void testSetDate_RI2() {
assertEquals(501, test.getMillisOfSecond());
}
+ public void testSetDate_RI_same() {
+ MutableDateTime setter = new MutableDateTime(2010, 12, 3, 2, 24, 48, 501, DateTimeZone.forID("America/Los_Angeles"));
+ MutableDateTime test = new MutableDateTime(2010, 12, 3, 2, 24, 48, 501, DateTimeZone.forID("America/Los_Angeles"));
+ test.setDate(setter);
+ assertEquals(2010, test.getYear());
+ assertEquals(12, test.getMonthOfYear());
+ assertEquals(3, test.getDayOfMonth());
+ assertEquals(2, test.getHourOfDay());
+ assertEquals(24, test.getMinuteOfHour());
+ assertEquals(48, test.getSecondOfMinute());
+ assertEquals(501, test.getMillisOfSecond());
+ }
+
+ public void testSetDate_RI_different1() {
+ MutableDateTime setter = new MutableDateTime(2010, 12, 1, 0, 0, 0, 0, DateTimeZone.forID("America/Los_Angeles"));
+ MutableDateTime test = new MutableDateTime(2010, 12, 3, 2, 24, 48, 501, DateTimeZone.forID("Europe/Moscow"));
+ test.setDate(setter);
+ assertEquals(2010, test.getYear());
+ assertEquals(12, test.getMonthOfYear());
+ assertEquals(1, test.getDayOfMonth());
+ assertEquals(2, test.getHourOfDay());
+ assertEquals(24, test.getMinuteOfHour());
+ assertEquals(48, test.getSecondOfMinute());
+ assertEquals(501, test.getMillisOfSecond());
+ }
+
+ public void testSetDate_RI_different2() {
+ MutableDateTime setter = new MutableDateTime(2010, 12, 1, 0, 0, 0, 0, DateTimeZone.forID("Europe/Moscow"));
+ MutableDateTime test = new MutableDateTime(2010, 12, 3, 2, 24, 48, 501, DateTimeZone.forID("America/Los_Angeles"));
+ test.setDate(setter);
+ assertEquals(2010, test.getYear());
+ assertEquals(12, test.getMonthOfYear());
+ assertEquals(1, test.getDayOfMonth());
+ assertEquals(2, test.getHourOfDay());
+ assertEquals(24, test.getMinuteOfHour());
+ assertEquals(48, test.getSecondOfMinute());
+ assertEquals(501, test.getMillisOfSecond());
+ }
+
//-----------------------------------------------------------------------
public void testSetTime_int_int_int_int1() {
MutableDateTime test = new MutableDateTime(2002, 6, 9, 12, 24, 48, 501);

0 comments on commit cd24d5c

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