[8.0.x] fix: avoid rounding microsecond to `1_000_000` (#11863)
Co-authored-by: Dương Quốc Khánh <dqkqdlot@gmail.com>
This commit is contained in:
		
							parent
							
								
									3b41c65c81
								
							
						
					
					
						commit
						608590097a
					
				| 
						 | 
				
			
			@ -0,0 +1 @@
 | 
			
		|||
Avoid microsecond exceeds ``1_000_000`` when using ``log-date-format`` with ``%f`` specifier, which might cause the test suite to crash.
 | 
			
		||||
| 
						 | 
				
			
			@ -71,7 +71,8 @@ class DatetimeFormatter(logging.Formatter):
 | 
			
		|||
            tz = timezone(timedelta(seconds=ct.tm_gmtoff), ct.tm_zone)
 | 
			
		||||
            # Construct `datetime.datetime` object from `struct_time`
 | 
			
		||||
            # and msecs information from `record`
 | 
			
		||||
            dt = datetime(*ct[0:6], microsecond=round(record.msecs * 1000), tzinfo=tz)
 | 
			
		||||
            # Using int() instead of round() to avoid it exceeding 1_000_000 and causing a ValueError (#11861).
 | 
			
		||||
            dt = datetime(*ct[0:6], microsecond=int(record.msecs * 1000), tzinfo=tz)
 | 
			
		||||
            return dt.strftime(datefmt)
 | 
			
		||||
        # Use `logging.Formatter` for non-microsecond formats
 | 
			
		||||
        return super().formatTime(record, datefmt)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue