Example #1
0
 def find_create_with(cls, start_on, end_on, period_type=None):
     ''' creates a period with defined start and end dates '''
     start_on = normalize_date(start_on, as_aware=True)
     end_on = normalize_date(end_on, as_aware=True)
     if not period_type:
         period_type = cls.type()
     try:
         period = cls.objects.get(start_on=start_on,
                                  end_on=end_on, period_type=period_type)
     except cls.DoesNotExist:
         period = cls(start_on=start_on, end_on=end_on,
                      period_type=period_type)
         period.save()
     return period
Example #2
0
    def boundaries(cls, date_obj):
        date_obj = normalize_date(date_obj, as_aware=True)

        start = date_obj.replace(month=0, day=0, hour=0, minute=0,
                                 second=0, microsecond=0)
        end = start.replace(year=date_obj.year + 1) - timedelta(ONE_MICROSECOND)
        return (start, end)
Example #3
0
    def boundaries(cls, date_obj):
        date_obj = normalize_date(date_obj, as_aware=True)

        start = date_obj - timedelta(date_obj.weekday())
        start = start.replace(hour=0, minute=0, second=0, microsecond=0)
        end = start + timedelta(cls.delta()) - timedelta(ONE_MICROSECOND)
        return (start, end)
Example #4
0
    def boundaries(cls, date_obj):
        date_obj = normalize_date(date_obj, as_aware=True)

        nyear, nmonth = next_month(date_obj.year, date_obj.month)

        start = date_obj.replace(day=1, hour=0, minute=0,
                                 second=0, microsecond=0)
        end = start.replace(year=nyear, month=nmonth) \
            - timedelta(ONE_MICROSECOND)
        return (start, end)
Example #5
0
    def find_create_by_date(cls, date_obj, dont_create=False):
        ''' creates a period to fit the provided date in '''
        if not isinstance(date_obj, datetime):
            date_obj = datetime.fromtimestamp(float(date_obj.strftime('%s')))
            date_obj = datetime(date_obj.year, date_obj.month,
                                date_obj.day, date_obj.hour,
                                date_obj.minute, 1,
                                tzinfo=timezone.utc)

        date_obj = normalize_date(date_obj, as_aware=True)
        try:
            period = [period for period in cls.objects.all()
                      if period.start_on <= date_obj
                      and period.end_on >= date_obj][0]
        except IndexError:

            period = cls.find_create_with(*cls.boundaries(date_obj))
            if dont_create:
                return period
            period.save()
        return period
Example #6
0
    def boundaries(cls, date_obj):

        date_obj = normalize_date(date_obj, as_aware=True)

        clean_start = date_obj.replace(month=1, day=1, hour=0, minute=0,
                                       second=0, microsecond=0)
        clean_end = clean_start - timedelta(ONE_MICROSECOND)
        clean_end = clean_end.replace(year=date_obj.year)

        if date_obj.month in (1, 2, 3):
            start = clean_start.replace(month=1)
            end = start.replace(month=4) - timedelta(ONE_MICROSECOND)
        elif date_obj.month in (4, 5, 6):
            start = clean_start.replace(month=4)
            end = start.replace(month=7) - timedelta(ONE_MICROSECOND)
        elif date_obj.month in (7, 8, 9):
            start = clean_start.replace(month=7)
            end = start.replace(month=10) - timedelta(ONE_MICROSECOND)
        else:
            start = clean_start.replace(month=10)
            end = clean_end

        return (start, end)
Example #7
0
 def normalize_date(self, obj):
     return normalize_date(obj, as_aware=self.is_aware())