Skip to content

Commit

Permalink
fix utc corrections. closes #223
Browse files Browse the repository at this point in the history
  • Loading branch information
fontanka16 committed May 2, 2022
1 parent 2ae9aa6 commit bd05b78
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
15 changes: 3 additions & 12 deletions src/folio_migration_tools/migration_tasks/loans_migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

import requests
from dateutil import parser as du_parser
from folio_uuid.folio_namespaces import FOLIONamespaces
from pydantic import BaseModel

from folio_migration_tools.circulation_helper import CirculationHelper
from folio_migration_tools.custom_dict import InsensitiveDictReader
from folio_migration_tools.helper import Helper
Expand All @@ -25,8 +28,6 @@
from folio_migration_tools.transaction_migration.transaction_result import (
TransactionResult,
)
from folio_uuid.folio_namespaces import FOLIONamespaces
from pydantic import BaseModel


class LoansMigrator(MigrationTaskBase):
Expand Down Expand Up @@ -588,16 +589,6 @@ def change_due_date(self, folio_loan, legacy_loan):
logging.info(exception)
return False, None, None

def make_loan_utc(self, legacy_loan: LegacyLoan):
if self.task_configuration.utc_difference != 0:
legacy_loan.due_date = legacy_loan.due_date + timedelta(
hours=self.task_configuration.utc_difference
)
legacy_loan.out_date = legacy_loan.out_date + timedelta(
hours=self.task_configuration.utc_difference
)
self.migration_report.add_general_statistics("Adjusted out and due dates to UTC")


def timings(t0, t0func, num_objects):
avg = num_objects / (time.time() - t0)
Expand Down
27 changes: 18 additions & 9 deletions src/folio_migration_tools/transaction_migration/legacy_loan.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,24 @@ def __init__(self, legacy_loan_dict, utc_difference=0, row=0):
self.patron_barcode: str = legacy_loan_dict["patron_barcode"].strip()
self.due_date: datetime = temp_date_due
self.out_date: datetime = temp_date_out
self.make_utc()
self.correct_for_1_day_loans()
self.renewal_count = int(legacy_loan_dict["renewal_count"])
self.next_item_status = legacy_loan_dict.get("next_item_status", "").strip()
if self.next_item_status not in legal_statuses:
self.errors.append(("Not an allowed status", self.next_item_status))

def correct_for_1_day_loans(self):
try:
self.make_loan_utc()
if self.due_date <= self.out_date:
if self.due_date.hour == 0:
self.due_date = self.due_date.replace(hour=23, minute=59)
if self.out_date.hour == 0:
self.out_date = self.out_date.replace(hour=0, minute=1)
if self.due_date <= self.out_date:
raise ValueError("Due date is before out date")
except Exception:
self.errors.append(("Time alignment issues", "both dates"))
self.renewal_count = int(legacy_loan_dict["renewal_count"])
self.next_item_status = legacy_loan_dict.get("next_item_status", "").strip()
if self.next_item_status not in legal_statuses:
self.errors.append(("Not an allowed status", self.next_item_status))

def to_dict(self):
return {
Expand All @@ -73,7 +78,11 @@ def to_dict(self):
"next_item_status": self.next_item_status,
}

def make_loan_utc(self):
if self.utc_difference != 0:
self.due_date = self.due_date + timedelta(hours=self.utc_difference)
self.out_date = self.out_date + timedelta(hours=self.utc_difference)
def make_utc(self):
try:
hours_to_add = -1 * self.utc_difference
if self.utc_difference != 0:
self.due_date = self.due_date + timedelta(hours=hours_to_add)
self.out_date = self.out_date + timedelta(hours=hours_to_add)
except Exception:
self.errors.append(("UTC correction issues", "both dates"))
25 changes: 21 additions & 4 deletions tests/test_legacy_loan.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ def test_init_tz():
loan_dict = {
"item_barcode": "the barcode with trailing space ",
"patron_barcode": " the barcode with leading space",
"due_date": "20220113 22:00",
"out_date": "20220113 20:00",
"due_date": "20220113 16:00",
"out_date": "20220113 14:00",
"renewal_count": "1",
"next_item_status": "Checked out",
}
legacy_loan = LegacyLoan(loan_dict, -6)
assert legacy_loan.patron_barcode == "the barcode with leading space"
assert legacy_loan.item_barcode == "the barcode with trailing space"
assert legacy_loan.due_date.isoformat() == "2022-01-13T16:00:00"
assert legacy_loan.out_date.isoformat() == "2022-01-13T14:00:00"
assert legacy_loan.due_date.isoformat() == "2022-01-13T22:00:00"
assert legacy_loan.out_date.isoformat() == "2022-01-13T20:00:00"
assert legacy_loan.renewal_count > 0


Expand All @@ -48,3 +48,20 @@ def test_init_tz_2():
assert legacy_loan.due_date.isoformat() == "2019-02-22T23:59:00"
assert legacy_loan.out_date.isoformat() == "2019-02-22T10:53:00"
assert legacy_loan.renewal_count > 0


def test_init_tz_3():
loan_dict = {
"item_barcode": "the barcode with trailing space ",
"patron_barcode": " the barcode with leading space",
"due_date": "20220113 16:00",
"out_date": "20220113 14:00",
"renewal_count": "1",
"next_item_status": "Checked out",
}
legacy_loan = LegacyLoan(loan_dict, 6)
assert legacy_loan.patron_barcode == "the barcode with leading space"
assert legacy_loan.item_barcode == "the barcode with trailing space"
assert legacy_loan.due_date.isoformat() == "2022-01-13T10:00:00"
assert legacy_loan.out_date.isoformat() == "2022-01-13T08:00:00"
assert legacy_loan.renewal_count > 0

0 comments on commit bd05b78

Please sign in to comment.