From ea340b61860a63e88af0acfb7761b61f8965b7dc Mon Sep 17 00:00:00 2001 From: Stephen Diehl Date: Tue, 10 Apr 2012 08:30:37 -0400 Subject: [PATCH] Datetime subclasses with tzinfo=UTC by default --- zipline/date_utils.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/zipline/date_utils.py b/zipline/date_utils.py index ddbe278b..966525c5 100644 --- a/zipline/date_utils.py +++ b/zipline/date_utils.py @@ -1,5 +1,6 @@ from collections import namedtuple +import time import pytz import calendar from dateutil import rrule @@ -7,8 +8,23 @@ from datetime import datetime, date, timedelta from dateutil.relativedelta import * # Datetime Tuple +# -------------- d_tuple = namedtuple('dt', ['year', 'month', 'day', 'hour', 'minute', 'second', 'micros']) +# UTC Datetime Subclasses +# ----------------------- +def utcnow(): + return datetime.now(pytz.utc) + +class utcdatetime(datetime): + def __new__(cls, *args, **kwargs): + kwargs['tzinfo'] = pytz.utc + dt = datetime.__new__(cls, *args, **kwargs) + return dt + +# Datetime Calculations +# --------------------- + WEEKDAYS = [rrule.MO, rrule.TU, rrule.WE, rrule.TH, rrule.FR] HOLIDAYS = { @@ -27,6 +43,7 @@ HOLIDAYS = { rule = rrule.rrule( rrule.DAILY, byweekday=WEEKDAYS, + cache=True, ) # Precompute the rule, so that dates are cached. @@ -53,3 +70,10 @@ if __name__ == '__main__': # days, excluding the preset holidays. for day in trading_days(now, now30): print day + + # Its now cached so if we do that traversal again it only + # takes like 1e-5 seconds. + tic = time.time() + for day in trading_days(now, now30): + print day + print time.time() - tic