
TIME DELTA OBJECTS
A timedelta object represents a duration, the
difference between two dates or times.
class
datetime.timedelta(days=0,
seconds=0, microseconds=0,
milliseconds=0, minutes=0, hours=0,
weeks=0)
All arguments are optional and default to 0.
Arguments may be integers or floats, and may be positive or
negative.
Only days, seconds and microseconds are stored internally. Arguments are converted to those units:
A millisecond is converted to 1000 microseconds.
A minute is converted to 60 seconds.
An hour is converted to 3600 seconds.
A week is converted to 7 days.
and days, seconds and microseconds are then normalized so that the representation is unique, with
0 <= microseconds < 1000000
0 <= seconds < 3600*24 (the number of seconds
in one day)
-999999999 <= days <= 999999999
The following example illustrates how any arguments besides days, seconds and microseconds are “merged” and normalized into those three resulting attributes:
>>>
>>> from datetime import timedelta >>> delta = timedelta( ... days=50, ... seconds=27, ... microseconds=10, ... milliseconds=29000, ... minutes=5, ... hours=8, ... weeks=2 ... ) >>> # Only days, seconds, and microseconds remain >>> delta datetime.timedelta(days=64, seconds=29156, microseconds=10)
If any argument is a float and there are fractional microseconds, the fractional microseconds left over from all arguments are combined and their sum is rounded to the nearest microsecond using round-half-to-even tiebreaker. If no argument is a float, the conversion and normalization processes are exact (no information is lost).
If the normalized value of days lies outside the indicated
range, OverflowError is raised.
Note that normalization of negative values may be surprising at first. For example:
>>>
>>> from datetime import timedelta >>> d = timedelta(microseconds=-1) >>> (d.days, d.seconds, d.microseconds) (-1, 86399, 999999)
Class attributes:
timedelta.min
The most negative timedelta object,
timedelta(-999999999).
timedelta.max
The most positive timedelta object,
timedelta(days=999999999, hours=23, minutes=59, seconds=59,
microseconds=999999).
timedelta.resolution
The smallest possible difference between non-equal
timedelta objects,
timedelta(microseconds=1).
Examples:
>>> from datetime import timedelta >>> year = timedelta(days=365) >>> ten_years = 10 * year >>> ten_years datetime.timedelta(days=3650) >>> ten_years.days // 365 10 >>> nine_years = ten_years - year >>> nine_years datetime.timedelta(days=3285) >>> three_years = nine_years // 3 >>> three_years, three_years.days // 365 (datetime.timedelta(days=1095), 3)
python3 unitest task context: complete solution please ty TESTING Read Mike's “Python 3 Testing: An Intro...