-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a LongConversion for micro timestamps, closed #99
- Loading branch information
1 parent
efd679f
commit b70dd6a
Showing
2 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/main/java/net/openhft/chronicle/wire/MicroTimestampLongConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters