ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

jc date 解析器实战指南:将 date 命令输出转换为结构化 JSON 时间数据

jc date 解析器实战指南:将 date 命令输出转换为结构化 JSON 时间数据 开发工具【免费下载链接】jcCLI tool and python library that converts the output of popular command-line tools, file-types, and common strings to JSON, YAML, or Dictionaries. This allows piping of output to tools like jq and simplifying automation scripts.项目地址https://gitcode.com/gh_mirrors/jc/jc点击查看免费下载本篇指南聚焦于开源项目 jcJSON Convert中的date命令解析器讲解如何将系统date命令输出转换为包含年月日、时分秒、时区、ISO 8601 时间戳与 epoch 时间戳等 18 个字段的结构化 JSON 数据。读完本文你将掌握date | jc --date的 CLI 用法、Python 模块调用方式、输出 Schema 中每个字段的精确含义以及epoch、epoch_utc、timezone_aware三个计算字段在本地时间与 UTC 场景下的行为差异。date 解析器是什么jc.parsers.date是 jc 项目内置的date命令输出解析器定义于 jc/parsers/date.py。它的作用是把date命令输出的纯文本例如Mon Aug 3 09:12:51 PDT 2020解析为结构化的字典 / JSON 数据方便后续通过jq等工具进行过滤、统计与自动化脚本处理。从源码中的解析器元数据可以看到见 jc/parsers/date.py版本2.6作者Kelly Brazil兼容平台linux、darwin、freebsd标签command、slurpable支持--slurp选项快速上手两种调用方式CLI 管道方式将date命令的输出通过管道交给 jc 解析$ date | jc --date也可以直接让 jc 代为执行date命令$ jc date--date即指定使用名为date的解析器jc date是 jc 的“魔法命令”形式——源码中info.magic_commands [date]见 jc/parsers/date.py表示该解析器可以直接以命令名调用。Python 模块方式在 Python 中调用import jc result jc.parse(date, date_command_output)其中date_command_output是date命令输出的字符串。返回值为字典类型若传入空字符串解析结果为空字典{}该行为由jc.utils.has_data检查以及 tests/test_date.py 中的test_date_nodata用例验证。输出 Schema18 个字段详解parse()函数返回的字典遵循以下 Schema完整定义见 jc/parsers/date.py{ year: integer, month: string, month_num: integer, day: integer, weekday: string, weekday_num: integer, hour: integer, hour_24: integer, minute: integer, second: integer, period: string, timezone: string, utc_offset: string, day_of_year: integer, week_of_year: integer, iso: string, epoch: integer, epoch_utc: integer, timezone_aware: boolean }各字段含义如下表字段类型说明yearinteger年份如 2021monthstring月份缩写%b如Marmonth_numinteger月份数字1-12dayinteger日weekdaystring星期缩写%a如Thuweekday_numintegerISO 星期编号1周一到 7周日由dt.isoweekday()计算hourinteger12 小时制小时1-12hour_24integer24 小时制小时0-23minuteinteger分钟secondinteger秒periodstringAM/PMtimezonestring时区缩写如UTC、PDT找不到时区时为空值utc_offsetstringUTC 偏移量%z如0000当 timezone 字段不是 UTC 时为nullday_of_yearinteger一年中的第几天%jweek_of_yearinteger一年中的第几周%WisostringISO 8601 格式时间戳dt.isoformat()epochinteger本地时区“朴素”epoch 时间戳见下文 [0]epoch_utcinteger时区感知的 UTC epoch 时间戳仅当 timezone 为 UTC 时可用见下文 [1]timezone_awareboolean为true时表示所有字段都基于 UTC 正确换算见下文 [2]三个计算字段的补充说明[0]epochnaive 时间戳基于解析器运行时所在系统的本地时间计算不含时区信息即“朴素”时间戳。[1]epoch_utc时区感知时间戳基于 UTC 计算只有输入数据中的 timezone 字段为 UTC 时才可用其他时区下该字段为null。[2]timezone_aware布尔值为true表示所有字段均基于 UTC 正确生成。实战示例与字段对照示例一非 UTC 时区PDT输入$ date Mon Aug 3 09:12:51 PDT 2020使用-ppretty 输出解析$ date | jc --date -p输出{ year: 2020, month: Aug, month_num: 8, day: 3, weekday: Mon, weekday_num: 1, hour: 9, hour_24: 9, minute: 12, second: 51, period: AM, timezone: PDT, utc_offset: null, day_of_year: 216, week_of_year: 31, iso: 2020-08-03T09:12:51, epoch: 1596471171, epoch_utc: null, timezone_aware: false }该用例与 tests/fixtures/generic/date.out 输入、tests/fixtures/generic/date.json 输出一一对应。可以看到由于时区是 PDT 而非 UTCutc_offset、epoch_utc均为nulltimezone_aware为falseiso字段也不含时区偏移。示例二UTC 时区文档官方示例$ date | jc --date -p输出{ year: 2021, month: Mar, month_num: 3, day: 25, weekday: Thu, weekday_num: 4, hour: 2, hour_24: 2, minute: 2, second: 26, period: AM, timezone: UTC, utc_offset: 0000, day_of_year: 84, week_of_year: 12, iso: 2021-03-25T02:02:2600:00, epoch: 1616662946, epoch_utc: 1616637746, timezone_aware: true }当输入中的时区为 UTC 时utc_offset为0000、epoch_utc有值、timezone_aware为trueiso字段带00:00时区偏移且iso中的时间与hour_24、minute、second完全一致。示例三12 小时制与 24 小时制的换算午夜前PM → 24 小时制输入Wed Mar 24 11:54:47 PM UTC 2021见 tests/fixtures/generic/date-before-midnight.out解析结果为见 tests/fixtures/generic/date-before-midnight.json{ year: 2021, month: Mar, month_num: 3, day: 24, weekday: Wed, weekday_num: 3, hour: 11, hour_24: 23, minute: 54, second: 47, period: PM, timezone: UTC, utc_offset: 0000, day_of_year: 83, week_of_year: 12, iso: 2021-03-24T23:54:4700:00, epoch: 1616655287, epoch_utc: 1616630087, timezone_aware: true }午夜后AM → 24 小时制输入Wed Mar 24 12:54:47 AM UTC 2021见 tests/fixtures/generic/date-after-midnight.out结果为见 tests/fixtures/generic/date-after-midnight.json{ year: 2021, month: Mar, month_num: 3, day: 24, weekday: Wed, weekday_num: 3, hour: 12, hour_24: 0, minute: 54, second: 47, period: AM, timezone: UTC, utc_offset: 0000, day_of_year: 83, week_of_year: 12, iso: 2021-03-24T00:54:4700:00, epoch: 1616572487, epoch_utc: 1616547287, timezone_aware: true }注意这里的关键换算11:54:47 PM→hour_24: 23而12:54:47 AM→hour_24: 0零点。这两个边界用例在 tests/test_date.py 中分别被test_date_before_midnight与test_date_after_midnight覆盖用于验证 24 小时制换算在午夜前后的正确性。示例四12 小时制时钟的 AM / PMUbuntu 20.04 环境在LANGen_US.UTF-8使用 12 小时制的系统上输入Tue Jan 5 01:02:04 AM UTC 2021见 tests/fixtures/ubuntu-20.04/date.out结果为见 tests/fixtures/ubuntu-20.04/date.json{ year: 2021, month: Jan, month_num: 1, day: 5, weekday: Tue, weekday_num: 2, hour: 1, hour_24: 1, minute: 2, second: 4, period: AM, timezone: UTC, utc_offset: 0000, day_of_year: 5, week_of_year: 1, iso: 2021-01-05T01:02:0400:00, epoch: 1609837324, epoch_utc: 1609808524, timezone_aware: true }对应的 PM 场景输入为Tue Mar 23 08:45:29 PM UTC 2021见 tests/fixtures/ubuntu-20.04/date2.outhour为 8、hour_24为 20见 tests/fixtures/ubuntu-20.04/date2.json。这两个用例在 tests/test_date.py 中分别由test_date_am_ubuntu_20_04与test_date_pm_ubuntu_20_04覆盖。parse() 函数签名与参数parse()是解析器的入口函数见 jc/parsers/date.pydef parse(data, rawFalse, quietFalse)参数说明datastring要解析的文本数据即date命令的输出rawboolean为True时返回未处理的原始结构化数据为False默认时返回经_process()处理后的数据quietboolean为True时抑制警告消息例如在不兼容平台上运行时的提示。返回值为字典包含原始或处理后的结构化数据。解析流程源码解析从 jc/parsers/date.py 可以梳理出完整的处理链路兼容性检查调用jc.utils.compatibility()对照info.compatible [linux, darwin, freebsd]检查当前平台输入类型检查调用jc.utils.input_type_check()校验data为字符串或字节流空数据检查调用jc.utils.has_data()数据为空时直接返回空字典{}时区识别维护一个庞大的时区缩写表涵盖PDT、UTC、CET、JST等以及UTC0800、UTC-0500这类数字偏移写法逐个分词匹配输入字符串中的时区标记时间戳转换调用jc.utils.timestamp()定义于 jc/utils.py并通过format_hint(1000, 6000, 7000)提示优先尝试对应格式timestamp对象提供naive与utc两个属性分别对应本地时间戳与 UTC 时间戳字段组装基于datetime.fromtimestamp()生成的datetime对象用strftime格式化出月份缩写%b、星期缩写%a、12 小时制小时%I、AM/PM%p、UTC 偏移%z、一年中第几天%j、一年中第几周%W等字段并用isoformat()生成iso字段最终处理_process()目前不进行额外加工直接返回原始结构化数据见 jc/parsers/date.py。一个值得注意的实现细节是utc_offset字段由dt.strftime(%z)生成or None兜底——当 datetime 对象本身不含时区信息即非 UTC 输入时%z返回空字符串进而被转换为null这正是 Schema 中“timezone 不是 UTC 时 utc_offset 为 null”的底层实现。时区识别机制解析器并不依赖固定位置的时区字段而是“在整个字符串中查找时区标记”。源码中的实现为见 jc/parsers/date.py先去除输入中的括号再按空白分词逐词与内置的时区缩写表比对命中即认定为时区。因此无论date输出中的时区位于末尾如Mon Aug 3 09:12:51 PDT 2020还是位于中间如Wed Mar 24 11:54:47 PM UTC 2021都能被正确识别。时区缩写表覆盖了全球主要时区缩写如EST/EDT、CET/CEST、JST、IST、AEST等以及UTC±HHMM这类直接带偏移的写法。需要留意的是时区识别用于timezone字段而epoch_utc的可用性仍取决于时区是否为 UTC——非 UTC 时区如 PDT即使被识别出来epoch_utc依然为null、timezone_aware依然为false。与 --slurp 搭配使用date解析器标记为slurpable见 jc/parsers/date.py可以配合 jc 的--slurp命令行选项使用将多行输入聚合后一次性解析。该能力同样记录在 docs/parsers/date.md 中。关于 slurp 模式的更多用法可参阅 jc 流式处理相关文档 docs/streaming.md。实用技巧与 jq 联动将date输出转为 JSON 后即可无缝接入jq做进一步筛选。例如# 查看当前时间的 24 小时制小时 $ date | jc --date | jq .hour_24 # 查看当前是否处于 UTC 时区感知状态 $ date | jc --date | jq .timezone_aware # 同时输出 ISO 时间戳与 epoch $ date | jc --date | jq {iso, epoch}适用前提与限制平台兼容性该解析器官方支持linux、darwin、freebsd三个平台在不兼容平台上运行时会输出兼容性警告quietTrue可抑制时区与 epoch 的关系epoch是基于运行 jc 的系统本地时间计算的朴素时间戳只有输入数据的 timezone 为 UTC 时epoch_utc才有值此时所有字段含iso、hour_24等均基于 UTC 生成解析依赖date输出格式解析器通过内置时间格式库与format_hint提示识别date命令的常见输出格式因此建议在标准环境下使用非标准格式如自定义date输出模板可能需要先规整输出再解析。参考资料解析器官方文档docs/parsers/date.md解析器源码jc/parsers/date.py单元测试tests/test_date.py测试输入与预期输出tests/fixtures/generic/date.out、tests/fixtures/generic/date.json、tests/fixtures/generic/date-before-midnight.out、tests/fixtures/generic/date-after-midnight.out、tests/fixtures/ubuntu-20.04/date.out、tests/fixtures/ubuntu-20.04/date2.out时间戳工具类jc/utils.py赞分享开发工具【免费下载链接】jcCLI tool and python library that converts the output of popular command-line tools, file-types, and common strings to JSON, YAML, or Dictionaries. This allows piping of output to tools like jq and simplifying automation scripts.项目地址https://gitcode.com/gh_mirrors/jc/jc点击查看免费下载相关推荐fastfetch 内置的 Shell 补全脚本怎么用为 bash/zsh/fish 启用 tab 补全fastfetch 内置的 Shell 补全脚本怎么用为 bash/zsh/fish 启用 tab 补全 fastfetch 仓库自带三份 shell 补全脚开发工具jc 的 file 命令解析器将 file * 输出转换为结构化 JSONjc 的 file 命令解析器将 file 输出转换为结构化 JSON 导读 file 命令是 Linux / macOS 系统上最常用的文件类型探测工具但开发工具jc 解析器实战指南用 jc --history 将 shell history 命令输出转换为 JSONjc 解析器实战指南用 jc history 将 shell history 命令输出转换为 JSON 本指南以 jc 开源项目中的 history 解析器文开发工具上一篇Traefik 对接 SPIFFE基于 Workload API 的 X.509-SVID 安全后端通信配置指南下一篇dromara/easy-query Kubernetes云原生编排的集成方案创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表