Skip to content

Commit

Permalink
Add a LongConversion for micro timestamps, closed #99
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-lawrey committed Jul 22, 2018
1 parent efd679f commit b70dd6a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package net.openhft.chronicle.wire;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;

public class MicroTimestampLongConverter implements LongConverter {
final DateTimeFormatter dtf = new DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd'T'HH:mm:ss")
.appendFraction(ChronoField.MICRO_OF_SECOND, 0, 6, true)
.toFormatter();

@Override
public long parse(CharSequence text) {
TemporalAccessor parse = dtf.parse(text);
long time = parse.getLong(ChronoField.EPOCH_DAY) * 86400_000_000L;
if (parse.isSupported(ChronoField.MICRO_OF_DAY))
time += parse.getLong(ChronoField.MICRO_OF_DAY);
else if (parse.isSupported(ChronoField.MILLI_OF_DAY))
time += parse.getLong(ChronoField.MILLI_OF_DAY) * 1_000L;
else if (parse.isSupported(ChronoField.SECOND_OF_DAY))
time += parse.getLong(ChronoField.SECOND_OF_DAY) * 1_000_000L;

return time;
}

@Override
public void append(StringBuilder text, long value) {
LocalDateTime ldt = LocalDateTime.ofEpochSecond(
value / 1_000_000,
(int) (value % 1_000_000 * 1000),
ZoneOffset.UTC);
dtf.formatTo(ldt, text);
}
}
36 changes: 34 additions & 2 deletions src/test/java/net/openhft/chronicle/wire/MethodWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.StringWriter;

import static junit.framework.TestCase.assertFalse;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -96,20 +97,51 @@ public void testNoArgs() {
"---\n" +
"methodTwo: \"\"\n" +
"---\n", wire.toString());
NoArgs mock = EasyMock.createMock(NoArgs.class);
NoArgs mock = createMock(NoArgs.class);
mock.methodOne();
mock.methodTwo();
EasyMock.replay(mock);
MethodReader reader = wire.methodReader(mock);
for (int i = 0; i < 3; i++)
assertEquals(i < 2, reader.readOne());
EasyMock.verify(mock);
verify(mock);
}

public interface NoArgs {
void methodOne();

void methodTwo();
}

@Test
public void testMicroTS() {
Wire wire = new TextWire(Bytes.elasticHeapByteBuffer(256));
HasMicroTS writer = wire.methodWriter(HasMicroTS.class);
long now = 1532251709775811L; //TimeProvider.get().currentTimeMicros();
// System.out.println(now);
MicroTS microTS = new MicroTS();
microTS.timeUS = now;
writer.microTS(microTS);
assertEquals("microTS: {\n" +
" timeUS: \"2018-07-22T09:28:29.775811\"\n" +
"}\n" +
"---\n", wire.toString());
HasMicroTS mock = createMock(HasMicroTS.class);
MethodReader reader = wire.methodReader(mock);
mock.microTS(microTS);
replay(mock);
for (int i = 0; i < 2; i++)
assertEquals(i < 1, reader.readOne());
verify(mock);
}

public interface HasMicroTS {
void microTS(MicroTS microTS);
}

static class MicroTS extends AbstractBytesMarshallable {
@LongConversion(MicroTimestampLongConverter.class)
long timeUS;
}
}

0 comments on commit b70dd6a

Please sign in to comment.