def parseTime(time): try: res = Time('2021-01-01 00:00:00').parse(time + '分') res = res[0]['keyDate'][11:16] # yyyy-mm-dd HH:MM:SS except Exception as ex: print(str(ex)) res = -1 #'时间错误!' return res
def py_time(): try: data = flask_content_type(request) current_time = data.get('current_time', None) format_ = data.get('format', 'YYYY-MM-DD HH:mm:ss') string = data.get('string', '') time = Time(current_time=current_time, format_=format_).parse(string=string) return jsonify(code=200, result=time) except Exception as e: return jsonify(code=500, error=str(e))
def py_time(current_time: str = Query( None, description='填写你认为的开始时间', regex= r'[1-9]\d{3}-([0-9]|1[0-2])-([0-9]|[1-2][0-9]|3[0-1]) \d{1,2}:\d{1,2}:\d{1,2}', title='2020-4-22 00:00:00'), data: str = Query(..., description='输入要分析的语句', title='一个小时前')): try: time = Time(current_time=current_time, format_='YYYY-MM-DD HH:mm:ss').parse(string=data) return ResponseModal(result=time) except Exception as e: return ResponseModal(msg=str(e), code=0)
def normalize_date_time(text): result = Time().parse(text) if len(result) == 0: return None, None key_time = result[0]["keyDate"].split()[1] base_time = result[0]["baseDate"].split()[1] result_time = key_time.split(":")[0] for t1, t2 in zip(key_time.split(":")[1:], base_time.split(":")[1:]): if t1 == t2: result_time += ":00" else: result_time += ":" + t1 date = result[0]["keyDate"].split()[0] return date, result_time
def parseDate(date): tokens = ['月', '-', '.', '/', '//'] date = date.replace('周', '星期').replace('礼拜', '星期') for token in tokens: regex = re.findall('^\d{1,2}\\' + token + '\d{1,2}', date) if regex: mm, dd = regex[0].split(token) date = mm + '月' + dd + '日' try: res = Time().parse(date) res = res[0]['keyDate'][:10] # yyyy-mm-dd HH:MM:SS #for i in range(len(res)): # print(res[i]['keyDate'][5:10] + ' ') except Exception as ex: print(str(ex)) res = -1 #'日期错误!' return res
def time(): """字符字符串时间解析""" assert Time('2020-4-22 00:00:00').parse('这个月的第三个星期天') == [{ 'key': '这个月第3个星期天', 'keyDate': '2020-04-19 00:00:00', 'baseDate': '2020-04-22 00:00:00' }] assert Time('2020-4-22 00:00:00').parse('前2年的国庆节的后一天晚上8点半') == [{ 'key': '前2年国庆节后1天晚上8点半', 'keyDate': '2018-09-30 20:30:00', 'baseDate': '2020-04-22 00:00:00' }] assert Time('2020-4-22 00:00:00').parse('上上个月') == [{ 'key': '上上个月', 'keyDate': '2020-02-22 00:00:00', 'baseDate': '2020-04-22 00:00:00' }] assert Time('2020-4-22 00:00:00').parse('今天晚上8点') == [{ 'key': '今天晚上8点', 'keyDate': '2020-04-22 20:00:00', 'baseDate': '2020-04-22 00:00:00' }] assert Time('2020-4-22 00:00:00').parse('今年儿童节晚上九点一刻') == [{ 'key': '今年儿童节晚上9点1刻', 'keyDate': '2020-06-01 21:15:00', 'baseDate': '2020-04-22 00:00:00' }] assert Time('2020-4-22 00:00:00').parse('今天中午十二点') == [{ 'key': '今天中午12点', 'keyDate': '2020-04-22 12:00:00', 'baseDate': '2020-04-22 00:00:00' }] assert Time('2020-4-22 00:00:00').parse('明年春节') == [{ 'key': '明年春节', 'keyDate': '2021-02-12 00:00:00', 'baseDate': '2020-04-22 00:00:00' }] assert Time('2020-4-22 00:00:00').parse('明年的感恩节') == [{ 'key': '明年感恩节', 'keyDate': '2021-11-25 00:00:00', 'baseDate': '2020-04-22 00:00:00' }] assert Time('2020-4-22 00:00:00').parse('下3个星期1早上7点半') == [{ 'key': '下3个星期1早上7点半', 'keyDate': '2020-05-11 07:30:00', 'baseDate': '2020-04-22 00:00:00' }] assert Time('2020-4-22 00:00:00').parse('今年的大寒') == [{ 'key': '今年大寒', 'keyDate': '2021-01-20 00:00:00', 'baseDate': '2020-04-22 00:00:00' }] assert Time('2020-4-22 00:00:00').parse('2019年12月') == [{ 'key': '2019年12月', 'keyDate': '2019-12-01 00:00:00', 'baseDate': '2020-04-22 00:00:00' }] assert Time('2020-4-22 00:00:00').parse('8年前') == [{ 'key': '8年前', 'keyDate': '2012-04-22 00:00:00', 'baseDate': '2020-04-22 00:00:00' }] assert Time('2020-4-22 00:00:00').parse('三天以后') == [{ 'key': '3天以后', 'keyDate': '2020-04-25 00:00:00', 'baseDate': '2020-04-22 00:00:00' }] assert Time('2020-4-22 00:00:00').parse('三天之内的下午3点') == [{ 'key': '3天之内下午3点', 'keyDate': '2020-04-25 15:00:00', 'baseDate': '2020-04-22 00:00:00' }] assert Time('2020-4-22 00:00:00').parse('后三天的下午4点56秒') == [{ 'key': '后3天下午4点56秒', 'keyDate': '2020-04-25 16:00:56', 'baseDate': '2020-04-22 00:00:00' }] assert Time('2020-4-22 00:00:00').parse('2020-1-2 10:20:10') == [{ 'key': '2020-1-2 10:20:10', 'keyDate': '2020-01-02 10:20:10', 'baseDate': '2020-04-22 00:00:00' }] assert Time('2020-4-22 00:00:00').parse('明晚九点,明天晚上九点') == [{ 'key': '明晚9点', 'keyDate': '2020-04-23 21:00:00', 'baseDate': '2020-04-22 00:00:00' }, { 'key': '明天晚上9点', 'keyDate': '2020-04-23 21:00:00', 'baseDate': '2020-04-22 00:00:00' }] assert Time('2020-4-22 00:00:00').parse('昨晚九点,明天晚上九点') == [{ 'key': '昨晚9点', 'keyDate': '2020-04-21 21:00:00', 'baseDate': '2020-04-22 00:00:00' }, { 'key': '明天晚上9点', 'keyDate': '2020-04-23 21:00:00', 'baseDate': '2020-04-22 00:00:00' }] assert Time('2020-4-22 10:10:10').parse('我后天去北京') == [{ 'key': '后天', 'keyDate': '2020-04-24 10:10:10', 'baseDate': '2020-04-22 10:10:10' }] assert Time('2020-9-20 10:10:10').parse('00年出生的孩子爱看的电视剧') == [{ 'key': '00年', 'keyDate': '2000-09-20 10:10:10', 'baseDate': '2020-09-20 10:10:10' }] assert Time('2020-9-20 10:10:10').parse('这个月的最后一天的前一天') == [{ 'key': '这个月最后1天前1天', 'keyDate': '2020-09-29 10:10:10', 'baseDate': '2020-09-20 10:10:10' }] assert Time('2020-9-20 10:10:10').parse('这个月的第一天') == [{ 'key': '这个月第1天', 'keyDate': '2020-09-01 10:10:10', 'baseDate': '2020-09-20 10:10:10' }] assert Time('2020-9-20 10:10:10').parse('这个月的最后1个星期5') == [{ 'key': '这个月最后1个星期5', 'keyDate': '2020-09-25 10:10:10', 'baseDate': '2020-09-20 10:10:10' }] assert Time('2020-9-20 10:10:10').parse('这个月的第1个星期1') == [{ 'key': '这个月第1个星期1', 'keyDate': '2020-09-07 10:10:10', 'baseDate': '2020-09-20 10:10:10' }] assert Time('2020-9-20 10:10:10').parse('一个小时前') == [{ 'key': '1个小时前', 'keyDate': '2020-09-20 09:10:10', 'baseDate': '2020-09-20 10:10:10' }] assert Time('2020-9-20 10:10:10').parse('再过8小时') == [{ 'key': '再过8小时', 'keyDate': '2020-09-20 18:10:10', 'baseDate': '2020-09-20 10:10:10' }]
def company(): print(Time('2021-3-4 20:00:00').parse('请陈小利、苏杨星期三早上10点在省公司参加安全培训'))