Commit a4bc3876 authored by Jérome Perrin's avatar Jérome Perrin

TargetSolver: use simple date arithmetics instead of createDateTimeFromMillis

Maybe this made sense long time ago, but nowadays we are using
equivalence testers which tolerate date differences with more flexibility.

createDateTimeFromMillis was also problematic as it uses internal
private attributes of DateTime which is a pylint error with more recent
DateTime
parent 03674992
Pipeline #20242 running with stage
in 0 seconds
...@@ -28,6 +28,8 @@ from __future__ import absolute_import ...@@ -28,6 +28,8 @@ from __future__ import absolute_import
# #
############################################################################## ##############################################################################
import datetime
from DateTime import DateTime
from .TargetSolver import TargetSolver from .TargetSolver import TargetSolver
class CopyToTarget(TargetSolver): class CopyToTarget(TargetSolver):
...@@ -60,13 +62,11 @@ class CopyToTarget(TargetSolver): ...@@ -60,13 +62,11 @@ class CopyToTarget(TargetSolver):
quantity_ratio = 0 quantity_ratio = 0
if old_quantity not in (None,0.0): # XXX: What if quantity happens to be an integer ? if old_quantity not in (None,0.0): # XXX: What if quantity happens to be an integer ?
quantity_ratio = new_quantity / old_quantity quantity_ratio = new_quantity / old_quantity
start_date_delta = 0 stop_date_delta = start_date_delta = datetime.timedelta()
stop_date_delta = 0
# get the date delta in milliseconds, to prevent rounding issues
if new_start_date is not None and old_start_date is not None: if new_start_date is not None and old_start_date is not None:
start_date_delta = new_start_date.millis() - old_start_date.millis() start_date_delta = new_start_date.asdatetime() - old_start_date.asdatetime()
if new_stop_date is not None and old_stop_date is not None: if new_stop_date is not None and old_stop_date is not None:
stop_date_delta = new_stop_date.millis() - old_stop_date.millis() stop_date_delta = new_stop_date.asdatetime() - old_stop_date.asdatetime()
return { return {
'quantity_ratio': quantity_ratio, 'quantity_ratio': quantity_ratio,
'start_date_delta': start_date_delta, 'start_date_delta': start_date_delta,
...@@ -87,15 +87,14 @@ class CopyToTarget(TargetSolver): ...@@ -87,15 +87,14 @@ class CopyToTarget(TargetSolver):
""" """
Generate values to save on simulation movement. Generate values to save on simulation movement.
""" """
from erp5.component.module.DateUtils import createDateTimeFromMillis
value_dict = {} value_dict = {}
# Modify quantity, start_date, stop_date # Modify quantity, start_date, stop_date
start_date = simulation_movement.getStartDate() start_date = simulation_movement.getStartDate()
if start_date is not None: if start_date is not None:
value_dict['start_date'] = createDateTimeFromMillis(start_date.millis() + start_date_delta) value_dict['start_date'] = DateTime(start_date.asdatetime() + start_date_delta)
stop_date = simulation_movement.getStopDate() stop_date = simulation_movement.getStopDate()
if stop_date is not None: if stop_date is not None:
value_dict['stop_date'] = createDateTimeFromMillis(stop_date.millis() + stop_date_delta) value_dict['stop_date'] = DateTime(stop_date.asdatetime() + stop_date_delta)
value_dict['quantity'] = simulation_movement.getQuantity() * quantity_ratio value_dict['quantity'] = simulation_movement.getQuantity() * quantity_ratio
return value_dict return value_dict
......
...@@ -453,30 +453,6 @@ def convertDateToHour(date=None): ...@@ -453,30 +453,6 @@ def convertDateToHour(date=None):
hour_ = (ordinal_date - ordinal_reference_date) * number_of_hours_in_day + number_of_hours_in_day + date.hour() hour_ = (ordinal_date - ordinal_reference_date) * number_of_hours_in_day + number_of_hours_in_day + date.hour()
return int(hour_) return int(hour_)
def createDateTimeFromMillis(millis): # pylint: disable=redefined-outer-name
"""
Returns a DateTime object, build from the number of milliseconds since epoch.
Parameter should be a int or long.
This one should be used by solvers, as DateTime.__cmp__ actually only
compares the _millis parameter of the two DateTime objects.
This is currently not perfect: DateTime only supports creating a new object
from a floating point number of seconds since epoch, so a rounding issue is
still possible, that's why _millis is explicitely set to the same value
after the DateTime object has been created from (millis / 1000.)
A better way would be to compute (yr,mo,dy,hr,mn,sc,tz,t,d,s,millisecs) from
millis, and then create the DateTime object from it (see "elif ac == 11:" in
DateTime._parse_args).
Another solution would be a DateTime implementation that relies exclusively
on integer values internally.
"""
millis = long(millis)
date = DateTime(millis / 1000.)
date._millis = millis
return date
def getNumberOfDayInMonth(date): def getNumberOfDayInMonth(date):
month = date.month() month = date.month()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment