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:

# 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):

Very easy!

No comments:

Post a Comment