Skip to content

Instantly share code, notes, and snippets.

from datetime import datetime, timedelta
def is_between(dttm, begin, end):
begin_check = datetime.combine(dttm.date(), begin)
end_check = datetime.combine(dttm.date(), end)
# Handle the case where the range crosses midnight (or both times given are
# the same, assuming semantically you want this mean a 24-hr range instead
# of a null range that always returns False)
@jholloway7
jholloway7 / gist:2014290
Created March 11, 2012 00:53
Implementing a Python datetime localizer using python-dateutil
from dateutil import tz
def _convert_tz(val, from_tzinfo, to_tzinfo):
# Interpret the naive datetime as in the 'from' time zone
val = val.replace(tzinfo=from_tzinfo)
# Convert to specified time zone
val = val.astimezone(to_tzinfo)
# Make it a naive datetime again