2025-04-09 20:34:32 +08:00

34 lines
1.3 KiB
Python

import datetime
from dateutil.relativedelta import relativedelta
def calculate_expiry_date(production_date_str, shelf_life, shelf_unit):
"""Calculates expiry date from production date and shelf life."""
if not production_date_str or not shelf_life or not shelf_unit:
return None
try:
production_date = datetime.date.fromisoformat(production_date_str)
life = int(shelf_life)
if life <= 0: return None # Shelf life must be positive
if shelf_unit == 'days':
return production_date + datetime.timedelta(days=life)
elif shelf_unit == 'months':
return production_date + relativedelta(months=life)
else:
return None # Invalid unit
except (ValueError, TypeError):
return None # Invalid date format or shelf life
def convert_volume(amount, from_unit, to_unit):
"""Converts volume between ml and L."""
if from_unit == to_unit:
return float(amount)
try:
amount = float(amount)
if from_unit == 'ml' and to_unit == 'L':
return amount / 1000.0
if from_unit == 'L' and to_unit == 'ml':
return amount * 1000.0
raise ValueError("Invalid unit conversion")
except (ValueError, TypeError):
raise ValueError(f"Invalid amount or units for conversion: {amount}, {from_unit}, {to_unit}")