Skip to content

Commit e3adfe0

Browse files
committed
Converted replay.date from string to a datetime and added replay.utc_date
1 parent 8e09a0d commit e3adfe0

File tree

5 files changed

+39
-7
lines changed

5 files changed

+39
-7
lines changed

sc2reader/parsers.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from time import ctime
1+
from math import ceil
2+
from datetime import datetime
23
from collections import defaultdict
34

45
from objects import Attribute, Message, Player, Event
@@ -136,7 +137,12 @@ def load(self, replay, filecontents):
136137

137138
replay.map = data[1].decode("hex")
138139
replay.file_time = data[5]
139-
replay.date = ctime( (data[5]-116444735995904000)/10000000 )
140+
141+
# TODO: This doesn't seem to produce exactly correct results.
142+
# This might be due to wrong value of the magic constant 116444735995904000
143+
# or rounding errors.
144+
replay.date = datetime.fromtimestamp((replay.file_time-116444735995904000)/10000000)
145+
replay.utc_date = datetime.utcfromtimestamp((replay.file_time-116444735995904000)/10000000)
140146

141147
replay.details_data = data
142148

sc2reader/replay.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ def __init__(self, replay, partial_parse=True, full_parse=True):
4242
#Used internally to ensure parse ordering
4343
self.__parsed = dict(details=False, attributes=False, messages=False, events=False, initdata=False)
4444

45-
# TODO: Change to something better
45+
# TODO: Test EPOCH differences between MacOsX and Windows
4646
# http://en.wikipedia.org/wiki/Epoch_(reference_date)
4747
# Notice that Windows and Mac have different EPOCHs, I wonder whether
4848
# this is different depending on the OS on which the replay was played.
49-
self.date = "" # Date when the game was played
49+
self.date = None # Date when the game was played in local time
50+
self.utc_date = None # Date when the game was played in UTC
5051

5152
#Make sure the file exists and is readable, first and foremost
5253
if not os.access(self.filename, os.F_OK):

shell.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@
22
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
33
from sc2reader import Replay
44
from mpyq import MPQArchive
5+
from datetime import datetime
6+
from time import gmtime
57

68
# replay = Replay("1.sc2replay")
79
#
810
# print replay.player["Nexpacisor"].avg_apm
9-
# print replay.player["dblrainbow"].apm
11+
# print replay.player["dblrainbow"].apm
12+
13+
replay = Replay("f.sc2replay", True, False)
14+
print replay.date
15+
print datetime.utcfromtimestamp((replay.file_time-116444735995904000)/10000000)
16+
print datetime.fromtimestamp((replay.file_time-116444735995904000)/10000000)

test_replays/build17811/info.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
Note: All datetimes in UTC+2
2+
13
1.SC2Replay
24
Date played 20 Feb 2011 22:44:48
35
Duration 32:47

test_replays/test_all.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Run tests with "py.test" in the project root dir
44
import os, sys
55
import pytest
6+
import datetime
67

78
sys.path.insert(0, os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)),"../")))
89

@@ -21,8 +22,7 @@ def test_empty():
2122

2223
def test_standard_1v1():
2324
replay = Replay("test_replays/build17811/1.SC2Replay")
24-
25-
# assert replay.date == "20 Feb 2011 22:44:48"
25+
2626
assert replay.length == (32, 47)
2727
assert replay.map == "Lost Temple"
2828
assert replay.build == 17811
@@ -152,3 +152,19 @@ def test_kr_realm_and_tampered_messages():
152152

153153
def test_encrypted():
154154
replay = Replay("test_replays/build17811/4.SC2Replay")
155+
156+
def test_datetimes():
157+
# Ignore seconds in comparisons, because they are off by one what is reported by Windows.
158+
# This might be a little nuance worth investigating at some point.
159+
160+
# Played at 20 Feb 2011 22:44:48 UTC+2
161+
replay = Replay("test_replays/build17811/1.SC2Replay")
162+
assert replay.utc_date == datetime.datetime(2011, 2, 20, 20, 44, 47)
163+
164+
# Played at 21 Feb 2011 00:42:13 UTC+2
165+
replay = Replay("test_replays/build17811/2.SC2Replay")
166+
assert replay.utc_date == datetime.datetime(2011, 2, 20, 22, 42, 12)
167+
168+
# Played at 25 Feb 2011 16:36:28 UTC+2
169+
replay = Replay("test_replays/build17811/3.SC2Replay")
170+
assert replay.utc_date == datetime.datetime(2011, 2, 25, 14, 36, 26)

0 commit comments

Comments
 (0)