MCPcopy Create free account
hub / github.com/Boris-code/feapder / format_date

Function format_date

feapder/utils/tools.py:1664–1710  ·  view source on GitHub ↗

@summary: 格式化日期格式 --------- @param date: 日期 eg:2017年4月17日 3时27分12秒 @param old_format: 原来的日期格式 如 '%Y年%m月%d日 %H时%M分%S秒' %y 两位数的年份表示(00-99) %Y 四位数的年份表示(000-9999) %m 月份(01-12) %d 月内中的一天(0-31) %H 24小时制小时数(0-23) %I 12小时制小时数(01-12) %M

(date, old_format="", new_format="%Y-%m-%d %H:%M:%S")

Source from the content-addressed store, hash-verified

1662
1663@run_safe_model("format_date")
1664def format_date(date, old_format="", new_format="%Y-%m-%d %H:%M:%S"):
1665 """
1666 @summary: 格式化日期格式
1667 ---------
1668 @param date: 日期 eg:2017年4月17日 3时27分12秒
1669 @param old_format: 原来的日期格式 如 '%Y年%m月%d日 %H时%M分%S秒'
1670 %y 两位数的年份表示(00-99)
1671 %Y 四位数的年份表示(000-9999)
1672 %m 月份(01-12)
1673 %d 月内中的一天(0-31)
1674 %H 24小时制小时数(0-23)
1675 %I 12小时制小时数(01-12)
1676 %M 分钟数(00-59)
1677 %S 秒(00-59)
1678 @param new_format: 输出的日期格式
1679 ---------
1680 @result: 格式化后的日期,类型为字符串 如2017-4-17 03:27:12
1681 """
1682 if not date:
1683 return ""
1684
1685 if not old_format:
1686 regex = "(\d+)"
1687 numbers = get_info(date, regex, allow_repeat=True)
1688 formats = ["%Y", "%m", "%d", "%H", "%M", "%S"]
1689 old_format = date
1690 for i, number in enumerate(numbers[:6]):
1691 if i == 0 and len(number) == 2: # 年份可能是两位 用小%y
1692 old_format = old_format.replace(
1693 number, formats[i].lower(), 1
1694 ) # 替换一次 '2017年11月30日 11:49' 防止替换11月时,替换11小时
1695 else:
1696 old_format = old_format.replace(number, formats[i], 1) # 替换一次
1697
1698 try:
1699 date_obj = datetime.datetime.strptime(date, old_format)
1700 if "T" in date and "Z" in date:
1701 date_obj += datetime.timedelta(hours=8)
1702 date_str = date_obj.strftime("%Y-%m-%d %H:%M:%S")
1703 else:
1704 date_str = datetime.datetime.strftime(date_obj, new_format)
1705
1706 except Exception as e:
1707 log.error("日期格式化出错,old_format = %s 不符合 %s 格式" % (old_format, date))
1708 date_str = date
1709
1710 return date_str
1711
1712
1713def transform_lower_num(data_str: str):

Callers 1

format_timeFunction · 0.85

Calls 2

get_infoFunction · 0.85
errorMethod · 0.80

Tested by

no test coverage detected