Skip to content

Commit

Permalink
Merge pull request #1 from nickpiggott/master
Browse files Browse the repository at this point in the history
Calculate local time values correctly
  • Loading branch information
nickpiggott authored Jul 21, 2020
2 parents ce4a93d + f61f996 commit 452996f
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/spi/binary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import datetime, dateutil.tz
import logging
import sys
from datetime import timedelta

logger = logging.getLogger("spi.binary")

Expand Down Expand Up @@ -417,6 +418,8 @@ def encode_timepoint(timepoint):

bits = bitarray(1)
bits.setall(False)
offset = (timepoint.utcoffset().days * 86400 + timepoint.utcoffset().seconds) + (timepoint.dst().days * 86400 + timepoint.dst().days)
timepoint = timepoint - timedelta(seconds=offset)

# b0: RFA(0)

Expand All @@ -425,7 +428,7 @@ def encode_timepoint(timepoint):
y = timepoint.year + 4800 - a
m = timepoint.month + (12 * a) - 3
jdn = timepoint.day + ((153 * m) + 2) / 5 + (365 * y) + (y / 4) - (y / 100) + (y / 400) - 32045
jd = jdn + (timepoint.hour - 12) / 24 + timepoint.minute / 1440 + timepoint.second / 86400
jd = jdn + timepoint.hour / 24 + timepoint.minute / 1440 + timepoint.second / 86400
mjd = (int)(jd - 2400000.5)
bits += encode_number(mjd, 17)

Expand Down Expand Up @@ -454,7 +457,6 @@ def encode_timepoint(timepoint):
# b32/48: LTO
if bits[19]:
bits += bitarray('00') # b49-50: RFA(0)
offset = (timepoint.utcoffset().days * 86400 + timepoint.utcoffset().seconds) + (timepoint.dst().days * 86400 + timepoint.dst().days)
bits += bitarray('0' if offset > 0 else '1') # b51: LTO sign
bits += encode_number(offset / (60 * 60) * 2, 5) # b52-56: Half hours

Expand Down

0 comments on commit 452996f

Please sign in to comment.