Skip to content

Commit

Permalink
Fix micro second timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
wnagele committed May 3, 2015
1 parent 75143d2 commit 6b9a6f3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public class PcapReader implements Iterable<Packet> {
private LinkType linkType;
private long snapLen;
private boolean caughtEOF = false;
// MathContext for BigDecimal to preserve only 16 decimal digits
private MathContext ts_mc = new MathContext(16);

private MathContext tsUsecMc = new MathContext(16);

//To read reversed-endian PCAPs; the header is the only part that switches
private boolean reverseHeaderByteOrder = false;
Expand Down Expand Up @@ -170,10 +170,8 @@ private Packet nextPacket() {
long packetTimestampMicros = PcapReaderUtil.convertInt(pcapPacketHeader, TIMESTAMP_MICROS_OFFSET, reverseHeaderByteOrder);
packet.put(Packet.TIMESTAMP_MICROS, packetTimestampMicros);

// Prepare the timestamp with a BigDecimal to include microseconds
BigDecimal packetTimestampUsec = new BigDecimal(packetTimestamp
+ (double) packetTimestampMicros/1000000, ts_mc);
packet.put(Packet.TS_USEC, packetTimestampUsec.longValue());
BigDecimal packetTimestampUsec = new BigDecimal(packetTimestamp + packetTimestampMicros / 1000000.0, tsUsecMc);
packet.put(Packet.TIMESTAMP_USEC, packetTimestampUsec.doubleValue());

long packetSize = PcapReaderUtil.convertInt(pcapPacketHeader, CAP_LEN_OFFSET, reverseHeaderByteOrder);
packetData = new byte[(int)packetSize];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class Packet extends HashMap<String, Object> {
private static final long serialVersionUID = 8723206921174160146L;

public static final String TIMESTAMP = "ts";
public static final String TIMESTAMP_MICROS = "tsmicros";
public static final String TS_USEC = "ts_usec";
public static final String TIMESTAMP_USEC = "ts_usec";
public static final String TIMESTAMP_MICROS = "ts_micros";
public static final String TTL = "ttl";
public static final String IP_VERSION = "ip_version";
public static final String IP_HEADER_LENGTH = "ip_header_length";
Expand Down

0 comments on commit 6b9a6f3

Please sign in to comment.