Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
DateTime Filter - Helper class extending get_datetime()

This is a helper class I put together because I wanted to output different timestamp formats in my logs using our native get_datetime().

I also wanted something semantically memorable to me. So I structured it around get_ calls with descriptive names.

This is a very basic example and can be modified to use any filter/format type and naming convention that is meaningful to you.

I keep it in my notebook so I can copy it into each algo.

Please feel free to revise and extend!

import datetime

def initialize(context):  
    #example outputs  
    print datetime_filter.get_timestamp()  
    print datetime_filter.get_pretty_date()  
    print datetime_filter.get_mdy_date()  
    print datetime_filter.get_day_name()  
    print datetime_filter.get_day_num()


class DateTimeFilter(object):  
    def __init__(self):  
        self.pretty_date_format = "%a %b %d %H:%M:%S %Y"  
        self.mdy_date_format = "%x"  
        self.day_name_format = "%a"  
        self.day_num_format = "%w"

    def get_timestamp(self): return get_datetime('US/Eastern')  
    def get_pretty_date(self): return datetime_filter.get_timestamp().strftime(self.pretty_date_format)  
    def get_mdy_date(self): return datetime_filter.get_timestamp().strftime(self.mdy_date_format)  
    def get_day_name(self): return datetime_filter.get_timestamp().strftime(self.day_name_format)  
    def get_day_num(self): return datetime_filter.get_timestamp().strftime(self.day_num_format)


datetime_filter = DateTimeFilter()