File tree Expand file tree Collapse file tree 3 files changed +41
-2
lines changed
Expand file tree Collapse file tree 3 files changed +41
-2
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
564570class MaybeTimeStamper :
Original file line number Diff line number Diff 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 """
You can’t perform that action at this time.
0 commit comments