def date_time_between_dates(self, datetime_start=None, datetime_end=None, tzinfo=None): """ Takes two DateTime objects and returns a random datetime between the two given datetimes. Accepts DateTime objects. :param datetime_start: DateTime :param datetime_end: DateTime :param tzinfo: timezone, instance of datetime.tzinfo subclass :example DateTime('1999-02-02 11:42:52') :return DateTime """ if datetime_start is None: datetime_start = datetime.now(tzinfo) if datetime_end is None: datetime_end = datetime.now(tzinfo) timestamp = self.generator.random.randint( datetime_to_timestamp(datetime_start), datetime_to_timestamp(datetime_end), ) if tzinfo is None: pick = datetime.fromtimestamp(timestamp, tzlocal()) pick = pick.astimezone(tzutc()).replace(tzinfo=None) else: pick = datetime.fromtimestamp(timestamp, tzinfo) return pick
def timestamp_to_datetime(timestamp, tzinfo): if tzinfo is None: pick = datetime.fromtimestamp(timestamp, tzlocal()) pick = pick.astimezone(tzutc()).replace(tzinfo=None) else: pick = datetime.fromtimestamp(timestamp, tzinfo) return pick
def date_time(cls): """ Get a datetime object for a date between January 1, 1970 and now :example DateTime('2005-08-16 20:39:21') :return datetime """ return datetime.fromtimestamp(cls.unix_time())
def date_time_ad(cls): """ Get a datetime object for a date between January 1, 001 and now :example DateTime('1265-03-22 21:15:52') :return datetime """ return datetime.fromtimestamp(random.randint(-62135600400, int(time())))
def date_time(cls, tzinfo=None): """ Get a datetime object for a date between January 1, 1970 and now :param tzinfo: timezone, instance of datetime.tzinfo subclass :example DateTime('2005-08-16 20:39:21') :return datetime """ return datetime.fromtimestamp(cls.unix_time(), tzinfo)
def date_time_ad(cls): """ Get a datetime object for a date between January 1, 001 and now :example DateTime('1265-03-22 21:15:52') :return datetime """ ts = random.randint(-62135600400, int(time())) # NOTE: using datetime.fromtimestamp(ts) directly will raise # a "ValueError: timestamp out of range for platform time_t" # on some platforms due to system C functions; # see http://stackoverflow.com/a/10588133/2315612 return datetime.fromtimestamp(0) + timedelta(seconds=ts)
def date_time_between(cls, start_date='-30y', end_date='now'): """ 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" :example DateTime('1999-02-02 11:42:52') :return DateTime """ start_date = cls._parse_date_time(start_date) end_date = cls._parse_date_time(end_date) timestamp = random.randint(start_date, end_date) return datetime.fromtimestamp(timestamp)
def date_time_between_dates(cls, datetime_start=None, datetime_end=None): """ Takes two DateTime objects and returns a random date between the two given dates. Accepts DateTime objects. :param datetime_start DateTime :param datetime_end DateTime :example DateTime('1999-02-02 11:42:52') :return DateTime """ if datetime_start is None: datetime_start = datetime.now() if datetime_end is None: datetime_end = datetime.now() timestamp = random.randint(datetime_to_timestamp(datetime_start), datetime_to_timestamp(datetime_end)) return datetime.fromtimestamp(timestamp)
def date_time_between_dates(cls, datetime_start=None, datetime_end=None, tzinfo=None): """ Takes two DateTime objects and returns a random date between the two given dates. Accepts DateTime objects. :param datetime_start DateTime :param datetime_end DateTime :param tzinfo: timezone, instance of datetime.tzinfo subclass :example DateTime('1999-02-02 11:42:52') :return DateTime """ if datetime_start is None: datetime_start = datetime.now(tzinfo) if datetime_end is None: datetime_end = datetime.now(tzinfo) timestamp = random.randint( datetime_to_timestamp(datetime_start), datetime_to_timestamp(datetime_end), ) return datetime.fromtimestamp(timestamp, tzinfo)