chinapnr / fishbase

自主开发、整理的一套 Python 基础函数库,涵盖 system 系统增强包、logger 日志记录增强包、file 文件处理增强包、 date 日期处理函数包、data 数据信息处理函数包、csv 处理增强函数包、crypt 加密/编码增强包等,可减少程序开发工作量、降低引用复杂度。
MIT License
94 stars 29 forks source link

fish_date 中 GetRandomTime 中函数优化和修改 #288

Closed wingfish closed 3 years ago

wingfish commented 3 years ago
  1. date_time_this_month() 函数,获取当前月的随机时间,修改名称为 random_datetime_this_month()

docstring 和 代码都需要修改;

@staticmethod
    def date_time_this_month():
        """
        获取当前月的随机时间

        :return:
            * date_this_month: (datetime) 当前月份的随机时间

        举例如下::

            print('--- GetRandomTime.date_time_this_month demo ---')
            print(GetRandomTime.date_time_this_month())
            print('---')

        执行结果::

            --- GetRandomTime.date_time_this_month demo demo ---
            2018-07-01 12:47:20
            ---

       """
        now = datetime.now()
        this_month_start = now.replace(
            day=1, hour=0, minute=0, second=0, microsecond=0)
        this_month_days = calendar.monthrange(now.year, now.month)
        random_seconds = random.randint(0, this_month_days[1]*A_DAY_SECONDS)

        return this_month_start + timedelta(seconds=random_seconds)
  1. date_time_this_year() 函数,获取当前年的随机时间字符串,修改名称为 random_datetime_this_year()

docstring 和 代码都需要修改;

    @staticmethod
    def date_time_this_year():
        """
        获取当前年的随机时间字符串

        :return:
            * date_this_year: (datetime) 当前月份的随机时间

        举例如下::

            print('--- GetRandomTime.date_time_this_year demo ---')
            print(GetRandomTime.date_time_this_year())
            print('---')

        执行结果::

            --- GetRandomTime.date_time_this_year demo demo ---
            2018-02-08 17:16:09
            ---
       """
        now = datetime.now()
        this_year_start = now.replace(
            month=1, day=1, hour=0, minute=0, second=0, microsecond=0)
        this_year_days = sum(calendar.mdays)
        random_seconds = random.randint(0, this_year_days*A_DAY_SECONDS)

        return this_year_start + timedelta(seconds=random_seconds)
wingfish commented 3 years ago

2021.6.4

 @staticmethod
    def random_datetime_this_month():
        """
        生成一个属于当前月的随机日期时间

        :return:
            * random_date: (datetime) 个属于当前月的随机日期时间

        举例如下::

            print('--- GetRandomTime.random_datetime_this_month demo ---')
            print(GetRandomTime.random_datetime_this_month)
            print('---')

        执行结果::

            --- GetRandomTime.date_time_this_month demo demo ---
            2018-07-01 12:47:20
            ---

       """
        now = datetime.now()
        begin_this_month = now.replace(
            day=1, hour=0, minute=0, second=0, microsecond=0)
        this_month_days = calendar.monthrange(now.year, now.month)
        random_seconds = random.randint(0, this_month_days[1]*A_DAY_SECONDS)

        random_date = begin_this_month + timedelta(seconds=random_seconds)

        return random_date
wingfish commented 3 years ago

2021.6.7 函数名和相关ut中的函数名都已经修改。 之后继续进行代码优化和ut的tc优化。

wingfish commented 3 years ago

Class 名称修改为 RandomTime,动词Get放到下面的函数中 get_random_datetime_this_month get_random_datetime_this_year get_random_date_by_year get_random_date_by_range