Sunday, December 14, 2008

Python Time Comparison Tricks

Cool operator overflow feature in Python brings a lot of abstraction.
When comparing Date and Time this brings an easy way to do it.


# Imports
import os
import datetime
from datetime import timedelta

# Get current time value:
current=datetime.datetime.now()

# Advancing Time (examples hours, minutes, days, seconds):
# Advancing 1 hour and 30 seconds
adv=current + timedelta(hours=1, seconds=30)

# Getting Delta Time
diff = adv - current
# Comparing
if diff > timedelta(hours=1):
bigger=true
else:
bigger=False


Very easy!

No comments:

Post a Comment