Complete worked example
python
from datetime import datetime, timezone
from decimal import Decimal, ROUND_HALF_EVEN
def inspect_example(price, timestamp):
amount = Decimal(price).quantize(Decimal('0.01'), rounding=ROUND_HALF_EVEN)
instant = datetime.fromisoformat(timestamp)
if instant.utcoffset() is None:
raise ValueError('explicit timezone required')
return {'amount_cents': int(amount * 100),
'utc': instant.astimezone(timezone.utc).isoformat()}- Reasoning
- Decimal receives the original price string, then quantize applies the chosen cent precision before conversion to an integer. The timestamp parser returns an aware datetime, and astimezone converts its represented instant to UTC. The report contains both the integer amount and normalized ISO timestamp.