鸿蒙 PC使用ohos-pip-autosign激活自动签名工具,安装第三方库arrow实现Python人性化时间处理库

鸿蒙 PC使用ohos-pip-autosign激活自动签名工具,安装第三方库arrow实现Python人性化时间处理库 欢迎加入开源鸿蒙PC社区https://harmonypc.csdn.net/欢迎在PC社区平台申请新建项目https://atomgit.com/OpenHarmonyPCDeveloperAtomGit 仓库地址https://atomgit.com/OpenHarmonyPCDeveloper/ohos_python_numpy1. 环境搭建本文介绍在鸿蒙 PCCodeArts IDE 搭建 Python 开发环境。借助鸿蒙专属包管理器 Harmonybrew 安装适配版 Python搭配 ohos-pip-autosign 自动签名工具解决系统对动态库的签名限制。通过虚拟环境隔离依赖以 NumPy 完成安装与脚本测试成功解决权限报错搭建出可用的 Python 开发环境。可以参考以下文章OpenHarmony 鸿蒙 PC CodeArts IDE 实现 Python开发完整开发环境搭建指南Python Arrow 库完整详解 全套实战 Demo一、Arrow 是什么arrow是 Python 第三方人性化时间处理库对原生datetime、time、calendar做了高度封装解决原生日期 API 繁琐、时区处理麻烦、格式化代码冗长的痛点。原生 datetime 痛点格式化、时间差、时区转换代码量大时间加减、上月/下周/季度获取逻辑复杂解析各种不规则日期字符串非常麻烦时区操作容易出错Arrow 核心优势链式调用一行代码搞定时间操作内置大量人性化时间语义shift(months-1)上月、ceil(day)当天结束自动兼容各种日期字符串解析完善时区、时间戳、本地时间/UTC互转人性化相对时间输出2小时前、3天内支持格式化、截取、区间、季度、周操作二、安装pip3installarrow三、核心基础API总览importarrow# 1. 获取当前时间本地/UTCarrow.now()# 本地当前时间arrow.utcnow()# UTC标准时间# 2. 构造指定时间arrow.Arrow(2026,6,17,12,30)arrow.get(2026-06-17 15:20:10)# 3. 时间戳互转arrow.get(1781712000)# 秒级时间戳arrow.get(1781712000000)# 毫秒时间戳# 4. 格式化输出t.format(YYYY-MM-DD HH:mm:ss)# 5. 时间偏移加减t.shift(days1)# 加1天t.shift(months-1)# 减1个月# 6. 取边界t.floor(day)# 当天0点t.ceil(hour)# 当前小时最后一秒# 7. 相对时间t.humanize(localezh-cn)# 中文3小时前完整可运行 Demo分场景Demo1获取当前时间、格式化、时间戳转换importarrow# 1. 当前本地时间nowarrow.now()print(本地完整时间对象,now)print(当前年月日时分秒,now.format(YYYY-MM-DD HH:mm:ss))print(当前日期,now.format(YYYY-MM-DD))print(当前时间,now.format(HH:mm:ss))print(当前年份季度,now.format(YYYY-Q))# 2. UTC 零时区时间utc_nowarrow.utcnow()print(\nUTC时间,utc_now.format(YYYY-MM-DD HH:mm:ss))# 3. 转秒级时间戳ts_secnow.timestamp()print(秒级时间戳,ts_sec)# 毫秒时间戳ts_msint(now.timestamp()*1000)print(毫秒时间戳,ts_ms)# 4. 时间戳还原时间对象t1arrow.get(ts_sec)t2arrow.get(ts_ms/1000)print(\n时间戳还原,t1.format(YYYY-MM-DD HH:mm:ss))# 5. 自定义格式化模板print(年月中文,now.format(YYYY年MM月DD日 HH点mm分))Demo2解析任意格式日期字符串最强功能无需写复杂匹配模板自动识别各种日期文本importarrow# 各种不规则字符串自动解析time_str_list[2026-06-17,2026/06/17 14:30,20260617152010,17 Jun 2026,2026-06-17T12:30:0008:00,2026年06月17日 16点20分]forsintime_str_list:tarrow.get(s)print(f原字符串{s:30}→ 标准格式{t.format(YYYY-MM-DD HH:mm:ss)})# 指定格式解析严格匹配tarrow.get(17|06|2026,DD|MM|YYYY)print(\n自定义分隔符解析,t.format(YYYY-MM-DD))Demo3时间偏移加减天/月/年/小时importarrow nowarrow.now()print(现在,now.format(YYYY-MM-DD HH:mm:ss))# 加1天t1now.shift(days1)print(明天,t1.format(YYYY-MM-DD))# 减3天t2now.shift(days-3)print(3天前,t2.format(YYYY-MM-DD))# 上个月、下个月last_monthnow.shift(months-1)next_monthnow.shift(months1)print(上月,last_month.format(YYYY-MM))print(下月,next_month.format(YYYY-MM))# 去年、明年last_yearnow.shift(years-1)print(去年今日,last_year.format(YYYY-MM-DD))# 小时、分钟、秒偏移add_2hnow.shift(hours2)sub_30mnow.shift(minutes-30)print(2小时后,add_2h.format(HH:mm))print(30分钟前,sub_30m.format(HH:mm))# 周偏移next_weeknow.shift(weeks1)print(下周今天,next_week.format(YYYY-MM-DD))Demo4取区间边界当天0点、当月首尾、季度importarrow nowarrow.now()# 当天零点 / 当天23:59:59day_startnow.floor(day)day_endnow.ceil(day)print(今日0点,day_start.format(YYYY-MM-DD HH:mm:ss))print(今日结束,day_end.format(YYYY-MM-DD HH:mm:ss))# 当月第一天、当月最后一天month_startnow.floor(month)month_endnow.ceil(month)print(\n本月第一天,month_start.format(YYYY-MM-DD))print(本月最后一天,month_end.format(YYYY-MM-DD))# 季度首尾quarter_startnow.floor(quarter)quarter_endnow.ceil(quarter)print(\n本季度起始,quarter_start.format(YYYY-MM-DD))print(本季度结束,quarter_end.format(YYYY-MM-DD))# 当年起始year_startnow.floor(year)print(今年第一天,year_start.format(YYYY-MM-DD))Demo5人性化相对时间中文几分钟前/几天内报表、日志、消息列表最常用importarrow# 构造几个时间点t1arrow.now().shift(minutes-5)t2arrow.now().shift(hours-3)t3arrow.now().shift(days-2)t4arrow.now().shift(weeks-1)t5arrow.now().shift(months-2)# localezh-cn 输出中文print(5分钟前,t1.humanize(localezh-cn))print(3小时前,t2.humanize(localezh-cn))print(2天前,t3.humanize(localezh-cn))print(1周前,t4.humanize(localezh-cn))print(2个月前,t5.humanize(localezh-cn))# 未来时间futurearrow.now().shift(days3)print(3天后,future.humanize(localezh-cn))Demo6时间比较、区间判断、计算时间差importarrow t1arrow.get(2026-01-01)t2arrow.get(2026-06-17)nowarrow.now()# 大小比较print(t1 t2,t1t2)print(t2 t1,t2t1)print(相等,t1t2)# 判断是否在区间内startarrow.get(2026-01-01)endarrow.get(2026-12-31)print(now 在2026区间,startnowend)# 计算相差天数、小时、秒diff_days(t2-t1).days diff_seconds(t2-t1).total_seconds()print(f\nt1到t2相差天数{diff_days})print(f相差总秒数{diff_seconds})# 精确差值deltat2-t1print(差值对象天,delta.days)print(差值总秒,delta.total_seconds())Demo7时区转换UTC ↔ 北京时间Asia/Shanghai处理接口UTC时间、国际时间必备importarrow# UTC时间转北京时间utc_tarrow.utcnow()cn_tutc_t.to(Asia/Shanghai)print(UTC时间,utc_t.format(YYYY-MM-DD HH:mm:ss))print(北京时间,cn_t.format(YYYY-MM-DD HH:mm:ss))# 本地时间转UTClocalarrow.now(Asia/Shanghai)back_utclocal.to(UTC)print(\n本地转UTC,back_utc.format(YYYY-MM-DD HH:mm:ss))# 指定时区构造时间ny_tarrow.now(America/New_York)print(纽约当前时间,ny_t.format(YYYY-MM-DD HH:mm:ss))Demo8常用业务工具封装可直接复制到项目importarrowclassArrowTimeUtil:staticmethoddefget_now_str(fmtYYYY-MM-DD HH:mm:ss):获取当前格式化字符串returnarrow.now().format(fmt)staticmethoddefget_ts_ms():获取毫秒时间戳returnint(arrow.now().timestamp()*1000)staticmethoddefstr_to_time(date_str,fmtYYYY-MM-DD HH:mm:ss):日期字符串转Arrow对象returnarrow.get(date_str,fmt)staticmethoddefget_day_range(offset0):获取某天的起止时间 offset0今天 -1昨天 1明天targetarrow.now().shift(daysoffset)starttarget.floor(day)endtarget.ceil(day)returnstart.format(),end.format()staticmethoddefget_month_range(offset0):获取某月起止 offset0本月 -1上月targetarrow.now().shift(monthsoffset)starttarget.floor(month)endtarget.ceil(month)returnstart.format(YYYY-MM-DD),end.format(YYYY-MM-DD)staticmethoddefhuman_cn(time_obj):输出中文相对时间returntime_obj.humanize(localezh-cn)if__name____main__:utilArrowTimeUtil()print(当前时间,util.get_now_str())print(毫秒时间戳,util.get_ts_ms())print(今日区间,util.get_day_range(0))print(上月区间,util.get_month_range(-1))tarrow.now().shift(hours-2)print(相对时间,util.human_cn(t))这是一套基于 Pythonarrow库封装的日期时间工具类统一处理时间格式化、时间戳、日期区间、中文相对时间全部静态方法不用实例化就能调用日常业务接口、日志、数据库时间处理非常好用。1. 头部导入importarrowarrow是 Python 第三方时间库比原生datetime更简洁支持时间偏移、格式化、本地化、时间戳转换安装命令pipinstallarrow2. 工具类定义ArrowTimeUtil全部方法加staticmethod静态装饰器调用方式ArrowTimeUtil.方法名()不需要创建对象。get_now_str() 获取当前格式化时间字符串staticmethoddefget_now_str(fmtYYYY-MM-DD HH:mm:ss):获取当前格式化字符串returnarrow.now().format(fmt)arrow.now()获取当前本地时间 Arrow 对象默认格式YYYY-MM-DD HH:mm:ss年-月-日 时:分:秒支持自定义传入 fmt例如get_now_str(YYYY-MM-DD)只返回日期get_ts_ms() 获取毫秒时间戳staticmethoddefget_ts_ms():获取毫秒时间戳returnint(arrow.now().timestamp()*1000).timestamp()返回秒级浮点数乘以1000转为毫秒int()去掉小数返回整数毫秒戳前后端交互常用str_to_time() 时间字符串转 Arrow 对象staticmethoddefstr_to_time(date_str,fmtYYYY-MM-DD HH:mm:ss):日期字符串转Arrow对象returnarrow.get(date_str,fmt)把文本时间如2026-06-23 12:30:00解析成可运算的 Arrow 时间对象后续可以做加减、对比、格式化。get_day_range(offset0) 获取某一天的开始、结束时间staticmethoddefget_day_range(offset0):获取某天的起止时间 offset0今天 -1昨天 1明天targetarrow.now().shift(daysoffset)starttarget.floor(day)endtarget.ceil(day)returnstart.format(),end.format()shift(daysoffset)时间偏移offset0 → 今天offset-1 → 昨天offset1 → 明天.floor(day)当天零点00:00:00.ceil(day)当天23:59:59.999返回两个格式化字符串用于数据库查询当天数据区间get_month_range(offset0) 获取某月起止日期staticmethoddefget_month_range(offset0):获取某月起止 offset0本月 -1上月targetarrow.now().shift(monthsoffset)starttarget.floor(month)endtarget.ceil(month)returnstart.format(YYYY-MM-DD),end.format(YYYY-MM-DD)逻辑和日期区间一致单位换成月份返回格式只有年月日常用于月度统计报表。human_cn() 生成中文相对时间staticmethoddefhuman_cn(time_obj):输出中文相对时间returntime_obj.humanize(localezh-cn)接收一个 Arrow 时间对象自动计算和当前时间的差值输出人性化中文如2小时前、3天前、1个月前适合消息列表、操作日志展示。3. 主函数测试代码if__name____main__:utilArrowTimeUtil()print(当前时间,util.get_now_str())print(毫秒时间戳,util.get_ts_ms())print(今日区间,util.get_day_range(0))print(上月区间,util.get_month_range(-1))tarrow.now().shift(hours-2)print(相对时间,util.human_cn(t))if __name__ __main__代表直接运行该文件时执行测试打印当前标准时间字符串打印毫秒时间戳打印今天0点~明天0点区间打印上月第一天到当月第一天区间构造2小时前的时间输出中文“2小时前”整体使用场景总结这套工具统一封装高频时间操作项目中不用重复写时间转换代码接口返回格式化时间前后端毫秒时间戳交互数据库按天/按月筛选数据日志、消息展示人性化中文时间差四、典型业务使用场景接口日志打印自动格式化、毫秒时间戳筛选时间区间查询今日、昨日、本月、上月数据前端传参解析自动识别各种不规则日期字符串消息创建时间展示5分钟前、3天前人性化显示跨时区项目UTC时间与本地时区互转定时任务快速计算N天/月前后执行时间报表统计季度、月度、年度时间边界快速获取五、对比原生 datetime 优势示例原生写法繁琐fromdatetimeimportdatetime,timedeltafromdateutil.relativedeltaimportrelativedelta nowdatetime.now()# 取上月last_monthnow-relativedelta(months1)print(last_month.strftime(%Y-%m))Arrow一行搞定importarrowprint(arrow.now().shift(months-1).format(YYYY-MM))六、注意事项解析模糊日期字符串时如果格式混乱建议传入第二个参数指定模板严格解析避免歧义humanize中文需传入localezh-cn默认英文时区名称标准写法Asia/Shanghai不要写GMT8时间差相减返回timedelta对象用.days取天数、.total_seconds()取总秒存储数据库推荐格式化YYYY-MM-DD HH:mm:ss或毫秒时间戳可读性高。