Commit c93107dd authored by Brickner_cp's avatar Brickner_cp

Fix Timestamp accuracy. Was millisecond, now microsecond.

parent 65a24f13
using System;
namespace PcapDotNet.Base
{
/// <summary>
/// Extension methods for DateTime.
/// </summary>
public static class DateTimeExtensions
{
/// <summary>
/// Returns a new DateTime that adds the specified number of microseconds to the value of this instance.
/// </summary>
/// <param name="dateTime">The DateTime to add microseconds to.</param>
/// <param name="value">A number of whole and fractional microseconds. The value parameter can be negative or positive. Note that this value is rounded to the nearest integer.</param>
/// <returns>An object whose value is the sum of the date and time represented by this instance and the number of microseconds represented by value.</returns>
public static DateTime AddMicroseconds(this DateTime dateTime, double value)
{
const long MaxValue = long.MaxValue / TimeSpanExtensions.TicksPerMicrosecond;
if (value > MaxValue)
{
throw new ArgumentOutOfRangeException("value", value,
string.Format("Value cannot be bigger than {0}", MaxValue));
}
const long MinValue = long.MinValue / TimeSpanExtensions.TicksPerMicrosecond;
if (value < MinValue)
{
throw new ArgumentOutOfRangeException("value", value,
string.Format("Value cannot be smaller than {0}", MinValue));
}
long roundedValue = (long)Math.Round(value);
long ticks = roundedValue * TimeSpanExtensions.TicksPerMicrosecond;
return dateTime.AddTicks(ticks);
}
}
}
\ No newline at end of file
......@@ -86,6 +86,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CharExtensions.cs" />
<Compile Include="DateTimeExtensions.cs" />
<Compile Include="EncodingExtensions.cs" />
<Compile Include="FuncExtensions.cs" />
<Compile Include="IDictionaryExtensions.cs" />
......
......@@ -7,6 +7,11 @@ namespace PcapDotNet.Base
/// </summary>
public static class TimeSpanExtensions
{
/// <summary>
/// Represents the number of ticks in 1 microsecond. This field is constant.
/// </summary>
public const long TicksPerMicrosecond = TimeSpan.TicksPerMillisecond / 1000;
/// <summary>
/// Divides the TimeSpan by a given value.
/// </summary>
......
using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PcapDotNet.Base;
using PcapDotNet.Packets;
using PcapDotNet.Packets.Ethernet;
using PcapDotNet.TestUtils;
namespace PcapDotNet.Core.Test
{
......@@ -65,6 +67,7 @@ namespace PcapDotNet.Core.Test
PacketCommunicatorReceiveResult result = communicator.ReceivePacket(out actualPacket);
Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result);
Assert.AreEqual(expectedPacket, actualPacket);
MoreAssert.IsInRange(expectedPacket.Timestamp.AddMicroseconds(-1), expectedPacket.Timestamp.AddMicroseconds(1), actualPacket.Timestamp);
}
}
......
......@@ -453,7 +453,7 @@ namespace PcapDotNet.Core.Test
case "frame.time_epoch":
double timeEpoch = double.Parse(field.Show());
DateTime fieldTimestamp = new DateTime(1970, 1, 1).AddSeconds(timeEpoch);
MoreAssert.IsInRange(fieldTimestamp.AddSeconds(-2), fieldTimestamp.AddSeconds(2), packet.Timestamp.ToUniversalTime(), "Timestamp");
MoreAssert.IsInRange(fieldTimestamp.AddMilliseconds(-1), fieldTimestamp.AddMilliseconds(1), packet.Timestamp.ToUniversalTime(), "Timestamp");
break;
case "frame.len":
......
......@@ -2,6 +2,7 @@
#include "Pcap.h"
using namespace System;
using namespace PcapDotNet::Base;
using namespace PcapDotNet::Core;
// static
......@@ -20,7 +21,9 @@ DateTime PacketTimestamp::MaximumPacketTimestamp::get()
void PacketTimestamp::PcapTimestampToDateTime(const timeval& pcapTimestamp, [Runtime::InteropServices::Out] DateTime% dateTime)
{
dateTime = DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind::Utc);
dateTime = dateTime.Add(TimeSpan::FromSeconds(pcapTimestamp.tv_sec) + TimeSpan::FromMilliseconds(((double)pcapTimestamp.tv_usec) / 1000));
TimeSpan seconds = TimeSpan::FromSeconds(pcapTimestamp.tv_sec);
TimeSpan microseconds = TimeSpan::FromTicks(pcapTimestamp.tv_usec * TimeSpanExtensions::TicksPerMicrosecond);
dateTime = dateTime.Add(seconds + microseconds);
dateTime = dateTime.ToLocalTime();
}
......@@ -30,7 +33,7 @@ void PacketTimestamp::DateTimeToPcapTimestamp(DateTime dateTime, timeval& pcapTi
dateTime = dateTime.ToUniversalTime();
TimeSpan timespan = dateTime - DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind::Utc);
pcapTimestamp.tv_sec = (long)timespan.TotalSeconds;
pcapTimestamp.tv_usec = (long)(timespan.Milliseconds * 1000);
pcapTimestamp.tv_usec = (long)((timespan.TotalMilliseconds - 1000 * (double)pcapTimestamp.tv_sec) * 1000);
}
// static
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment