Skip to content

Commit 1b8680e

Browse files
m-endraMarcin Endraszkahynek
authored
TimeStamper now returns UTC timezone for custom format string. (#713)
* TimeStamper now returns UTC timezone for custom format string. * Add pull request link for #713 --------- Co-authored-by: Marcin Endraszka <marcin.endraszka.psnke@dralias.com> Co-authored-by: Hynek Schlawack <hs@ox.cx>
1 parent 414047d commit 1b8680e

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ You can find our backwards-compatibility policy [here](https://github.com/hynek/
1515

1616
## [Unreleased](https://github.com/hynek/structlog/compare/25.2.0...HEAD)
1717

18+
### Fixed
19+
- `structlog.processors.TimeStamper` now returns UTC timezone for custom format string.
20+
[#713](https://github.com/hynek/structlog/pull/713)
1821

1922
## [25.2.0](https://github.com/hynek/structlog/compare/25.1.0...25.2.0) - 2025-03-11
2023

src/structlog/processors.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,12 +553,18 @@ def stamper_iso_utc(event_dict: EventDict) -> EventDict:
553553

554554
return stamper_iso_local
555555

556-
def stamper_fmt(event_dict: EventDict) -> EventDict:
556+
def stamper_fmt_local(event_dict: EventDict) -> EventDict:
557557
event_dict[key] = now().astimezone().strftime(fmt)
558+
return event_dict
558559

560+
def stamper_fmt_utc(event_dict: EventDict) -> EventDict:
561+
event_dict[key] = now().strftime(fmt)
559562
return event_dict
560563

561-
return stamper_fmt
564+
if utc:
565+
return stamper_fmt_utc
566+
567+
return stamper_fmt_local
562568

563569

564570
class MaybeTimeStamper:

tests/processors/test_renderers.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,36 @@ def test_formats(self):
414414

415415
assert "1980" == d["timestamp"]
416416

417+
@freeze_time("1980-03-25 16:00:00")
418+
def test_inserts_formatted_utc(self):
419+
"""
420+
The fmt string in UTC timezone works.
421+
422+
The exact hours calculated here maybe incorrect because of freezegun bugs:
423+
https://github.com/spulec/freezegun/issues/348
424+
https://github.com/spulec/freezegun/issues/494
425+
"""
426+
427+
ts = TimeStamper(fmt="%Y-%m-%d %H:%M:%S %Z")
428+
d = ts(None, None, {})
429+
430+
assert "1980-03-25 16:00:00 UTC" == d["timestamp"]
431+
432+
@freeze_time("1980-03-25 16:00:00")
433+
def test_inserts_formatted_local(self):
434+
"""
435+
The fmt string in local timezone works.
436+
437+
The exact hours calculated here maybe incorrect because of freezegun bugs:
438+
https://github.com/spulec/freezegun/issues/348
439+
https://github.com/spulec/freezegun/issues/494
440+
"""
441+
local_tz = datetime.datetime.now().astimezone().tzname()
442+
ts = TimeStamper(fmt="%Y-%m-%d %H:%M:%S %Z", utc=False)
443+
d = ts(None, None, {})
444+
445+
assert f"1980-03-25 16:00:00 {local_tz}" == d["timestamp"]
446+
417447
@freeze_time("1980-03-25 16:00:00")
418448
def test_tz_aware(self):
419449
"""

0 commit comments

Comments
 (0)