Skip to content

Commit fa1b6d1

Browse files
committed
Replaced use of 2.7-and-later 'total_seconds' from timedelta.
1 parent 2f7a126 commit fa1b6d1

File tree

2 files changed

+78
-50
lines changed

2 files changed

+78
-50
lines changed

simplefix/message.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@ def append_tz_timestamp(self, tag, timestamp=None, precision=3,
315315
# Get offset of local timezone east of UTC.
316316
utc = datetime.datetime.utcfromtimestamp(now)
317317
local = datetime.datetime.fromtimestamp(now)
318-
offset = int((local - utc).total_seconds() / 60)
318+
td = local - utc
319+
offset = int(((td.days * 86400) + td.seconds) / 60)
319320

320321
s = local.strftime("%Y%m%d-%H:%M:%S")
321322
if precision == 3:
@@ -358,7 +359,8 @@ def append_tz_time_only(self, tag, timestamp=None, precision=3,
358359

359360
now = time.mktime(t.timetuple()) + (t.microsecond * 1e-6)
360361
utc = datetime.datetime.utcfromtimestamp(now)
361-
offset = int((t - utc).total_seconds() / 60)
362+
td = t - utc
363+
offset = int(((td.days * 86400) + td.seconds) / 60)
362364

363365
s = t.strftime("%H:%M")
364366
if precision == 0:

test/test_message.py

Lines changed: 74 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@
3131
from simplefix import FixMessage
3232

3333

34+
# NOTE: RHEL6 ships with Python 2.6, which is increasingly difficult to
35+
# support as both 2.7 and 3.x have added numerous features that seem like
36+
# they've been around forever, but actually aren't in 2.6.
37+
#
38+
# While in a few cases, I try to monkey-patch stuff up to 2.7-equivalent,
39+
# in a lot of the test code, I just skip the tests if they're run on
40+
# a 2.6 interpreter -- or I would but unittest in 2.6 doesn't understand
41+
# skipping, so they just pass without testing anything.
42+
#
43+
# This constant is used to check the version and alter behaviour to suit.
3444
VERSION = (sys.version_info[0] * 10) + sys.version_info[1]
3545

3646

@@ -58,7 +68,8 @@ def test_string_with_equals(self):
5868

5969
def test_string_without_equals(self):
6070
"""Test field set with string not containing equals sign"""
61-
if VERSION == 26: return
71+
if VERSION == 26:
72+
return
6273

6374
msg = FixMessage()
6475
with self.assertRaises(ValueError):
@@ -67,7 +78,8 @@ def test_string_without_equals(self):
6778

6879
def test_string_with_bad_tag(self):
6980
"""Test field set with bad tag in tag=value string"""
70-
if VERSION == 26: return
81+
if VERSION == 26:
82+
return
7183

7284
msg = FixMessage()
7385
with self.assertRaises(ValueError):
@@ -101,7 +113,8 @@ def test_set_session_version(self):
101113

102114
def test_get_repeating(self):
103115
"""Test retrieval of repeating field's value"""
104-
if VERSION == 26: return
116+
if VERSION == 26:
117+
return
105118

106119
pkt = FixMessage()
107120
pkt.append_pair(42, "a")
@@ -138,7 +151,8 @@ def test_raw_msg_type(self):
138151

139152
def test_empty_message(self):
140153
"""Test encoding of empty message"""
141-
if VERSION == 26: return
154+
if VERSION == 26:
155+
return
142156

143157
msg = FixMessage()
144158
with self.assertRaises(ValueError):
@@ -147,7 +161,8 @@ def test_empty_message(self):
147161

148162
def test_encode_no_35(self):
149163
"""Test encoding without MessageType(35) field"""
150-
if VERSION == 26: return
164+
if VERSION == 26:
165+
return
151166

152167
msg = FixMessage()
153168
msg.append_pair(8, "FIX.4.2")
@@ -157,7 +172,8 @@ def test_encode_no_35(self):
157172

158173
def test_encode_no_8(self):
159174
"""Test encoding without BeginString(8) field"""
160-
if VERSION == 26: return
175+
if VERSION == 26:
176+
return
161177

162178
msg = FixMessage()
163179
msg.append_pair(35, "D")
@@ -318,7 +334,8 @@ def test_time_seconds_only(self):
318334

319335
def test_time_bad_precision(self):
320336
"""Test bad time precision values"""
321-
if VERSION == 26: return
337+
if VERSION == 26:
338+
return
322339

323340
msg = FixMessage()
324341
t = 1484581872.933458
@@ -392,7 +409,8 @@ def test_utcts_seconds_only(self):
392409

393410
def test_utcts_bad_precision(self):
394411
"""Test UTCTimestamp bad time precision values"""
395-
if VERSION == 26: return
412+
if VERSION == 26:
413+
return
396414

397415
msg = FixMessage()
398416
t = 1484581872.933458
@@ -448,7 +466,8 @@ def test_utcto_seconds_only(self):
448466

449467
def test_utcto_bad_precision(self):
450468
"""Test UTCTimeOnly bad time precision values"""
451-
if VERSION == 26: return
469+
if VERSION == 26:
470+
return
452471

453472
msg = FixMessage()
454473
t = 1484581872.933458
@@ -475,7 +494,8 @@ def test_utcto_parts_15_51_12_933_458(self):
475494
return
476495

477496
def test_utcto_parts_bad_hour(self):
478-
if VERSION == 26: return
497+
if VERSION == 26:
498+
return
479499

480500
msg = FixMessage()
481501
with self.assertRaises(ValueError):
@@ -487,7 +507,8 @@ def test_utcto_parts_bad_hour(self):
487507
return
488508

489509
def test_utcto_parts_bad_minute(self):
490-
if VERSION == 26: return
510+
if VERSION == 26:
511+
return
491512

492513
msg = FixMessage()
493514
with self.assertRaises(ValueError):
@@ -499,7 +520,8 @@ def test_utcto_parts_bad_minute(self):
499520
return
500521

501522
def test_utcto_parts_bad_seconds(self):
502-
if VERSION == 26: return
523+
if VERSION == 26:
524+
return
503525

504526
msg = FixMessage()
505527
with self.assertRaises(ValueError):
@@ -511,7 +533,8 @@ def test_utcto_parts_bad_seconds(self):
511533
return
512534

513535
def test_utcto_parts_bad_ms(self):
514-
if VERSION == 26: return
536+
if VERSION == 26:
537+
return
515538

516539
msg = FixMessage()
517540
with self.assertRaises(ValueError):
@@ -523,7 +546,8 @@ def test_utcto_parts_bad_ms(self):
523546
return
524547

525548
def test_utcto_parts_bad_us(self):
526-
if VERSION == 26: return
549+
if VERSION == 26:
550+
return
527551

528552
msg = FixMessage()
529553
with self.assertRaises(ValueError):
@@ -535,7 +559,8 @@ def test_utcto_parts_bad_us(self):
535559
return
536560

537561
def test_offset_range(self):
538-
if VERSION == 26: return
562+
if VERSION == 26:
563+
return
539564

540565
msg = FixMessage()
541566
with self.assertRaises(ValueError):
@@ -582,6 +607,14 @@ def test_append_tzts_none(self):
582607
msg.append_tz_timestamp(1253, None)
583608
return
584609

610+
@staticmethod
611+
def calculate_tz_offset(t):
612+
local = datetime.datetime.fromtimestamp(t)
613+
utc = datetime.datetime.utcfromtimestamp(t)
614+
td = local - utc
615+
offset = int(((td.days * 86400) + td.seconds) / 60)
616+
return offset
617+
585618
def test_append_tzts_float(self):
586619
msg = FixMessage()
587620
t = 1484581872.933458
@@ -592,9 +625,7 @@ def test_append_tzts_float(self):
592625
(test.tm_year, test.tm_mon, test.tm_mday,
593626
test.tm_hour, test.tm_min, test.tm_sec,
594627
int((t - int(t)) * 1000))
595-
offset = int((datetime.datetime.fromtimestamp(t) -
596-
datetime.datetime.utcfromtimestamp(t)).total_seconds()
597-
/ 60)
628+
offset = self.calculate_tz_offset(t)
598629
if offset == 0:
599630
s += "Z"
600631
else:
@@ -619,9 +650,7 @@ def test_append_tzts_datetime(self):
619650
(test.tm_year, test.tm_mon, test.tm_mday,
620651
test.tm_hour, test.tm_min, test.tm_sec,
621652
int((t - int(t)) * 1000))
622-
offset = int((datetime.datetime.fromtimestamp(t) -
623-
datetime.datetime.utcfromtimestamp(t)).total_seconds()
624-
/ 60)
653+
offset = self.calculate_tz_offset(t)
625654
if offset == 0:
626655
s += "Z"
627656
else:
@@ -646,9 +675,7 @@ def test_tzts_microseconds(self):
646675
(test.tm_year, test.tm_mon, test.tm_mday,
647676
test.tm_hour, test.tm_min, test.tm_sec,
648677
int((t % 1) * 1e6))
649-
offset = int((datetime.datetime.fromtimestamp(t) -
650-
datetime.datetime.utcfromtimestamp(t)).total_seconds()
651-
/ 60)
678+
offset = self.calculate_tz_offset(t)
652679
if offset == 0:
653680
s += "Z"
654681
else:
@@ -672,9 +699,7 @@ def test_tzts_seconds_only(self):
672699
s = "%04u%02u%02u-%02u:%02u:%02u" % \
673700
(test.tm_year, test.tm_mon, test.tm_mday,
674701
test.tm_hour, test.tm_min, test.tm_sec)
675-
offset = int((datetime.datetime.fromtimestamp(t) -
676-
datetime.datetime.utcfromtimestamp(t)).total_seconds()
677-
/ 60)
702+
offset = self.calculate_tz_offset(t)
678703
if offset == 0:
679704
s += "Z"
680705
else:
@@ -690,7 +715,8 @@ def test_tzts_seconds_only(self):
690715

691716
def test_tzts_bad_precision(self):
692717
"""Test bad TZTimestamp precision value"""
693-
if VERSION == 26: return
718+
if VERSION == 26:
719+
return
694720

695721
msg = FixMessage()
696722
t = 1484581872.933458
@@ -706,9 +732,7 @@ def test_tzto_datetime(self):
706732
test = time.localtime(t)
707733
s = "%02u:%02u:%02u.%03u" % \
708734
(test.tm_hour, test.tm_min, test.tm_sec, int((t % 1) * 1e3))
709-
offset = int((datetime.datetime.fromtimestamp(t) -
710-
datetime.datetime.utcfromtimestamp(t)).total_seconds()
711-
/ 60)
735+
offset = self.calculate_tz_offset(t)
712736
if offset == 0:
713737
s += "Z"
714738
else:
@@ -730,9 +754,7 @@ def test_tzto_minutes(self):
730754

731755
test = time.localtime(t)
732756
s = "%02u:%02u" % (test.tm_hour, test.tm_min)
733-
offset = int((datetime.datetime.fromtimestamp(t) -
734-
datetime.datetime.utcfromtimestamp(t)).total_seconds()
735-
/ 60)
757+
offset = self.calculate_tz_offset(t)
736758
if offset == 0:
737759
s += "Z"
738760
else:
@@ -755,9 +777,7 @@ def test_tzto_microseconds(self):
755777
test = time.localtime(t)
756778
s = "%02u:%02u:%02u.%06u" % \
757779
(test.tm_hour, test.tm_min, test.tm_sec, int((t % 1) * 1e6))
758-
offset = int((datetime.datetime.fromtimestamp(t) -
759-
datetime.datetime.utcfromtimestamp(t)).total_seconds()
760-
/ 60)
780+
offset = self.calculate_tz_offset(t)
761781
if offset == 0:
762782
s += "Z"
763783
else:
@@ -780,9 +800,7 @@ def test_tzto_seconds_only(self):
780800
test = time.localtime(t)
781801
s = "%02u:%02u:%02u" % \
782802
(test.tm_hour, test.tm_min, test.tm_sec)
783-
offset = int((datetime.datetime.fromtimestamp(t) -
784-
datetime.datetime.utcfromtimestamp(t)).total_seconds()
785-
/ 60)
803+
offset = self.calculate_tz_offset(t)
786804
if offset == 0:
787805
s += "Z"
788806
else:
@@ -798,7 +816,8 @@ def test_tzto_seconds_only(self):
798816

799817
def test_tzto_bad_precision(self):
800818
"""Test bad TZTimeOnly precision value"""
801-
if VERSION == 26: return
819+
if VERSION == 26:
820+
return
802821

803822
msg = FixMessage()
804823
t = 1484581872.933458
@@ -855,7 +874,8 @@ def test_tzto_parts_15_51_12_933_458_150(self):
855874

856875
def test_tzto_parts_bad_hour(self):
857876
"""Test TZTimeOnly with out-of-range hour components"""
858-
if VERSION == 26: return
877+
if VERSION == 26:
878+
return
859879

860880
msg = FixMessage()
861881
with self.assertRaises(ValueError):
@@ -868,7 +888,8 @@ def test_tzto_parts_bad_hour(self):
868888

869889
def test_tzto_parts_bad_minute(self):
870890
"""Test TZTimeOnly with out-of-range minute components"""
871-
if VERSION == 26: return
891+
if VERSION == 26:
892+
return
872893

873894
msg = FixMessage()
874895
with self.assertRaises(ValueError):
@@ -881,7 +902,8 @@ def test_tzto_parts_bad_minute(self):
881902

882903
def test_tzto_parts_bad_seconds(self):
883904
"""Test TZTimeOnly with out-of-range seconds components"""
884-
if VERSION == 26: return
905+
if VERSION == 26:
906+
return
885907

886908
msg = FixMessage()
887909
with self.assertRaises(ValueError):
@@ -894,7 +916,8 @@ def test_tzto_parts_bad_seconds(self):
894916

895917
def test_tzto_parts_bad_ms(self):
896918
"""Test TZTimeOnly with out-of-range milliseconds components"""
897-
if VERSION == 26: return
919+
if VERSION == 26:
920+
return
898921

899922
msg = FixMessage()
900923
with self.assertRaises(ValueError):
@@ -907,7 +930,8 @@ def test_tzto_parts_bad_ms(self):
907930

908931
def test_tzto_parts_bad_us(self):
909932
"""Test TZTimeOnly with out-of-range microseconds components"""
910-
if VERSION == 26: return
933+
if VERSION == 26:
934+
return
911935

912936
msg = FixMessage()
913937
with self.assertRaises(ValueError):
@@ -943,7 +967,8 @@ def test_strings(self):
943967

944968
def test_contains(self):
945969
"""Test use of 'in' and 'not in' operators"""
946-
if VERSION == 26: return
970+
if VERSION == 26:
971+
return
947972

948973
msg = FixMessage()
949974
msg.append_strings(["8=FIX.4.4", "35=0"])
@@ -955,7 +980,8 @@ def test_contains(self):
955980

956981
def test_none_value(self):
957982
"""Test encoding of None value"""
958-
if VERSION == 26: return
983+
if VERSION == 26:
984+
return
959985

960986
msg = FixMessage()
961987
msg.append_pair(99999, None)

0 commit comments

Comments
 (0)