Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../usr/lib64/python3....
File: datetime.py
"""Concrete date/time and related types.
[0] Fix | Delete
[1] Fix | Delete
See http://www.iana.org/time-zones/repository/tz-link.html for
[2] Fix | Delete
time zone and DST data sources.
[3] Fix | Delete
"""
[4] Fix | Delete
[5] Fix | Delete
import time as _time
[6] Fix | Delete
import math as _math
[7] Fix | Delete
import sys
[8] Fix | Delete
[9] Fix | Delete
def _cmp(x, y):
[10] Fix | Delete
return 0 if x == y else 1 if x > y else -1
[11] Fix | Delete
[12] Fix | Delete
MINYEAR = 1
[13] Fix | Delete
MAXYEAR = 9999
[14] Fix | Delete
_MAXORDINAL = 3652059 # date.max.toordinal()
[15] Fix | Delete
[16] Fix | Delete
# Utility functions, adapted from Python's Demo/classes/Dates.py, which
[17] Fix | Delete
# also assumes the current Gregorian calendar indefinitely extended in
[18] Fix | Delete
# both directions. Difference: Dates.py calls January 1 of year 0 day
[19] Fix | Delete
# number 1. The code here calls January 1 of year 1 day number 1. This is
[20] Fix | Delete
# to match the definition of the "proleptic Gregorian" calendar in Dershowitz
[21] Fix | Delete
# and Reingold's "Calendrical Calculations", where it's the base calendar
[22] Fix | Delete
# for all computations. See the book for algorithms for converting between
[23] Fix | Delete
# proleptic Gregorian ordinals and many other calendar systems.
[24] Fix | Delete
[25] Fix | Delete
# -1 is a placeholder for indexing purposes.
[26] Fix | Delete
_DAYS_IN_MONTH = [-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
[27] Fix | Delete
[28] Fix | Delete
_DAYS_BEFORE_MONTH = [-1] # -1 is a placeholder for indexing purposes.
[29] Fix | Delete
dbm = 0
[30] Fix | Delete
for dim in _DAYS_IN_MONTH[1:]:
[31] Fix | Delete
_DAYS_BEFORE_MONTH.append(dbm)
[32] Fix | Delete
dbm += dim
[33] Fix | Delete
del dbm, dim
[34] Fix | Delete
[35] Fix | Delete
def _is_leap(year):
[36] Fix | Delete
"year -> 1 if leap year, else 0."
[37] Fix | Delete
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
[38] Fix | Delete
[39] Fix | Delete
def _days_before_year(year):
[40] Fix | Delete
"year -> number of days before January 1st of year."
[41] Fix | Delete
y = year - 1
[42] Fix | Delete
return y*365 + y//4 - y//100 + y//400
[43] Fix | Delete
[44] Fix | Delete
def _days_in_month(year, month):
[45] Fix | Delete
"year, month -> number of days in that month in that year."
[46] Fix | Delete
assert 1 <= month <= 12, month
[47] Fix | Delete
if month == 2 and _is_leap(year):
[48] Fix | Delete
return 29
[49] Fix | Delete
return _DAYS_IN_MONTH[month]
[50] Fix | Delete
[51] Fix | Delete
def _days_before_month(year, month):
[52] Fix | Delete
"year, month -> number of days in year preceding first day of month."
[53] Fix | Delete
assert 1 <= month <= 12, 'month must be in 1..12'
[54] Fix | Delete
return _DAYS_BEFORE_MONTH[month] + (month > 2 and _is_leap(year))
[55] Fix | Delete
[56] Fix | Delete
def _ymd2ord(year, month, day):
[57] Fix | Delete
"year, month, day -> ordinal, considering 01-Jan-0001 as day 1."
[58] Fix | Delete
assert 1 <= month <= 12, 'month must be in 1..12'
[59] Fix | Delete
dim = _days_in_month(year, month)
[60] Fix | Delete
assert 1 <= day <= dim, ('day must be in 1..%d' % dim)
[61] Fix | Delete
return (_days_before_year(year) +
[62] Fix | Delete
_days_before_month(year, month) +
[63] Fix | Delete
day)
[64] Fix | Delete
[65] Fix | Delete
_DI400Y = _days_before_year(401) # number of days in 400 years
[66] Fix | Delete
_DI100Y = _days_before_year(101) # " " " " 100 "
[67] Fix | Delete
_DI4Y = _days_before_year(5) # " " " " 4 "
[68] Fix | Delete
[69] Fix | Delete
# A 4-year cycle has an extra leap day over what we'd get from pasting
[70] Fix | Delete
# together 4 single years.
[71] Fix | Delete
assert _DI4Y == 4 * 365 + 1
[72] Fix | Delete
[73] Fix | Delete
# Similarly, a 400-year cycle has an extra leap day over what we'd get from
[74] Fix | Delete
# pasting together 4 100-year cycles.
[75] Fix | Delete
assert _DI400Y == 4 * _DI100Y + 1
[76] Fix | Delete
[77] Fix | Delete
# OTOH, a 100-year cycle has one fewer leap day than we'd get from
[78] Fix | Delete
# pasting together 25 4-year cycles.
[79] Fix | Delete
assert _DI100Y == 25 * _DI4Y - 1
[80] Fix | Delete
[81] Fix | Delete
def _ord2ymd(n):
[82] Fix | Delete
"ordinal -> (year, month, day), considering 01-Jan-0001 as day 1."
[83] Fix | Delete
[84] Fix | Delete
# n is a 1-based index, starting at 1-Jan-1. The pattern of leap years
[85] Fix | Delete
# repeats exactly every 400 years. The basic strategy is to find the
[86] Fix | Delete
# closest 400-year boundary at or before n, then work with the offset
[87] Fix | Delete
# from that boundary to n. Life is much clearer if we subtract 1 from
[88] Fix | Delete
# n first -- then the values of n at 400-year boundaries are exactly
[89] Fix | Delete
# those divisible by _DI400Y:
[90] Fix | Delete
#
[91] Fix | Delete
# D M Y n n-1
[92] Fix | Delete
# -- --- ---- ---------- ----------------
[93] Fix | Delete
# 31 Dec -400 -_DI400Y -_DI400Y -1
[94] Fix | Delete
# 1 Jan -399 -_DI400Y +1 -_DI400Y 400-year boundary
[95] Fix | Delete
# ...
[96] Fix | Delete
# 30 Dec 000 -1 -2
[97] Fix | Delete
# 31 Dec 000 0 -1
[98] Fix | Delete
# 1 Jan 001 1 0 400-year boundary
[99] Fix | Delete
# 2 Jan 001 2 1
[100] Fix | Delete
# 3 Jan 001 3 2
[101] Fix | Delete
# ...
[102] Fix | Delete
# 31 Dec 400 _DI400Y _DI400Y -1
[103] Fix | Delete
# 1 Jan 401 _DI400Y +1 _DI400Y 400-year boundary
[104] Fix | Delete
n -= 1
[105] Fix | Delete
n400, n = divmod(n, _DI400Y)
[106] Fix | Delete
year = n400 * 400 + 1 # ..., -399, 1, 401, ...
[107] Fix | Delete
[108] Fix | Delete
# Now n is the (non-negative) offset, in days, from January 1 of year, to
[109] Fix | Delete
# the desired date. Now compute how many 100-year cycles precede n.
[110] Fix | Delete
# Note that it's possible for n100 to equal 4! In that case 4 full
[111] Fix | Delete
# 100-year cycles precede the desired day, which implies the desired
[112] Fix | Delete
# day is December 31 at the end of a 400-year cycle.
[113] Fix | Delete
n100, n = divmod(n, _DI100Y)
[114] Fix | Delete
[115] Fix | Delete
# Now compute how many 4-year cycles precede it.
[116] Fix | Delete
n4, n = divmod(n, _DI4Y)
[117] Fix | Delete
[118] Fix | Delete
# And now how many single years. Again n1 can be 4, and again meaning
[119] Fix | Delete
# that the desired day is December 31 at the end of the 4-year cycle.
[120] Fix | Delete
n1, n = divmod(n, 365)
[121] Fix | Delete
[122] Fix | Delete
year += n100 * 100 + n4 * 4 + n1
[123] Fix | Delete
if n1 == 4 or n100 == 4:
[124] Fix | Delete
assert n == 0
[125] Fix | Delete
return year-1, 12, 31
[126] Fix | Delete
[127] Fix | Delete
# Now the year is correct, and n is the offset from January 1. We find
[128] Fix | Delete
# the month via an estimate that's either exact or one too large.
[129] Fix | Delete
leapyear = n1 == 3 and (n4 != 24 or n100 == 3)
[130] Fix | Delete
assert leapyear == _is_leap(year)
[131] Fix | Delete
month = (n + 50) >> 5
[132] Fix | Delete
preceding = _DAYS_BEFORE_MONTH[month] + (month > 2 and leapyear)
[133] Fix | Delete
if preceding > n: # estimate is too large
[134] Fix | Delete
month -= 1
[135] Fix | Delete
preceding -= _DAYS_IN_MONTH[month] + (month == 2 and leapyear)
[136] Fix | Delete
n -= preceding
[137] Fix | Delete
assert 0 <= n < _days_in_month(year, month)
[138] Fix | Delete
[139] Fix | Delete
# Now the year and month are correct, and n is the offset from the
[140] Fix | Delete
# start of that month: we're done!
[141] Fix | Delete
return year, month, n+1
[142] Fix | Delete
[143] Fix | Delete
# Month and day names. For localized versions, see the calendar module.
[144] Fix | Delete
_MONTHNAMES = [None, "Jan", "Feb", "Mar", "Apr", "May", "Jun",
[145] Fix | Delete
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
[146] Fix | Delete
_DAYNAMES = [None, "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
[147] Fix | Delete
[148] Fix | Delete
[149] Fix | Delete
def _build_struct_time(y, m, d, hh, mm, ss, dstflag):
[150] Fix | Delete
wday = (_ymd2ord(y, m, d) + 6) % 7
[151] Fix | Delete
dnum = _days_before_month(y, m) + d
[152] Fix | Delete
return _time.struct_time((y, m, d, hh, mm, ss, wday, dnum, dstflag))
[153] Fix | Delete
[154] Fix | Delete
def _format_time(hh, mm, ss, us, timespec='auto'):
[155] Fix | Delete
specs = {
[156] Fix | Delete
'hours': '{:02d}',
[157] Fix | Delete
'minutes': '{:02d}:{:02d}',
[158] Fix | Delete
'seconds': '{:02d}:{:02d}:{:02d}',
[159] Fix | Delete
'milliseconds': '{:02d}:{:02d}:{:02d}.{:03d}',
[160] Fix | Delete
'microseconds': '{:02d}:{:02d}:{:02d}.{:06d}'
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
if timespec == 'auto':
[164] Fix | Delete
# Skip trailing microseconds when us==0.
[165] Fix | Delete
timespec = 'microseconds' if us else 'seconds'
[166] Fix | Delete
elif timespec == 'milliseconds':
[167] Fix | Delete
us //= 1000
[168] Fix | Delete
try:
[169] Fix | Delete
fmt = specs[timespec]
[170] Fix | Delete
except KeyError:
[171] Fix | Delete
raise ValueError('Unknown timespec value')
[172] Fix | Delete
else:
[173] Fix | Delete
return fmt.format(hh, mm, ss, us)
[174] Fix | Delete
[175] Fix | Delete
# Correctly substitute for %z and %Z escapes in strftime formats.
[176] Fix | Delete
def _wrap_strftime(object, format, timetuple):
[177] Fix | Delete
# Don't call utcoffset() or tzname() unless actually needed.
[178] Fix | Delete
freplace = None # the string to use for %f
[179] Fix | Delete
zreplace = None # the string to use for %z
[180] Fix | Delete
Zreplace = None # the string to use for %Z
[181] Fix | Delete
[182] Fix | Delete
# Scan format for %z and %Z escapes, replacing as needed.
[183] Fix | Delete
newformat = []
[184] Fix | Delete
push = newformat.append
[185] Fix | Delete
i, n = 0, len(format)
[186] Fix | Delete
while i < n:
[187] Fix | Delete
ch = format[i]
[188] Fix | Delete
i += 1
[189] Fix | Delete
if ch == '%':
[190] Fix | Delete
if i < n:
[191] Fix | Delete
ch = format[i]
[192] Fix | Delete
i += 1
[193] Fix | Delete
if ch == 'f':
[194] Fix | Delete
if freplace is None:
[195] Fix | Delete
freplace = '%06d' % getattr(object,
[196] Fix | Delete
'microsecond', 0)
[197] Fix | Delete
newformat.append(freplace)
[198] Fix | Delete
elif ch == 'z':
[199] Fix | Delete
if zreplace is None:
[200] Fix | Delete
zreplace = ""
[201] Fix | Delete
if hasattr(object, "utcoffset"):
[202] Fix | Delete
offset = object.utcoffset()
[203] Fix | Delete
if offset is not None:
[204] Fix | Delete
sign = '+'
[205] Fix | Delete
if offset.days < 0:
[206] Fix | Delete
offset = -offset
[207] Fix | Delete
sign = '-'
[208] Fix | Delete
h, m = divmod(offset, timedelta(hours=1))
[209] Fix | Delete
assert not m % timedelta(minutes=1), "whole minute"
[210] Fix | Delete
m //= timedelta(minutes=1)
[211] Fix | Delete
zreplace = '%c%02d%02d' % (sign, h, m)
[212] Fix | Delete
assert '%' not in zreplace
[213] Fix | Delete
newformat.append(zreplace)
[214] Fix | Delete
elif ch == 'Z':
[215] Fix | Delete
if Zreplace is None:
[216] Fix | Delete
Zreplace = ""
[217] Fix | Delete
if hasattr(object, "tzname"):
[218] Fix | Delete
s = object.tzname()
[219] Fix | Delete
if s is not None:
[220] Fix | Delete
# strftime is going to have at this: escape %
[221] Fix | Delete
Zreplace = s.replace('%', '%%')
[222] Fix | Delete
newformat.append(Zreplace)
[223] Fix | Delete
else:
[224] Fix | Delete
push('%')
[225] Fix | Delete
push(ch)
[226] Fix | Delete
else:
[227] Fix | Delete
push('%')
[228] Fix | Delete
else:
[229] Fix | Delete
push(ch)
[230] Fix | Delete
newformat = "".join(newformat)
[231] Fix | Delete
return _time.strftime(newformat, timetuple)
[232] Fix | Delete
[233] Fix | Delete
# Just raise TypeError if the arg isn't None or a string.
[234] Fix | Delete
def _check_tzname(name):
[235] Fix | Delete
if name is not None and not isinstance(name, str):
[236] Fix | Delete
raise TypeError("tzinfo.tzname() must return None or string, "
[237] Fix | Delete
"not '%s'" % type(name))
[238] Fix | Delete
[239] Fix | Delete
# name is the offset-producing method, "utcoffset" or "dst".
[240] Fix | Delete
# offset is what it returned.
[241] Fix | Delete
# If offset isn't None or timedelta, raises TypeError.
[242] Fix | Delete
# If offset is None, returns None.
[243] Fix | Delete
# Else offset is checked for being in range, and a whole # of minutes.
[244] Fix | Delete
# If it is, its integer value is returned. Else ValueError is raised.
[245] Fix | Delete
def _check_utc_offset(name, offset):
[246] Fix | Delete
assert name in ("utcoffset", "dst")
[247] Fix | Delete
if offset is None:
[248] Fix | Delete
return
[249] Fix | Delete
if not isinstance(offset, timedelta):
[250] Fix | Delete
raise TypeError("tzinfo.%s() must return None "
[251] Fix | Delete
"or timedelta, not '%s'" % (name, type(offset)))
[252] Fix | Delete
if offset.microseconds:
[253] Fix | Delete
raise ValueError("tzinfo.%s() must return a whole number "
[254] Fix | Delete
"of seconds, got %s" % (name, offset))
[255] Fix | Delete
if not -timedelta(1) < offset < timedelta(1):
[256] Fix | Delete
raise ValueError("%s()=%s, must be strictly between "
[257] Fix | Delete
"-timedelta(hours=24) and timedelta(hours=24)" %
[258] Fix | Delete
(name, offset))
[259] Fix | Delete
[260] Fix | Delete
def _check_int_field(value):
[261] Fix | Delete
if isinstance(value, int):
[262] Fix | Delete
return value
[263] Fix | Delete
if not isinstance(value, float):
[264] Fix | Delete
try:
[265] Fix | Delete
value = value.__int__()
[266] Fix | Delete
except AttributeError:
[267] Fix | Delete
pass
[268] Fix | Delete
else:
[269] Fix | Delete
if isinstance(value, int):
[270] Fix | Delete
return value
[271] Fix | Delete
raise TypeError('__int__ returned non-int (type %s)' %
[272] Fix | Delete
type(value).__name__)
[273] Fix | Delete
raise TypeError('an integer is required (got type %s)' %
[274] Fix | Delete
type(value).__name__)
[275] Fix | Delete
raise TypeError('integer argument expected, got float')
[276] Fix | Delete
[277] Fix | Delete
def _check_date_fields(year, month, day):
[278] Fix | Delete
year = _check_int_field(year)
[279] Fix | Delete
month = _check_int_field(month)
[280] Fix | Delete
day = _check_int_field(day)
[281] Fix | Delete
if not MINYEAR <= year <= MAXYEAR:
[282] Fix | Delete
raise ValueError('year must be in %d..%d' % (MINYEAR, MAXYEAR), year)
[283] Fix | Delete
if not 1 <= month <= 12:
[284] Fix | Delete
raise ValueError('month must be in 1..12', month)
[285] Fix | Delete
dim = _days_in_month(year, month)
[286] Fix | Delete
if not 1 <= day <= dim:
[287] Fix | Delete
raise ValueError('day must be in 1..%d' % dim, day)
[288] Fix | Delete
return year, month, day
[289] Fix | Delete
[290] Fix | Delete
def _check_time_fields(hour, minute, second, microsecond, fold):
[291] Fix | Delete
hour = _check_int_field(hour)
[292] Fix | Delete
minute = _check_int_field(minute)
[293] Fix | Delete
second = _check_int_field(second)
[294] Fix | Delete
microsecond = _check_int_field(microsecond)
[295] Fix | Delete
if not 0 <= hour <= 23:
[296] Fix | Delete
raise ValueError('hour must be in 0..23', hour)
[297] Fix | Delete
if not 0 <= minute <= 59:
[298] Fix | Delete
raise ValueError('minute must be in 0..59', minute)
[299] Fix | Delete
if not 0 <= second <= 59:
[300] Fix | Delete
raise ValueError('second must be in 0..59', second)
[301] Fix | Delete
if not 0 <= microsecond <= 999999:
[302] Fix | Delete
raise ValueError('microsecond must be in 0..999999', microsecond)
[303] Fix | Delete
if fold not in (0, 1):
[304] Fix | Delete
raise ValueError('fold must be either 0 or 1', fold)
[305] Fix | Delete
return hour, minute, second, microsecond, fold
[306] Fix | Delete
[307] Fix | Delete
def _check_tzinfo_arg(tz):
[308] Fix | Delete
if tz is not None and not isinstance(tz, tzinfo):
[309] Fix | Delete
raise TypeError("tzinfo argument must be None or of a tzinfo subclass")
[310] Fix | Delete
[311] Fix | Delete
def _cmperror(x, y):
[312] Fix | Delete
raise TypeError("can't compare '%s' to '%s'" % (
[313] Fix | Delete
type(x).__name__, type(y).__name__))
[314] Fix | Delete
[315] Fix | Delete
def _divide_and_round(a, b):
[316] Fix | Delete
"""divide a by b and round result to the nearest integer
[317] Fix | Delete
[318] Fix | Delete
When the ratio is exactly half-way between two integers,
[319] Fix | Delete
the even integer is returned.
[320] Fix | Delete
"""
[321] Fix | Delete
# Based on the reference implementation for divmod_near
[322] Fix | Delete
# in Objects/longobject.c.
[323] Fix | Delete
q, r = divmod(a, b)
[324] Fix | Delete
# round up if either r / b > 0.5, or r / b == 0.5 and q is odd.
[325] Fix | Delete
# The expression r / b > 0.5 is equivalent to 2 * r > b if b is
[326] Fix | Delete
# positive, 2 * r < b if b negative.
[327] Fix | Delete
r *= 2
[328] Fix | Delete
greater_than_half = r > b if b > 0 else r < b
[329] Fix | Delete
if greater_than_half or r == b and q % 2 == 1:
[330] Fix | Delete
q += 1
[331] Fix | Delete
[332] Fix | Delete
return q
[333] Fix | Delete
[334] Fix | Delete
[335] Fix | Delete
class timedelta:
[336] Fix | Delete
"""Represent the difference between two datetime objects.
[337] Fix | Delete
[338] Fix | Delete
Supported operators:
[339] Fix | Delete
[340] Fix | Delete
- add, subtract timedelta
[341] Fix | Delete
- unary plus, minus, abs
[342] Fix | Delete
- compare to timedelta
[343] Fix | Delete
- multiply, divide by int
[344] Fix | Delete
[345] Fix | Delete
In addition, datetime supports subtraction of two datetime objects
[346] Fix | Delete
returning a timedelta, and addition or subtraction of a datetime
[347] Fix | Delete
and a timedelta giving a datetime.
[348] Fix | Delete
[349] Fix | Delete
Representation: (days, seconds, microseconds). Why? Because I
[350] Fix | Delete
felt like it.
[351] Fix | Delete
"""
[352] Fix | Delete
__slots__ = '_days', '_seconds', '_microseconds', '_hashcode'
[353] Fix | Delete
[354] Fix | Delete
def __new__(cls, days=0, seconds=0, microseconds=0,
[355] Fix | Delete
milliseconds=0, minutes=0, hours=0, weeks=0):
[356] Fix | Delete
# Doing this efficiently and accurately in C is going to be difficult
[357] Fix | Delete
# and error-prone, due to ubiquitous overflow possibilities, and that
[358] Fix | Delete
# C double doesn't have enough bits of precision to represent
[359] Fix | Delete
# microseconds over 10K years faithfully. The code here tries to make
[360] Fix | Delete
# explicit where go-fast assumptions can be relied on, in order to
[361] Fix | Delete
# guide the C implementation; it's way more convoluted than speed-
[362] Fix | Delete
# ignoring auto-overflow-to-long idiomatic Python could be.
[363] Fix | Delete
[364] Fix | Delete
# XXX Check that all inputs are ints or floats.
[365] Fix | Delete
[366] Fix | Delete
# Final values, all integer.
[367] Fix | Delete
# s and us fit in 32-bit signed ints; d isn't bounded.
[368] Fix | Delete
d = s = us = 0
[369] Fix | Delete
[370] Fix | Delete
# Normalize everything to days, seconds, microseconds.
[371] Fix | Delete
days += weeks*7
[372] Fix | Delete
seconds += minutes*60 + hours*3600
[373] Fix | Delete
microseconds += milliseconds*1000
[374] Fix | Delete
[375] Fix | Delete
# Get rid of all fractions, and normalize s and us.
[376] Fix | Delete
# Take a deep breath <wink>.
[377] Fix | Delete
if isinstance(days, float):
[378] Fix | Delete
dayfrac, days = _math.modf(days)
[379] Fix | Delete
daysecondsfrac, daysecondswhole = _math.modf(dayfrac * (24.*3600.))
[380] Fix | Delete
assert daysecondswhole == int(daysecondswhole) # can't overflow
[381] Fix | Delete
s = int(daysecondswhole)
[382] Fix | Delete
assert days == int(days)
[383] Fix | Delete
d = int(days)
[384] Fix | Delete
else:
[385] Fix | Delete
daysecondsfrac = 0.0
[386] Fix | Delete
d = days
[387] Fix | Delete
assert isinstance(daysecondsfrac, float)
[388] Fix | Delete
assert abs(daysecondsfrac) <= 1.0
[389] Fix | Delete
assert isinstance(d, int)
[390] Fix | Delete
assert abs(s) <= 24 * 3600
[391] Fix | Delete
# days isn't referenced again before redefinition
[392] Fix | Delete
[393] Fix | Delete
if isinstance(seconds, float):
[394] Fix | Delete
secondsfrac, seconds = _math.modf(seconds)
[395] Fix | Delete
assert seconds == int(seconds)
[396] Fix | Delete
seconds = int(seconds)
[397] Fix | Delete
secondsfrac += daysecondsfrac
[398] Fix | Delete
assert abs(secondsfrac) <= 2.0
[399] Fix | Delete
else:
[400] Fix | Delete
secondsfrac = daysecondsfrac
[401] Fix | Delete
# daysecondsfrac isn't referenced again
[402] Fix | Delete
assert isinstance(secondsfrac, float)
[403] Fix | Delete
assert abs(secondsfrac) <= 2.0
[404] Fix | Delete
[405] Fix | Delete
assert isinstance(seconds, int)
[406] Fix | Delete
days, seconds = divmod(seconds, 24*3600)
[407] Fix | Delete
d += days
[408] Fix | Delete
s += int(seconds) # can't overflow
[409] Fix | Delete
assert isinstance(s, int)
[410] Fix | Delete
assert abs(s) <= 2 * 24 * 3600
[411] Fix | Delete
# seconds isn't referenced again before redefinition
[412] Fix | Delete
[413] Fix | Delete
usdouble = secondsfrac * 1e6
[414] Fix | Delete
assert abs(usdouble) < 2.1e6 # exact value not critical
[415] Fix | Delete
# secondsfrac isn't referenced again
[416] Fix | Delete
[417] Fix | Delete
if isinstance(microseconds, float):
[418] Fix | Delete
microseconds = round(microseconds + usdouble)
[419] Fix | Delete
seconds, microseconds = divmod(microseconds, 1000000)
[420] Fix | Delete
days, seconds = divmod(seconds, 24*3600)
[421] Fix | Delete
d += days
[422] Fix | Delete
s += seconds
[423] Fix | Delete
else:
[424] Fix | Delete
microseconds = int(microseconds)
[425] Fix | Delete
seconds, microseconds = divmod(microseconds, 1000000)
[426] Fix | Delete
days, seconds = divmod(seconds, 24*3600)
[427] Fix | Delete
d += days
[428] Fix | Delete
s += seconds
[429] Fix | Delete
microseconds = round(microseconds + usdouble)
[430] Fix | Delete
assert isinstance(s, int)
[431] Fix | Delete
assert isinstance(microseconds, int)
[432] Fix | Delete
assert abs(s) <= 3 * 24 * 3600
[433] Fix | Delete
assert abs(microseconds) < 3.1e6
[434] Fix | Delete
[435] Fix | Delete
# Just a little bit of carrying possible for microseconds and seconds.
[436] Fix | Delete
seconds, us = divmod(microseconds, 1000000)
[437] Fix | Delete
s += seconds
[438] Fix | Delete
days, s = divmod(s, 24*3600)
[439] Fix | Delete
d += days
[440] Fix | Delete
[441] Fix | Delete
assert isinstance(d, int)
[442] Fix | Delete
assert isinstance(s, int) and 0 <= s < 24*3600
[443] Fix | Delete
assert isinstance(us, int) and 0 <= us < 1000000
[444] Fix | Delete
[445] Fix | Delete
if abs(d) > 999999999:
[446] Fix | Delete
raise OverflowError("timedelta # of days is too large: %d" % d)
[447] Fix | Delete
[448] Fix | Delete
self = object.__new__(cls)
[449] Fix | Delete
self._days = d
[450] Fix | Delete
self._seconds = s
[451] Fix | Delete
self._microseconds = us
[452] Fix | Delete
self._hashcode = -1
[453] Fix | Delete
return self
[454] Fix | Delete
[455] Fix | Delete
def __repr__(self):
[456] Fix | Delete
if self._microseconds:
[457] Fix | Delete
return "%s.%s(%d, %d, %d)" % (self.__class__.__module__,
[458] Fix | Delete
self.__class__.__qualname__,
[459] Fix | Delete
self._days,
[460] Fix | Delete
self._seconds,
[461] Fix | Delete
self._microseconds)
[462] Fix | Delete
if self._seconds:
[463] Fix | Delete
return "%s.%s(%d, %d)" % (self.__class__.__module__,
[464] Fix | Delete
self.__class__.__qualname__,
[465] Fix | Delete
self._days,
[466] Fix | Delete
self._seconds)
[467] Fix | Delete
return "%s.%s(%d)" % (self.__class__.__module__,
[468] Fix | Delete
self.__class__.__qualname__,
[469] Fix | Delete
self._days)
[470] Fix | Delete
[471] Fix | Delete
def __str__(self):
[472] Fix | Delete
mm, ss = divmod(self._seconds, 60)
[473] Fix | Delete
hh, mm = divmod(mm, 60)
[474] Fix | Delete
s = "%d:%02d:%02d" % (hh, mm, ss)
[475] Fix | Delete
if self._days:
[476] Fix | Delete
def plural(n):
[477] Fix | Delete
return n, abs(n) != 1 and "s" or ""
[478] Fix | Delete
s = ("%d day%s, " % plural(self._days)) + s
[479] Fix | Delete
if self._microseconds:
[480] Fix | Delete
s = s + ".%06d" % self._microseconds
[481] Fix | Delete
return s
[482] Fix | Delete
[483] Fix | Delete
def total_seconds(self):
[484] Fix | Delete
"""Total seconds in the duration."""
[485] Fix | Delete
return ((self.days * 86400 + self.seconds) * 10**6 +
[486] Fix | Delete
self.microseconds) / 10**6
[487] Fix | Delete
[488] Fix | Delete
# Read-only field accessors
[489] Fix | Delete
@property
[490] Fix | Delete
def days(self):
[491] Fix | Delete
"""days"""
[492] Fix | Delete
return self._days
[493] Fix | Delete
[494] Fix | Delete
@property
[495] Fix | Delete
def seconds(self):
[496] Fix | Delete
"""seconds"""
[497] Fix | Delete
return self._seconds
[498] Fix | Delete
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function