Skip to content
This repository has been archived by the owner on Apr 21, 2021. It is now read-only.

Commit

Permalink
[FasterXML#71] use configured TimeZone when deserializing instants (l…
Browse files Browse the repository at this point in the history
…ongs) into LocalDate/LocalDateTime
  • Loading branch information
nezda committed Aug 26, 2015
1 parent 0a1feaf commit dc79e33
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import java.io.IOException;

import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
Expand Down Expand Up @@ -37,7 +37,7 @@ public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws I
: _format.createParser(ctxt).parseLocalDate(str);
}
if (p.getCurrentToken() == JsonToken.VALUE_NUMBER_INT) {
return new LocalDate(p.getLongValue());
return new LocalDate(p.getLongValue(), DateTimeZone.forTimeZone(ctxt.getTimeZone()));
}

// [yyyy,mm,dd] or ["yyyy","mm","dd"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;

import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;

import com.fasterxml.jackson.core.JsonParser;
Expand Down Expand Up @@ -38,7 +39,7 @@ public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt)
return (str.length() == 0) ? null
: _format.createParser(ctxt).parseLocalDateTime(str);
case VALUE_NUMBER_INT:
return new LocalDateTime(p.getLongValue());
return new LocalDateTime(p.getLongValue(), DateTimeZone.forTimeZone(ctxt.getTimeZone()));
case START_ARRAY:
// [yyyy,mm,dd,hh,MM,ss,ms]
if (p.isExpectedStartArrayToken()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,16 @@ public void testLocalDateDeser() throws IOException
assertNull(MAPPER.readValue(quote(""), LocalDate.class));
}

public void testLocalDateDeserWithTimeZone() throws IOException
public void testLocalDateDeserWithTimeZone() throws IOException
{
final String trickyInstant = "1238558582001";
// MAPPER is using default TimeZone (GMT)
LocalDate date3 = MAPPER.readValue(trickyInstant, LocalDate.class);
assertEquals(2009, date3.getYear());
assertEquals(4, date3.getMonthOfYear());
assertEquals(1, date3.getDayOfMonth());
assertEquals(ISOChronology.getInstanceUTC(), date3.getChronology());

MAPPER.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));

// couple of acceptable formats, so:
Expand All @@ -164,6 +172,14 @@ public void testLocalDateDeserWithTimeZone() throws IOException

// since 1.6.1, for [JACKSON-360]
assertNull(MAPPER.readValue(quote(""), LocalDate.class));

MAPPER.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));

LocalDate date4 = MAPPER.readValue(trickyInstant, LocalDate.class);
assertEquals(2009, date4.getYear());
assertEquals(3, date4.getMonthOfYear());
assertEquals(31, date4.getDayOfMonth());
assertEquals(ISOChronology.getInstanceUTC(), date4.getChronology());
}

public void testLocalDateDeserWithTypeInfo() throws IOException
Expand Down Expand Up @@ -313,6 +329,17 @@ public void testLocalDateTimeDeser() throws IOException

// since 1.6.1, for [JACKSON-360]
assertNull(MAPPER.readValue(quote(""), LocalDateTime.class));

// MAPPER is using default TimeZone (GMT)
LocalDateTime date3 = MAPPER.readValue("1238558582001", LocalDateTime.class);
assertEquals(2009, date3.getYear());
assertEquals(4, date3.getMonthOfYear());
assertEquals(1, date3.getDayOfMonth());
assertEquals(4, date3.getHourOfDay());
assertEquals(3, date3.getMinuteOfHour());
assertEquals(2, date3.getSecondOfMinute());
assertEquals(1, date3.getMillisOfSecond());
assertEquals(ISOChronology.getInstanceUTC(), date3.getChronology());
}

public void testLocalDateTimeDeserWithTimeZone() throws IOException
Expand Down

0 comments on commit dc79e33

Please sign in to comment.