fix: present web timestamps in UTC+3
This commit is contained in:
1 parent
00bbe66c77
commit
cffaaac755
8 files changed
+25
-18
No files matched your search
@@ -7,7 +7,7 @@ from xml.sax.saxutils import escape
|
||||
from zipfile import ZIP_DEFLATED, ZipFile
|
||||
|
||||
|
||||
def _cell(column: int, row: int, value: object, *, header: bool = False) -> str:
|
||||
def _cell(column: int, row: int, value: object, *, header: bool = False, display_timezone: timezone = timezone.utc) -> str:
|
||||
name = ""
|
||||
number = column
|
||||
while number:
|
||||
@@ -16,22 +16,22 @@ def _cell(column: int, row: int, value: object, *, header: bool = False) -> str:
|
||||
reference = f"{name}{row}"
|
||||
style = ' s="1"' if header else ''
|
||||
if isinstance(value, datetime):
|
||||
timestamp = value.replace(tzinfo=value.tzinfo or timezone.utc).astimezone(timezone.utc)
|
||||
serial = (timestamp - datetime(1899, 12, 30, tzinfo=timezone.utc)).total_seconds() / 86400
|
||||
timestamp = value.replace(tzinfo=value.tzinfo or timezone.utc).astimezone(display_timezone)
|
||||
serial = (timestamp.replace(tzinfo=None) - datetime(1899, 12, 30)).total_seconds() / 86400
|
||||
return f'<c r="{reference}" s="2"><v>{serial:.10f}</v></c>'
|
||||
if isinstance(value, (int, float)) and not isinstance(value, bool):
|
||||
return f'<c r="{reference}"{style}><v>{value}</v></c>'
|
||||
return f'<c r="{reference}" t="inlineStr"{style}><is><t>{escape(str(value or ""))}</t></is></c>'
|
||||
|
||||
|
||||
def build_xlsx(*, sheet_name: str, headers: list[str], rows: list[list[object]]) -> bytes:
|
||||
"""Builds one readable XLSX worksheet with a frozen, styled header row."""
|
||||
def build_xlsx(*, sheet_name: str, headers: list[str], rows: list[list[object]], display_timezone: timezone = timezone.utc) -> bytes:
|
||||
"""Build a workbook with dates converted to its user-facing time zone."""
|
||||
sheet_rows = [
|
||||
'<row r="1" ht="24">' + ''.join(_cell(index, 1, value, header=True) for index, value in enumerate(headers, 1)) + '</row>'
|
||||
'<row r="1" ht="24">' + ''.join(_cell(index, 1, value, header=True, display_timezone=display_timezone) for index, value in enumerate(headers, 1)) + '</row>'
|
||||
]
|
||||
for row_number, row in enumerate(rows, 2):
|
||||
sheet_rows.append('<row r="%d">%s</row>' % (row_number, ''.join(
|
||||
_cell(index, value_row, value) for index, value in enumerate(row, 1) for value_row in (row_number,)
|
||||
_cell(index, value_row, value, display_timezone=display_timezone) for index, value in enumerate(row, 1) for value_row in (row_number,)
|
||||
)))
|
||||
widths = ''.join(f'<col min="{index}" max="{index}" width="{min(max(max([len(header)] + [len(str(row[index - 1] or "")) for row in rows]) + 2, 14), 48)}" customWidth="1"/>' for index, header in enumerate(headers, 1))
|
||||
sheet = ('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
|
||||
|
||||
Reference in new issue
Block a user