Timedelta.max

Overview:

  • The maximum time duration that can be represented using the timedelta class is provided by the attribute timedelta.max.
  • Trying to create a timedelta of greater than the timedelta.max will result in a overflow.

Example:

# Example Python program that prints
# the maximum representable time duration 
# using the Python class datetime.timedelta

from datetime import timedelta

# Obtain maximum time duration possible through timedelta
max = timedelta.max
print("Maximum timedelta:")
print(max)
print(type(max))

# Try creating a timedelta bigger than timedelta.max
print(timedelta(timedelta.max.days + 1))

Output:

Maximum timedelta:

999999999 days, 23:59:59.999999

<class 'datetime.timedelta'>

Traceback (most recent call last):

  File "/Valli/PythonProgs/td_ex2.py", line 14, in <module>

    print(timedelta(timedelta.max.days + 1))

OverflowError: days=1000000000; must have magnitude <= 999999999

 


Copyright 2024 © pythontic.com