def date_time_this_decade(cls, before_now=True, after_now=False): """ Gets a DateTime object for the decade year. :param before_now: include days in current decade before today :param after_now: include days in current decade after today :example DateTime('2012-04-04 11:02:02') :return DateTime """ now = datetime.now() this_decade_start = datetime(now.year - (now.year % 10), 1, 1) next_decade_start = datetime(this_decade_start.year + 10, 1, 1) if before_now and after_now: return cls.date_time_between_dates(this_decade_start, next_decade_start) elif not before_now and after_now: return cls.date_time_between_dates(now, next_decade_start) elif not after_now and before_now: return cls.date_time_between_dates(this_decade_start, now) else: return now
def date_time_this_decade(cls, before_now=True, after_now=False, tzinfo=None): """ Gets a DateTime object for the decade year. :param before_now: include days in current decade before today :param after_now: include days in current decade after today :param tzinfo: timezone, instance of datetime.tzinfo subclass :example DateTime('2012-04-04 11:02:02') :return DateTime """ now = datetime.now(tzinfo) this_decade_start = datetime(now.year - (now.year % 10), 1, 1, tzinfo=tzinfo) next_decade_start = datetime(this_decade_start.year + 10, 1, 1, tzinfo=tzinfo) if before_now and after_now: return cls.date_time_between_dates(this_decade_start, next_decade_start, tzinfo) elif not before_now and after_now: return cls.date_time_between_dates(now, next_decade_start, tzinfo) elif not after_now and before_now: return cls.date_time_between_dates(this_decade_start, now, tzinfo) else: return now
def date_time_this_century(cls, before_now=True, after_now=False, tzinfo=None): """ Gets a DateTime object for the current century. :param before_now: include days in current century before today :param after_now: include days in current century after today :param tzinfo: timezone, instance of datetime.tzinfo subclass :example DateTime('2012-04-04 11:02:02') :return DateTime """ now = datetime.now(tzinfo) this_century_start = datetime(now.year - (now.year % 100), 1, 1, tzinfo=tzinfo) next_century_start = datetime(this_century_start.year + 100, 1, 1, tzinfo=tzinfo) if before_now and after_now: return cls.date_time_between_dates(this_century_start, next_century_start, tzinfo) elif not before_now and after_now: return cls.date_time_between_dates(now, next_century_start, tzinfo) elif not after_now and before_now: return cls.date_time_between_dates(this_century_start, now, tzinfo) else: return now
def date_time_between(self, start_date='-30y', end_date='now', tzinfo=None): """ Get a DateTime object based on a random date between two given dates. Accepts date strings that can be recognized by strtotime(). :param start_date Defaults to 30 years ago :param end_date Defaults to "now" :param tzinfo: timezone, instance of datetime.tzinfo subclass :example DateTime('1999-02-02 11:42:52') :return DateTime """ start_date = self._parse_date_time(start_date, tzinfo=tzinfo) end_date = self._parse_date_time(end_date, tzinfo=tzinfo) timestamp = self.generator.random.randint(start_date, end_date) return datetime(1970, 1, 1,tzinfo=tzinfo) + timedelta(seconds=timestamp)
def date_time_this_month(cls, before_now=True, after_now=False): """ Gets a DateTime object for the current month. :param before_now: include days in current month before today :param after_now: include days in current month after today :example DateTime('2012-04-04 11:02:02') :return DateTime """ now = datetime.now() this_month_start = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0) next_month_start = datetime(now.year, now.month + 1 % 12, 1) if before_now and after_now: return cls.date_time_between_dates(this_month_start, next_month_start) elif not before_now and after_now: return cls.date_time_between_dates(now, next_month_start) elif not after_now and before_now: return cls.date_time_between_dates(this_month_start, now) else: return now
def date_time_this_year(cls, before_now=True, after_now=False, tzinfo=None): """ Gets a DateTime object for the current year. :param before_now: include days in current year before today :param after_now: include days in current year after today :param tzinfo: timezone, instance of datetime.tzinfo subclass :example DateTime('2012-04-04 11:02:02') :return DateTime """ now = datetime.now(tzinfo) this_year_start = now.replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0) next_year_start = datetime(now.year + 1, 1, 1, tzinfo=tzinfo) if before_now and after_now: return cls.date_time_between_dates(this_year_start, next_year_start, tzinfo) elif not before_now and after_now: return cls.date_time_between_dates(now, next_year_start, tzinfo) elif not after_now and before_now: return cls.date_time_between_dates(this_year_start, now, tzinfo) else: return now