# -*- coding: utf-8 -*- # @Author: Gree # @Date: 2020-12-17 13:39:53 # @Last Modified by: Gree # @Last Modified time: 2021-06-01 14:28:16 import datetime class GetTime(object): """ 1. 该类主要用于得到目前时间,然后获取一周前、一个月前的时间点,根据前时间节点到现今的时间节点时间段, 再通过 conn_sql.py 中 DbRun 类中的方法获取原始数据。 """ def __init__(self): """ __init__ 函数: input: output: features: 定义初始变量 step1: 定义 self.time_now_timestamp、self.time_mid_timestamp 等对象 """ # 获取 self.time_now_timestamp self.time_now_timestamp = datetime.datetime.now() # 获取 self.time_mid_timestamp self.time_mid_timestamp = self.time_now_timestamp - datetime.timedelta(days=250) def get_time(self, days = 0): """ get_time 函数: input: days = 0(形参默认值) output: time_before, time_now features: 该方法主要用于获取所需要的前时间节点,比如一个星期之前,或者一个月之前,形参默认值都为0 step1: 该方法主要用于获取所需要的前时间节点,比如一个星期之前,或者一个月之前,形参默认值都为0 step2: 如果该方法不传参数,采用默认值,则 time_before 和 time_now 都为空字符串 """ # 输出 log 信息 print("Loading the module of get_time ...") # 捕获异常 try: # 条件判断 if days == 0: # 赋值 time_before = '' # 赋值 time_now = '' else: # 获取 time_before_timestamp time_before_timestamp = self.time_now_timestamp - datetime.timedelta(days = days) # 获取 time_now time_now = self.time_now_timestamp.strftime("%Y-%m-%d %H:%M:%S") # 获取 time_before time_before = time_before_timestamp.strftime("%Y-%m-%d %H:%M:%S") # 获取 time_now time_now = "\'" + time_now + "\'" # 获取 time_before time_before = "\'" + time_before + "\'" except Exception as e: print("The error of get_time():", e) else: # 输出 log 信息 print("The information of time is gained!") # 返回 time_before, time_now return time_before, time_now finally: # 输出 log 信息 print("Exiting the module of get_time ...")