51
51
)
52
52
from neo4j .time .arithmetic import (
53
53
nano_add ,
54
- nano_sub ,
55
- nano_mul ,
56
54
nano_div ,
57
- nano_mod ,
58
- nano_divmod ,
59
55
symmetric_divmod ,
60
56
round_half_to_even ,
61
57
)
@@ -334,18 +330,16 @@ class Duration(tuple):
334
330
max = None
335
331
336
332
def __new__ (cls , years = 0 , months = 0 , weeks = 0 , days = 0 , hours = 0 , minutes = 0 ,
337
- seconds = 0 , milliseconds = 0 , microseconds = 0 , nanoseconds = 0 ,
338
- subseconds = 0 ):
333
+ seconds = 0 , subseconds = 0 , milliseconds = 0 , microseconds = 0 ,
334
+ nanoseconds = 0 ):
339
335
340
- if subseconds and nanoseconds :
341
- raise ValueError ("Specify either subseconds or nanoseconds" )
342
336
if subseconds :
343
- deprecation_warn ("subseconds will be removed in 5.0."
337
+ deprecation_warn ("` subseconds` will be removed in 5.0. "
344
338
"Use `nanoseconds` instead." )
345
339
with _decimal_context (prec = 9 , rounding = ROUND_HALF_EVEN ):
346
340
nanoseconds = int (Decimal (subseconds ) * NANO_SECONDS )
347
341
348
- mo = round_half_to_even (12 * years + months )
342
+ mo = int (12 * years + months )
349
343
if mo < MIN_INT64 or mo > MAX_INT64 :
350
344
raise ValueError ("Months value out of range" )
351
345
d = int (7 * weeks + days )
@@ -360,26 +354,22 @@ def __new__(cls, years=0, months=0, weeks=0, days=0, hours=0, minutes=0,
360
354
raise ValueError ("Days value out of range" )
361
355
if s < MIN_INT64 or s > MAX_INT64 :
362
356
raise ValueError ("Seconds value out of range" )
363
- if ns < MIN_INT64 or s > MAX_INT64 :
364
- raise ValueError ("Nanoseconds value out of range" )
357
+ if s < MIN_INT64 or s > MAX_INT64 :
358
+ raise ValueError ("Seconds value out of range" )
365
359
return tuple .__new__ (cls , (mo , d , s , ns ))
366
360
367
361
def __bool__ (self ):
368
362
return any (map (bool , self ))
369
363
370
364
__nonzero__ = __bool__
371
365
372
- def _check_composite (self ):
373
- if sum ((self [0 ], self [1 ], self [2 ] or self [3 ])) > 1 :
374
- deprecation_warn (
375
- "Dividing composite durations will cause an error in 5.0"
376
- )
377
-
378
366
def __add__ (self , other ):
379
367
if isinstance (other , Duration ):
380
368
return Duration (
381
- months = self [0 ] + other [0 ], days = self [1 ] + other [1 ],
382
- seconds = self [2 ] + other [2 ], nanoseconds = self [3 ] + other [3 ]
369
+ months = self [0 ] + int (other .months ),
370
+ days = self [1 ] + int (other .days ),
371
+ seconds = self [2 ] + int (other .seconds ),
372
+ nanoseconds = self [3 ] + int (other .nanoseconds )
383
373
)
384
374
if isinstance (other , timedelta ):
385
375
return Duration (
@@ -392,30 +382,35 @@ def __add__(self, other):
392
382
def __sub__ (self , other ):
393
383
if isinstance (other , Duration ):
394
384
return Duration (
395
- months = self [0 ] - other [0 ], days = self [1 ] - other [1 ],
396
- seconds = self [2 ] - other [2 ],
397
- nanoseconds = self [3 ] - other [3 ]
385
+ months = self [0 ] - int (other .months ),
386
+ days = self [1 ] - int (other .days ),
387
+ seconds = self [2 ] - int (other .seconds ),
388
+ nanoseconds = self [3 ] - int (other .nanoseconds )
398
389
)
399
390
if isinstance (other , timedelta ):
400
391
return Duration (
401
- months = self [0 ], days = self [1 ] - other .days ,
392
+ months = self [0 ],
393
+ days = self [1 ] - other .days ,
402
394
seconds = self [2 ] - other .seconds ,
403
395
nanoseconds = self [3 ] - other .microseconds * 1000
404
396
)
405
397
return NotImplemented
406
398
407
399
def __mul__ (self , other ):
400
+ if isinstance (other , float ):
401
+ deprecation_warn ("Multiplication with float will be deprecated in "
402
+ "5.0." )
408
403
if isinstance (other , (int , float )):
409
404
return Duration (
410
405
months = self [0 ] * other , days = self [1 ] * other ,
411
406
seconds = self [2 ] * other , nanoseconds = self [3 ] * other
412
407
)
413
408
return NotImplemented
414
409
410
+ @deprecated ("Will be removed in 5.0." )
415
411
def __floordiv__ (self , other ):
416
- self ._check_composite ()
417
412
if isinstance (other , int ):
418
- # TODO 5.0: new method (floor months, days, nanoseconds)
413
+ # TODO 5.0: new method (floor months, days, nanoseconds) or remove
419
414
# return Duration(
420
415
# months=self[0] // other, days=self[1] // other,
421
416
# nanoseconds=(self[2] * NANO_SECONDS + self[3]) // other
@@ -426,10 +421,10 @@ def __floordiv__(self, other):
426
421
seconds = int (seconds // other ))
427
422
return NotImplemented
428
423
424
+ @deprecated ("Will be removed in 5.0." )
429
425
def __mod__ (self , other ):
430
- self ._check_composite ()
431
426
if isinstance (other , int ):
432
- # TODO 5.0: new method (mod months, days, nanoseconds)
427
+ # TODO 5.0: new method (mod months, days, nanoseconds) or remove
433
428
# return Duration(
434
429
# months=self[0] % other, days=self[1] % other,
435
430
# nanoseconds=(self[2] * NANO_SECONDS + self[3]) % other
@@ -441,13 +436,14 @@ def __mod__(self, other):
441
436
seconds = seconds , subseconds = subseconds )
442
437
return NotImplemented
443
438
439
+ @deprecated ("Will be removed in 5.0." )
444
440
def __divmod__ (self , other ):
445
441
if isinstance (other , int ):
446
442
return self .__floordiv__ (other ), self .__mod__ (other )
447
443
return NotImplemented
448
444
445
+ @deprecated ("Will be removed in 5.0." )
449
446
def __truediv__ (self , other ):
450
- self ._check_composite ()
451
447
if isinstance (other , (int , float )):
452
448
return Duration (
453
449
months = round_half_to_even (self [0 ] / other ),
@@ -1141,9 +1137,10 @@ def from_iso_format(cls, s):
1141
1137
@classmethod
1142
1138
def from_ticks (cls , ticks , tz = None ):
1143
1139
if 0 <= ticks < 86400 :
1144
- minute , second = nano_divmod (ticks , 60 )
1145
- hour , minute = divmod (minute , 60 )
1146
- return cls .__new (ticks , hour , minute , second , tz )
1140
+ ticks = Decimal (ticks ) * NANO_SECONDS
1141
+ ticks = int (ticks .quantize (Decimal ("1." ), rounding = ROUND_HALF_EVEN ))
1142
+ assert 0 <= ticks < 86400000000000
1143
+ return cls .from_ticks_ns (ticks , tz = tz )
1147
1144
raise ValueError ("Ticks out of range (0..86400)" )
1148
1145
1149
1146
@classmethod
@@ -1249,7 +1246,7 @@ def ticks(self):
1249
1246
def ticks_ns (self ):
1250
1247
""" Return the total number of seconds since midnight.
1251
1248
"""
1252
- # TODO 5.0: this will replace self.__ticks
1249
+ # TODO 5.0: this will replace self.ticks
1253
1250
return self .__ticks
1254
1251
1255
1252
@property
@@ -1408,9 +1405,9 @@ def to_native(self):
1408
1405
""" Convert to a native Python `datetime.time` value.
1409
1406
"""
1410
1407
h , m , s , ns = self .hour_minute_second_nanoseconds
1411
- mic_s = round_half_to_even (ns / 1000 )
1408
+ µs = round_half_to_even (ns / 1000 )
1412
1409
tz = self .tzinfo
1413
- return time (h , m , s , mic_s , tz )
1410
+ return time (h , m , s , µs , tz )
1414
1411
1415
1412
def iso_format (self ):
1416
1413
s = "%02d:%02d:%02d.%09d" % self .hour_minute_second_nanoseconds
@@ -1568,8 +1565,8 @@ def from_clock_time(cls, clock_time, epoch):
1568
1565
else :
1569
1566
ordinal , seconds = divmod (seconds , 86400 )
1570
1567
ticks = epoch .time ().ticks_ns + seconds * NANO_SECONDS + nanoseconds
1571
- ticks_overflow , ticks = divmod (ticks , 86400 * NANO_SECONDS )
1572
- ordinal += ticks_overflow
1568
+ days , ticks = divmod (ticks , 86400 * NANO_SECONDS )
1569
+ ordinal += days
1573
1570
date_ = Date .from_ordinal (ordinal + epoch .date ().to_ordinal ())
1574
1571
time_ = Time .from_ticks_ns (ticks )
1575
1572
return cls .combine (date_ , time_ )
0 commit comments