1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
import locale
from contextlib import contextmanager
from functools import lru_cache
from itertools import product
from unittest import mock
import pytest
import pyqtgraph as pg
from pyqtgraph.graphicsItems.DateAxisItem import (
DAY_HOUR_ZOOM_LEVEL,
DAY_SPACING,
HMS_ZOOM_LEVEL,
HOUR_MINUTE_ZOOM_LEVEL,
HOUR_SPACING,
MINUTE_SPACING,
MONTH_SPACING,
MS_SPACING,
MS_ZOOM_LEVEL,
SEC_PER_YEAR,
SECOND_SPACING,
WEEK_SPACING,
YEAR_MONTH_ZOOM_LEVEL,
YEAR_SPACING,
ZoomLevel,
applyOffsetFromUtc,
calculateUtcOffset,
getPreferredOffsetFromUtc,
)
from pyqtgraph.Qt.QtCore import QDate, QDateTime, QTime, QTimeZone
from pyqtgraph.Qt.QtGui import QFont, QFontMetrics
app = pg.mkQApp()
def makeDateAxis():
axis = pg.DateAxisItem()
axis.fontMetrics = QFontMetrics(QFont())
axis.zoomLevel = YEAR_MONTH_ZOOM_LEVEL
return axis
@lru_cache
def densityForZoomLevel(level):
axis = makeDateAxis()
density = 3600
while axis.zoomLevel != level and density > 1:
axis.setZoomLevelForDensity(density)
density -= 1
return density
def getViewLengthInPxForZoomLevel(level, valuesRange):
return valuesRange / densityForZoomLevel(level)
def assert_subarray(subarray, array):
start = array.index(subarray[0])
assert array[start:start+len(subarray)] == subarray
@contextmanager
def inTimezone(timezone):
def fromSecsSinceEpochLocal(timestamp):
return QDateTime.fromMSecsSinceEpoch(timestamp * 1000).toTimeZone(timezone)
with mock.patch.object(QDateTime, "fromSecsSinceEpoch", fromSecsSinceEpochLocal):
yield
@pytest.fixture(autouse=True)
def reset_zoom_levels_utc_offsets():
for level in (
DAY_HOUR_ZOOM_LEVEL,
HOUR_MINUTE_ZOOM_LEVEL,
HMS_ZOOM_LEVEL,
MS_ZOOM_LEVEL,
):
level.utcOffset = None
@pytest.fixture
def dateAxis():
return makeDateAxis()
@pytest.fixture(autouse=True)
def use_c_locale():
locale.setlocale(locale.LC_TIME, "C")
def test_preferred_utc_offset_respects_chosen_offset():
assert getPreferredOffsetFromUtc(0, 7200) == 7200
assert getPreferredOffsetFromUtc(0, -7200) == -7200
@pytest.mark.qt_no_exception_capture
def test_preferred_utc_offset_doesnt_break_with_big_timestamps():
timestamp = SEC_PER_YEAR ** 13
assert -16 * 3600 <= getPreferredOffsetFromUtc(timestamp) <= 16 * 3600
assert getPreferredOffsetFromUtc(timestamp, 3600) == 3600
assert -16 * 3600 <= getPreferredOffsetFromUtc(-timestamp) <= 16 * 3600
assert getPreferredOffsetFromUtc(-timestamp, -1800) == -1800
def test_utc_offset_works_with_float_timestamp():
assert -16 * 3600 <= calculateUtcOffset(123456.0734) <= 16 * 3600
def test_applyOffsetFromUtc_does_what_it_promises_to_do():
timeZone = QTimeZone(b"UTC+4")
startDate = QDateTime(QDate(1970, 1, 2), QTime(2, 0), timeZone)
goalDate = QDateTime(QDate(1970, 1, 1), QTime(22, 0), timeZone)
assert (
startDate.toUTC().time() == goalDate.time()
and startDate.toUTC().date() == goalDate.date()
)
with inTimezone(timeZone):
shifted = applyOffsetFromUtc(startDate.toSecsSinceEpoch())
assert shifted == goalDate.toSecsSinceEpoch()
@pytest.mark.parametrize(
("timeZone", "transitionDate", "expectedDayTickStrings", "expectedHourTickStrings"),
(
(
QTimeZone(b"Europe/Berlin"),
QDate(2022, 10, 30),
["Sun 30"],
["01:00", "02:00", "02:00", "03:00", "04:00"],
),
(
QTimeZone(b"Europe/Berlin"),
QDate(2023, 3, 26),
["Sun 26"],
["01:00", "03:00", "04:00", "05:00", "06:00"],
),
(
QTimeZone(b"Pacific/Chatham"),
QDate(2024, 4, 7),
["Sun 07"],
["01:00", "02:00", "03:00", "03:00", "04:00"],
),
(
QTimeZone(b"Pacific/Chatham"),
QDate(2022, 9, 25),
["Sun 25"],
["01:00", "02:00", "04:00", "05:00", "06:00"],
),
(
QTimeZone(b"America/St_Johns"),
QDate(2012, 11, 4),
["Sun 04"],
["01:00", "01:00", "02:00", "03:00", "04:00"],
),
(
QTimeZone(b"America/St_Johns"),
QDate(1995, 4, 2),
["Sun 02"],
["02:00", "03:00", "04:00", "05:00", "06:00"],
),
(
QTimeZone(b"Australia/Lord_Howe"),
QDate(2007, 3, 25),
["Sun 25"],
["01:00", "02:00", "03:00", "04:00", "05:00"],
),
(
QTimeZone(b"Australia/Lord_Howe"),
QDate(2010, 10, 3),
["Sun 03"],
["01:00", "03:00", "04:00", "05:00", "06:00"],
),
),
ids=(
f"{zone}-{direction}"
for zone, direction
in product(
("Berlin", "Chatham", "St_Johns", "Lord_Howe"),
("backward", "forward"),
)
),
)
def test_maps_tick_values_to_local_times(
timeZone,
transitionDate,
expectedDayTickStrings,
expectedHourTickStrings,
dateAxis,
):
minTime = QDateTime(transitionDate, QTime(0, 0, 0, 0), timeZone).toSecsSinceEpoch()
maxTime = QDateTime(transitionDate, QTime(4, 0, 0, 0), timeZone).toSecsSinceEpoch()
xvals = list(range(minTime, maxTime + 3600, 3600))
timeRange = maxTime - minTime
lengthInPixels = getViewLengthInPxForZoomLevel(HOUR_MINUTE_ZOOM_LEVEL, timeRange)
with inTimezone(timeZone):
tickValues = dateAxis.tickValues(xvals[0] - 1, xvals[-1] + 1, lengthInPixels)
for spacing, ticks in tickValues:
if spacing == DAY_SPACING:
tickStrings = dateAxis.tickStrings(ticks, 1, DAY_SPACING)
assert_subarray(expectedDayTickStrings, tickStrings)
elif spacing == HOUR_SPACING:
tickStrings = dateAxis.tickStrings(ticks, 1, spacing)
assert_subarray(expectedHourTickStrings, tickStrings)
@pytest.mark.parametrize(
("timeZone"),
(
QTimeZone(b"Europe/Berlin"),
QTimeZone(b"Pacific/Chatham"),
QTimeZone(b"America/St_Johns"),
QTimeZone(b"Australia/Lord_Howe"),
),
ids=("Berlin", "Chatham", "St_Johns", "Lord_Howe"),
)
def test_maps_hour_ticks_to_local_times_when_skip_greater_than_one(timeZone, dateAxis):
date = QDate(2023, 5, 10)
minTime = QDateTime(date, QTime(0, 0, 0, 0), timeZone).toSecsSinceEpoch()
maxTime = QDateTime(date, QTime(18, 0, 0, 0), timeZone).toSecsSinceEpoch()
xvals = list(range(minTime, maxTime + 3600, 3600))
timeRange = maxTime - minTime
lengthInPixels = getViewLengthInPxForZoomLevel(DAY_HOUR_ZOOM_LEVEL, timeRange)
with inTimezone(timeZone):
tickValues = dateAxis.tickValues(xvals[0] - 1, xvals[-1] + 1, lengthInPixels)
for spacing, ticks in tickValues:
if spacing == HOUR_SPACING:
tickStrings = dateAxis.tickStrings(ticks, 1, spacing)
assert_subarray(["06:00", "12:00", "18:00"], tickStrings)
@pytest.mark.parametrize(
("zoomLevel", "expectedHourTickStrings"),
(
(HOUR_MINUTE_ZOOM_LEVEL, ["01:00", "02:00", "03:00", "04:00", "05:00"]),
(DAY_HOUR_ZOOM_LEVEL, ["06:00", "12:00", "18:00"]),
),
)
def test_custom_utc_offset_works(zoomLevel, expectedHourTickStrings, dateAxis):
maxHour = 4 if zoomLevel == HOUR_MINUTE_ZOOM_LEVEL else 18
utcZone = QTimeZone(b"UTC")
date = QDate(2001, 1, 1)
minTime = QDateTime(date, QTime(0, 0, 0, 0), utcZone).toSecsSinceEpoch()
maxTime = QDateTime(date, QTime(maxHour, 0, 0, 0), utcZone).toSecsSinceEpoch()
size_px = getViewLengthInPxForZoomLevel(zoomLevel, maxTime - minTime)
xvals = list(range(minTime, maxTime + 3600, 3600))
dateAxis.utcOffset = -3600
for spacing, ticks in dateAxis.tickValues(xvals[0] - 1, xvals[-1] + 1, size_px):
if spacing == DAY_SPACING:
tickStrings = dateAxis.tickStrings(ticks, 1, DAY_SPACING)
assert_subarray(["Mon 01"], tickStrings)
elif spacing == HOUR_SPACING:
tickStrings = dateAxis.tickStrings(ticks, 1, spacing)
assert_subarray(expectedHourTickStrings, tickStrings)
@pytest.mark.parametrize(
("localZone", "spacing", "expectedExtentionInHours"),
(
(QTimeZone(b"UTC+7"), MS_SPACING, 0),
(QTimeZone(b"UTC+5"), SECOND_SPACING, 0),
(QTimeZone(b"UTC+3"), MINUTE_SPACING, 0),
(QTimeZone(b"UTC+7"), HOUR_SPACING, 7),
(QTimeZone(b"UTC+6"), DAY_SPACING, 6),
(QTimeZone(b"UTC+5"), WEEK_SPACING, 5),
(QTimeZone(b"UTC+4"), MONTH_SPACING, 4),
(QTimeZone(b"UTC+3"), YEAR_SPACING, 3),
(QTimeZone(b"UTC"), MS_SPACING, 0),
(QTimeZone(b"UTC"), SECOND_SPACING, 0),
(QTimeZone(b"UTC"), MINUTE_SPACING, 0),
(QTimeZone(b"UTC"), HOUR_SPACING, 0),
(QTimeZone(b"UTC"), DAY_SPACING, 0),
(QTimeZone(b"UTC"), WEEK_SPACING, 0),
(QTimeZone(b"UTC"), MONTH_SPACING, 0),
(QTimeZone(b"UTC"), YEAR_SPACING, 0),
),
)
def test_extendTimeRangeForSpacing_repsects_utc_offset(
localZone, spacing, expectedExtentionInHours,
):
utcZone = QTimeZone(b"UTC")
date = QDate(2001, 1, 1)
minTime = QDateTime(date, QTime(0, 0, 0, 0), utcZone).toSecsSinceEpoch()
maxTime = QDateTime(date, QTime(18, 0, 0, 0), utcZone).toSecsSinceEpoch()
zoom = ZoomLevel([], "")
with inTimezone(localZone):
extMin, extMax = zoom.extendTimeRangeForSpacing(spacing, minTime, maxTime)
assert extMax - maxTime == expectedExtentionInHours * 3600
assert minTime - extMin == expectedExtentionInHours * 3600
|