Skip to content

Commit

Permalink
DateAxisItem: Add type annotations to newly added functions
Browse files Browse the repository at this point in the history
fix its tests: use C locale, which seems to be
available on Windows, Linux and MacOS
  • Loading branch information
Gatsik committed Jul 4, 2024
1 parent 33a8375 commit 191e089
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
31 changes: 22 additions & 9 deletions pyqtgraph/graphicsItems/DateAxisItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,19 @@ def __init__(self, tickSpecs, exampleText):
self.utcOffset = None
self.exampleText = exampleText

def extendTimeRangeForSpacing(self, spacing, minVal, maxVal):
def extendTimeRangeForSpacing(
self, spacing: int, minVal: int | float, maxVal: int | float,
) -> tuple[int | float, int | float]:
if spacing < HOUR_SPACING:
return minVal, maxVal

extendedMax = maxVal + abs(getPreferredOffsetFromUtc(maxVal, self.utcOffset))
extendedMin = minVal - abs(getPreferredOffsetFromUtc(minVal, self.utcOffset))
return extendedMin, extendedMax

def moveTicksToLocalTimeCoords(self, ticks, spacing, skipFactor):
def moveTicksToLocalTimeCoords(
self, ticks: np.ndarray, spacing: int, skipFactor: int,
) -> np.ndarray:
if len(ticks) == 0:
return ticks

Expand Down Expand Up @@ -232,36 +236,42 @@ def tickValues(self, minVal, maxVal, minSpc):
], "99:99:99")


def fromSecsSinceEpoch(timestamp):
def fromSecsSinceEpoch(timestamp: float | int) -> QDateTime:
try:
return QDateTime.fromSecsSinceEpoch(round(timestamp))
except OverflowError:
return QDateTime()


def calculateUtcOffset(timestamp):
def calculateUtcOffset(timestamp: float | int) -> int:
return -fromSecsSinceEpoch(timestamp).offsetFromUtc()


def getPreferredOffsetFromUtc(timestamp, preferred_offset=None):
def getPreferredOffsetFromUtc(
timestamp: float | int,
preferred_offset: int | None = None,
) -> int:
"""Retrieve the utc offset respecting the daylight saving time"""
if preferred_offset is not None:
return preferred_offset
return calculateUtcOffset(timestamp)


def adjustTimestampToPreferredUtcOffset(timestamp, offest=None):
def adjustTimestampToPreferredUtcOffset(
timestamp: float | int,
offest: int | None = None,
) -> int | float:
return timestamp - getPreferredOffsetFromUtc(timestamp, offest)


def offsetToLocalHour(timestamp):
def offsetToLocalHour(timestamp: float | int) -> int:
local = fromSecsSinceEpoch(timestamp)
roundedToHour = local.time()
roundedToHour.setHMS(roundedToHour.hour(), 0, 0)
return -roundedToHour.secsTo(local.time())


def applyOffsetFromUtc(timestamp):
def applyOffsetFromUtc(timestamp: float | int) -> int:
"""
UTC+4
1970-01-02 02:00 (local) == 1970-01-01 22:00 (UTC) -> 1970-01-01 22:00 (local)
Expand All @@ -276,7 +286,10 @@ def applyOffsetFromUtc(timestamp):
return repositioned.toSecsSinceEpoch()


def applyOffsetToUtc(timestamp, preferred_offset=None):
def applyOffsetToUtc(
timestamp: float | int,
preferred_offset: int | None = None,
) -> int:
delocalized = applyOffsetFromUtc(timestamp)
return getPreferredOffsetFromUtc(delocalized, preferred_offset)

Expand Down
9 changes: 5 additions & 4 deletions tests/graphicsItems/test_DateAxisItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
SEC_PER_YEAR,
SECOND_SPACING,
WEEK_SPACING,
YEAR_MONTH_ZOOM_LEVEL,
YEAR_SPACING,
ZoomLevel,
applyOffsetFromUtc,
Expand All @@ -35,7 +36,7 @@
def makeDateAxis():
axis = pg.DateAxisItem()
axis.fontMetrics = QFontMetrics(QFont())
axis.zoomLevel = None
axis.zoomLevel = YEAR_MONTH_ZOOM_LEVEL
return axis


Expand Down Expand Up @@ -85,8 +86,8 @@ def dateAxis():


@pytest.fixture(autouse=True)
def use_en_us_locale():
locale.setlocale(locale.LC_TIME, "en_US")
def use_c_locale():
locale.setlocale(locale.LC_TIME, "C")


def test_preferred_utc_offset_respects_chosen_offset():
Expand All @@ -108,7 +109,7 @@ def test_utc_offset_works_with_float_timestamp():
assert -16 * 3600 <= calculateUtcOffset(123456.0734) <= 16 * 3600


def test_shift_local_time_to_utc_time_does_what_it_promises_to_do():
def test_applyOffsetFromUtc_does_what_it_promises_to_do():
timeZone = QTimeZone(b"UTC+4")

startDate = QDateTime(QDate(1970, 1, 2), QTime(2, 0), timeZone)
Expand Down

0 comments on commit 191e089

Please sign in to comment.