Skip to content

Commit 0a47e1c

Browse files
committed
Try to guess and handle timestamps in milliseconds
1 parent 143c481 commit 0a47e1c

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

BencodeNET.Tests/Parsing/TorrentParserTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,22 @@ public void CreationDate_IsParsed()
9898
torrent.CreationDate.Should().Be(new DateTime(2016, 1, 1));
9999
}
100100

101+
[Theory]
102+
[AutoMockedData]
103+
public void CreationDate_UnixTimeInMilliseconds_IsParsed()
104+
{
105+
// Arrange
106+
ParsedData = ValidSingleFileTorrentData;
107+
ParsedData[TorrentFields.CreationDate] = (BNumber) 1451606400000;
108+
109+
// Act
110+
var parser = new TorrentParser(BencodeParser);
111+
var torrent = parser.Parse((BencodeStream) null);
112+
113+
// Assert
114+
torrent.CreationDate.Should().Be(new DateTime(2016, 1, 1));
115+
}
116+
101117
[Theory]
102118
[InlineAutoMockedData("utf8")]
103119
[InlineAutoMockedData("UTF8")]

BencodeNET/Objects/BNumber.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ public static implicit operator bool(BNumber bint)
7272
public static implicit operator DateTime?(BNumber number)
7373
{
7474
if (number == null) return null;
75+
76+
if (number.Value > int.MaxValue)
77+
{
78+
// Assuming unix timestamp is in milliseconds instead of seconds
79+
return Epoch.AddMilliseconds(number);
80+
}
81+
7582
return Epoch.AddSeconds(number);
7683
}
7784

0 commit comments

Comments
 (0)