mirror of
https://github.com/plexusorg/toml4j.git
synced 2025-02-11 11:40:27 +00:00
Timezone can be set on TomlWriter.
DateConverterJdk6 class handles Java 6 date formatting limitations.
This commit is contained in:
parent
a6e31da5ca
commit
b59a246b86
5 changed files with 67 additions and 12 deletions
|
@ -3,6 +3,7 @@ package com.moandjiezana.toml;
|
|||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
@ -10,6 +11,7 @@ import java.util.regex.Pattern;
|
|||
class DateConverter implements ValueConverter, ValueWriter {
|
||||
|
||||
static final DateConverter DATE_PARSER = new DateConverter();
|
||||
static final DateConverter DATE_PARSER_JDK_6 = new DateConverterJdk6();
|
||||
private static final Pattern DATE_REGEX = Pattern.compile("(\\d{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9])(\\.\\d*)?(Z|(?:[+\\-]\\d{2}:\\d{2}))(.*)");
|
||||
|
||||
@Override
|
||||
|
@ -89,8 +91,8 @@ class DateConverter implements ValueConverter, ValueWriter {
|
|||
|
||||
@Override
|
||||
public void write(Object value, WriterContext context) {
|
||||
DateFormat dateFormat = context.getDateFormat();
|
||||
context.write(dateFormat.format(value));
|
||||
DateFormat formatter = getFormatter(context.getTimeZone());
|
||||
context.write(formatter.format(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -98,8 +100,38 @@ class DateConverter implements ValueConverter, ValueWriter {
|
|||
return true;
|
||||
}
|
||||
|
||||
private DateFormat getFormatter(TimeZone timeZone) {
|
||||
String format = "UTC".equals(timeZone.getID()) ? "yyyy-MM-dd'T'HH:m:ss'Z'" : "yyyy-MM-dd'T'HH:m:ssXXX";
|
||||
SimpleDateFormat formatter = new SimpleDateFormat(format);
|
||||
formatter.setTimeZone(timeZone);
|
||||
|
||||
return formatter;
|
||||
}
|
||||
|
||||
private DateConverter() {}
|
||||
|
||||
private static class DateConverterJdk6 extends DateConverter {
|
||||
@Override
|
||||
public void write(Object value, WriterContext context) {
|
||||
TimeZone timeZone = context.getTimeZone();
|
||||
DateFormat formatter = getFormatter(timeZone);
|
||||
String date = formatter.format(value);
|
||||
|
||||
if ("UTC".equals(timeZone.getID())) {
|
||||
context.write(date);
|
||||
} else {
|
||||
context.write(date.substring(0, 22)).write(':').write(date.substring(22));
|
||||
}
|
||||
}
|
||||
|
||||
private DateFormat getFormatter(TimeZone timeZone) {
|
||||
String format = "UTC".equals(timeZone.getID()) ? "yyyy-MM-dd'T'HH:m:ss'Z'" : "yyyy-MM-dd'T'HH:m:ssZ";
|
||||
SimpleDateFormat formatter = new SimpleDateFormat(format);
|
||||
formatter.setTimeZone(timeZone);
|
||||
return formatter;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "datetime";
|
||||
|
|
|
@ -50,6 +50,12 @@ public class TomlWriter {
|
|||
return this;
|
||||
}
|
||||
|
||||
public TomlWriter.Builder timeZone(TimeZone timeZone) {
|
||||
this.timeZone = timeZone;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param spaces number of spaces to put between opening square bracket and first item and between closing square bracket and last item
|
||||
* @return this TomlWriter.Builder instance
|
||||
|
|
|
@ -2,6 +2,7 @@ package com.moandjiezana.toml;
|
|||
|
||||
import static com.moandjiezana.toml.BooleanConverter.BOOLEAN_PARSER;
|
||||
import static com.moandjiezana.toml.DateConverter.DATE_PARSER;
|
||||
import static com.moandjiezana.toml.DateConverter.DATE_PARSER_JDK_6;
|
||||
import static com.moandjiezana.toml.MapValueWriter.MAP_VALUE_WRITER;
|
||||
import static com.moandjiezana.toml.NumberConverter.NUMBER_PARSER;
|
||||
import static com.moandjiezana.toml.ObjectValueWriter.OBJECT_VALUE_WRITER;
|
||||
|
@ -29,8 +30,12 @@ class ValueWriters {
|
|||
|
||||
private ValueWriters() {}
|
||||
|
||||
private static DateConverter getPlatformSpecificDateConverter() {
|
||||
return Runtime.class.getPackage().getSpecificationVersion().startsWith("1.6") ? DATE_PARSER_JDK_6 : DATE_PARSER;
|
||||
}
|
||||
|
||||
private static final ValueWriter[] VALUE_WRITERs = {
|
||||
STRING_PARSER, NUMBER_PARSER, BOOLEAN_PARSER, DATE_PARSER,
|
||||
STRING_PARSER, NUMBER_PARSER, BOOLEAN_PARSER, getPlatformSpecificDateConverter(),
|
||||
MAP_VALUE_WRITER, PRIMITIVE_ARRAY_VALUE_WRITER, TABLE_ARRAY_VALUE_WRITER
|
||||
};
|
||||
}
|
||||
|
|
|
@ -2,8 +2,6 @@ package com.moandjiezana.toml;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.TimeZone;
|
||||
|
||||
|
@ -108,12 +106,8 @@ class WriterContext {
|
|||
}
|
||||
}
|
||||
|
||||
DateFormat getDateFormat() {
|
||||
String format = "yyyy-MM-dd'T'HH:m:ss'Z'";
|
||||
SimpleDateFormat formatter = new SimpleDateFormat(format);
|
||||
formatter.setTimeZone(timeZone);
|
||||
|
||||
return formatter;
|
||||
TimeZone getTimeZone() {
|
||||
return timeZone;
|
||||
}
|
||||
|
||||
WriterContext setIsArrayOfTable(boolean isArrayOfTable) {
|
||||
|
|
|
@ -16,6 +16,7 @@ import java.net.URI;
|
|||
import java.net.URL;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
@ -350,6 +351,23 @@ public class TomlWriterTest {
|
|||
assertEquals(expected, new TomlWriter().write(new WithWrappers()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_use_specified_time_zone() throws Exception {
|
||||
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
|
||||
calendar.set(2015, Calendar.JULY, 1, 11, 15, 30);
|
||||
calendar.set(Calendar.MILLISECOND, 0);
|
||||
|
||||
Map<String, Date> o = new HashMap<String, Date>();
|
||||
|
||||
o.put("sast", calendar.getTime());
|
||||
|
||||
TomlWriter writer = new TomlWriter.Builder().
|
||||
timeZone(TimeZone.getTimeZone("Africa/Johannesburg")).
|
||||
build();
|
||||
|
||||
assertEquals("sast = 2015-07-01T13:15:30+02:00\n", writer.write(o));
|
||||
}
|
||||
|
||||
private static class SimpleTestClass {
|
||||
int a = 1;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue