From 3947e424e7ad1d7e31e955869cbcfce086a1b923 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sun, 9 Oct 2022 11:00:02 -0700 Subject: [PATCH] rename iso-datetime parser to datetime-iso --- CHANGELOG | 4 +- README.md | 2 +- completions/jc_bash_completion.sh | 2 +- completions/jc_zsh_completion.sh | 5 +- docs/parsers/iso_datetime.md | 64 +----- jc/lib.py | 1 + jc/parsers/datetime_iso.py | 313 ++++++++++++++++++++++++++++++ jc/parsers/iso_datetime.py | 294 ++-------------------------- man/jc.1 | 9 +- 9 files changed, 347 insertions(+), 347 deletions(-) create mode 100644 jc/parsers/datetime_iso.py diff --git a/CHANGELOG b/CHANGELOG index 3ca51671..ed6a0418 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,7 +1,9 @@ jc changelog 20221004 v1.22.1 -- fix proc-pid-stat parser for command names with spaces and newlines +- fix `proc-pid-stat` parser for command names with spaces and newlines +- rename `iso-datetime` parser to `datetime-iso`. A deprecation warning will + display until `iso-datetime` is removed in a future version. - refactor cli module - optimize timestamps diff --git a/README.md b/README.md index 724178be..2e02ce0b 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,7 @@ option. | ` --csv` | CSV file parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/csv) | | ` --csv-s` | CSV file streaming parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/csv_s) | | ` --date` | `date` command parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/date) | +| ` --datetime-iso` | ISO 8601 Datetime string parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/datetime_iso) | | ` --df` | `df` command parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/df) | | ` --dig` | `dig` command parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/dig) | | ` --dir` | `dir` command parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/dir) | @@ -199,7 +200,6 @@ option. | ` --iostat-s` | `iostat` command streaming parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/iostat_s) | | ` --ip-address` | IPv4 and IPv6 Address string parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/ip_address) | | ` --iptables` | `iptables` command parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/iptables) | -| ` --iso-datetime` | ISO 8601 Datetime string parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/iso_datetime) | | ` --iw-scan` | `iw dev [device] scan` command parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/iw_scan) | | ` --jar-manifest` | Java MANIFEST.MF file parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/jar_manifest) | | ` --jobs` | `jobs` command parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/jobs) | diff --git a/completions/jc_bash_completion.sh b/completions/jc_bash_completion.sh index 8bf8bea5..56134694 100644 --- a/completions/jc_bash_completion.sh +++ b/completions/jc_bash_completion.sh @@ -4,7 +4,7 @@ _jc() jc_about_options jc_about_mod_options jc_help_options jc_special_options jc_commands=(acpi airport arp blkid chage cksum crontab date df dig dmidecode dpkg du env file finger free git gpg hciconfig id ifconfig iostat iptables iw jobs last lastb ls lsblk lsmod lsof lspci lsusb md5 md5sum mdadm mount mpstat netstat nmcli ntpq pidstat ping ping6 pip pip3 postconf printenv ps route rpm rsync sfdisk sha1sum sha224sum sha256sum sha384sum sha512sum shasum ss stat sum sysctl systemctl systeminfo timedatectl top tracepath tracepath6 traceroute traceroute6 udevadm ufw uname update-alternatives upower uptime vdir vmstat w wc who xrandr zipinfo) - jc_parsers=(--acpi --airport --airport-s --arp --asciitable --asciitable-m --blkid --cef --cef-s --chage --cksum --crontab --crontab-u --csv --csv-s --date --df --dig --dir --dmidecode --dpkg-l --du --email-address --env --file --finger --free --fstab --git-log --git-log-s --gpg --group --gshadow --hash --hashsum --hciconfig --history --hosts --id --ifconfig --ini --iostat --iostat-s --ip-address --iptables --iso-datetime --iw-scan --jar-manifest --jobs --jwt --kv --last --ls --ls-s --lsblk --lsmod --lsof --lspci --lsusb --m3u --mdadm --mount --mpstat --mpstat-s --netstat --nmcli --ntpq --passwd --pci-ids --pidstat --pidstat-s --ping --ping-s --pip-list --pip-show --plist --postconf --proc --proc-buddyinfo --proc-consoles --proc-cpuinfo --proc-crypto --proc-devices --proc-diskstats --proc-filesystems --proc-interrupts --proc-iomem --proc-ioports --proc-loadavg --proc-locks --proc-meminfo --proc-modules --proc-mtrr --proc-pagetypeinfo --proc-partitions --proc-slabinfo --proc-softirqs --proc-stat --proc-swaps --proc-uptime --proc-version --proc-vmallocinfo --proc-vmstat --proc-zoneinfo --proc-driver-rtc --proc-net-arp --proc-net-dev --proc-net-dev-mcast --proc-net-if-inet6 --proc-net-igmp --proc-net-igmp6 --proc-net-ipv6-route --proc-net-netlink --proc-net-netstat --proc-net-packet --proc-net-protocols --proc-net-route --proc-net-unix --proc-pid-fdinfo --proc-pid-io --proc-pid-maps --proc-pid-mountinfo --proc-pid-numa-maps --proc-pid-smaps --proc-pid-stat --proc-pid-statm --proc-pid-status --ps --route --rpm-qi --rsync --rsync-s --sfdisk --shadow --ss --stat --stat-s --sysctl --syslog --syslog-s --syslog-bsd --syslog-bsd-s --systemctl --systemctl-lj --systemctl-ls --systemctl-luf --systeminfo --time --timedatectl --timestamp --top --top-s --tracepath --traceroute --udevadm --ufw --ufw-appinfo --uname --update-alt-gs --update-alt-q --upower --uptime --url --vmstat --vmstat-s --w --wc --who --x509-cert --xml --xrandr --yaml --zipinfo) + jc_parsers=(--acpi --airport --airport-s --arp --asciitable --asciitable-m --blkid --cef --cef-s --chage --cksum --crontab --crontab-u --csv --csv-s --date --datetime-iso --df --dig --dir --dmidecode --dpkg-l --du --email-address --env --file --finger --free --fstab --git-log --git-log-s --gpg --group --gshadow --hash --hashsum --hciconfig --history --hosts --id --ifconfig --ini --iostat --iostat-s --ip-address --iptables --iso-datetime --iw-scan --jar-manifest --jobs --jwt --kv --last --ls --ls-s --lsblk --lsmod --lsof --lspci --lsusb --m3u --mdadm --mount --mpstat --mpstat-s --netstat --nmcli --ntpq --passwd --pci-ids --pidstat --pidstat-s --ping --ping-s --pip-list --pip-show --plist --postconf --proc --proc-buddyinfo --proc-consoles --proc-cpuinfo --proc-crypto --proc-devices --proc-diskstats --proc-filesystems --proc-interrupts --proc-iomem --proc-ioports --proc-loadavg --proc-locks --proc-meminfo --proc-modules --proc-mtrr --proc-pagetypeinfo --proc-partitions --proc-slabinfo --proc-softirqs --proc-stat --proc-swaps --proc-uptime --proc-version --proc-vmallocinfo --proc-vmstat --proc-zoneinfo --proc-driver-rtc --proc-net-arp --proc-net-dev --proc-net-dev-mcast --proc-net-if-inet6 --proc-net-igmp --proc-net-igmp6 --proc-net-ipv6-route --proc-net-netlink --proc-net-netstat --proc-net-packet --proc-net-protocols --proc-net-route --proc-net-unix --proc-pid-fdinfo --proc-pid-io --proc-pid-maps --proc-pid-mountinfo --proc-pid-numa-maps --proc-pid-smaps --proc-pid-stat --proc-pid-statm --proc-pid-status --ps --route --rpm-qi --rsync --rsync-s --sfdisk --shadow --ss --stat --stat-s --sysctl --syslog --syslog-s --syslog-bsd --syslog-bsd-s --systemctl --systemctl-lj --systemctl-ls --systemctl-luf --systeminfo --time --timedatectl --timestamp --top --top-s --tracepath --traceroute --udevadm --ufw --ufw-appinfo --uname --update-alt-gs --update-alt-q --upower --uptime --url --vmstat --vmstat-s --w --wc --who --x509-cert --xml --xrandr --yaml --zipinfo) jc_options=(--force-color -C --debug -d --monochrome -m --meta-out -M --pretty -p --quiet -q --raw -r --unbuffer -u --yaml-out -y) jc_about_options=(--about -a) jc_about_mod_options=(--pretty -p --yaml-out -y --monochrome -m --force-color -C) diff --git a/completions/jc_zsh_completion.sh b/completions/jc_zsh_completion.sh index 11cbcfa9..83ab332c 100644 --- a/completions/jc_zsh_completion.sh +++ b/completions/jc_zsh_completion.sh @@ -97,7 +97,7 @@ _jc() { 'xrandr:run "xrandr" command with magic syntax.' 'zipinfo:run "zipinfo" command with magic syntax.' ) - jc_parsers=(--acpi --airport --airport-s --arp --asciitable --asciitable-m --blkid --cef --cef-s --chage --cksum --crontab --crontab-u --csv --csv-s --date --df --dig --dir --dmidecode --dpkg-l --du --email-address --env --file --finger --free --fstab --git-log --git-log-s --gpg --group --gshadow --hash --hashsum --hciconfig --history --hosts --id --ifconfig --ini --iostat --iostat-s --ip-address --iptables --iso-datetime --iw-scan --jar-manifest --jobs --jwt --kv --last --ls --ls-s --lsblk --lsmod --lsof --lspci --lsusb --m3u --mdadm --mount --mpstat --mpstat-s --netstat --nmcli --ntpq --passwd --pci-ids --pidstat --pidstat-s --ping --ping-s --pip-list --pip-show --plist --postconf --proc --proc-buddyinfo --proc-consoles --proc-cpuinfo --proc-crypto --proc-devices --proc-diskstats --proc-filesystems --proc-interrupts --proc-iomem --proc-ioports --proc-loadavg --proc-locks --proc-meminfo --proc-modules --proc-mtrr --proc-pagetypeinfo --proc-partitions --proc-slabinfo --proc-softirqs --proc-stat --proc-swaps --proc-uptime --proc-version --proc-vmallocinfo --proc-vmstat --proc-zoneinfo --proc-driver-rtc --proc-net-arp --proc-net-dev --proc-net-dev-mcast --proc-net-if-inet6 --proc-net-igmp --proc-net-igmp6 --proc-net-ipv6-route --proc-net-netlink --proc-net-netstat --proc-net-packet --proc-net-protocols --proc-net-route --proc-net-unix --proc-pid-fdinfo --proc-pid-io --proc-pid-maps --proc-pid-mountinfo --proc-pid-numa-maps --proc-pid-smaps --proc-pid-stat --proc-pid-statm --proc-pid-status --ps --route --rpm-qi --rsync --rsync-s --sfdisk --shadow --ss --stat --stat-s --sysctl --syslog --syslog-s --syslog-bsd --syslog-bsd-s --systemctl --systemctl-lj --systemctl-ls --systemctl-luf --systeminfo --time --timedatectl --timestamp --top --top-s --tracepath --traceroute --udevadm --ufw --ufw-appinfo --uname --update-alt-gs --update-alt-q --upower --uptime --url --vmstat --vmstat-s --w --wc --who --x509-cert --xml --xrandr --yaml --zipinfo) + jc_parsers=(--acpi --airport --airport-s --arp --asciitable --asciitable-m --blkid --cef --cef-s --chage --cksum --crontab --crontab-u --csv --csv-s --date --datetime-iso --df --dig --dir --dmidecode --dpkg-l --du --email-address --env --file --finger --free --fstab --git-log --git-log-s --gpg --group --gshadow --hash --hashsum --hciconfig --history --hosts --id --ifconfig --ini --iostat --iostat-s --ip-address --iptables --iso-datetime --iw-scan --jar-manifest --jobs --jwt --kv --last --ls --ls-s --lsblk --lsmod --lsof --lspci --lsusb --m3u --mdadm --mount --mpstat --mpstat-s --netstat --nmcli --ntpq --passwd --pci-ids --pidstat --pidstat-s --ping --ping-s --pip-list --pip-show --plist --postconf --proc --proc-buddyinfo --proc-consoles --proc-cpuinfo --proc-crypto --proc-devices --proc-diskstats --proc-filesystems --proc-interrupts --proc-iomem --proc-ioports --proc-loadavg --proc-locks --proc-meminfo --proc-modules --proc-mtrr --proc-pagetypeinfo --proc-partitions --proc-slabinfo --proc-softirqs --proc-stat --proc-swaps --proc-uptime --proc-version --proc-vmallocinfo --proc-vmstat --proc-zoneinfo --proc-driver-rtc --proc-net-arp --proc-net-dev --proc-net-dev-mcast --proc-net-if-inet6 --proc-net-igmp --proc-net-igmp6 --proc-net-ipv6-route --proc-net-netlink --proc-net-netstat --proc-net-packet --proc-net-protocols --proc-net-route --proc-net-unix --proc-pid-fdinfo --proc-pid-io --proc-pid-maps --proc-pid-mountinfo --proc-pid-numa-maps --proc-pid-smaps --proc-pid-stat --proc-pid-statm --proc-pid-status --ps --route --rpm-qi --rsync --rsync-s --sfdisk --shadow --ss --stat --stat-s --sysctl --syslog --syslog-s --syslog-bsd --syslog-bsd-s --systemctl --systemctl-lj --systemctl-ls --systemctl-luf --systeminfo --time --timedatectl --timestamp --top --top-s --tracepath --traceroute --udevadm --ufw --ufw-appinfo --uname --update-alt-gs --update-alt-q --upower --uptime --url --vmstat --vmstat-s --w --wc --who --x509-cert --xml --xrandr --yaml --zipinfo) jc_parsers_describe=( '--acpi:`acpi` command parser' '--airport:`airport -I` command parser' @@ -115,6 +115,7 @@ _jc() { '--csv:CSV file parser' '--csv-s:CSV file streaming parser' '--date:`date` command parser' + '--datetime-iso:ISO 8601 Datetime string parser' '--df:`df` command parser' '--dig:`dig` command parser' '--dir:`dir` command parser' @@ -144,7 +145,7 @@ _jc() { '--iostat-s:`iostat` command streaming parser' '--ip-address:IPv4 and IPv6 Address string parser' '--iptables:`iptables` command parser' - '--iso-datetime:ISO 8601 Datetime string parser' + '--iso-datetime:Deprecated - please use datetime-iso' '--iw-scan:`iw dev [device] scan` command parser' '--jar-manifest:Java MANIFEST.MF file parser' '--jobs:`jobs` command parser' diff --git a/docs/parsers/iso_datetime.md b/docs/parsers/iso_datetime.md index fc72a29d..d7ede304 100644 --- a/docs/parsers/iso_datetime.md +++ b/docs/parsers/iso_datetime.md @@ -5,65 +5,10 @@ jc - JSON Convert ISO 8601 Datetime string parser -This parser supports standard ISO 8601 strings that include both date and -time. If no timezone or offset information is available in the sring, then -UTC timezone is used. +This parser has been renamed to datetime-iso (cli) or datetime_iso (module). -Usage (cli): - - $ echo "2022-07-20T14:52:45Z" | jc --iso-datetime - -Usage (module): - - import jc - result = jc.parse('iso_datetime', iso_8601_string) - -Schema: - - { - "year": integer, - "month": string, - "month_num": integer, - "day": integer, - "weekday": string, - "weekday_num": integer, - "hour": integer, - "hour_24": integer, - "minute": integer, - "second": integer, - "microsecond": integer, - "period": string, - "utc_offset": string, - "day_of_year": integer, - "week_of_year": integer, - "iso": string, - "timestamp": integer # [0] - } - - [0] timezone aware UNIX timestamp expressed in UTC - -Examples: - - $ echo "2022-07-20T14:52:45Z" | jc --iso-datetime -p - { - "year": 2022, - "month": "Jul", - "month_num": 7, - "day": 20, - "weekday": "Wed", - "weekday_num": 3, - "hour": 2, - "hour_24": 14, - "minute": 52, - "second": 45, - "microsecond": 0, - "period": "PM", - "utc_offset": "+0000", - "day_of_year": 201, - "week_of_year": 29, - "iso": "2022-07-20T14:52:45+00:00", - "timestamp": 1658328765 - } +This parser will be removed in a future version, so please start using +the new parser name. @@ -73,7 +18,8 @@ Examples: def parse(data, raw=False, quiet=False) ``` -Main text parsing function +This parser is deprecated and calls datetime_iso. Please use datetime_iso +directly. This parser will be removed in the future. Parameters: diff --git a/jc/lib.py b/jc/lib.py index 656ba665..09a8ea6a 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -25,6 +25,7 @@ parsers = [ 'csv', 'csv-s', 'date', + 'datetime-iso', 'df', 'dig', 'dir', diff --git a/jc/parsers/datetime_iso.py b/jc/parsers/datetime_iso.py new file mode 100644 index 00000000..3b11eb79 --- /dev/null +++ b/jc/parsers/datetime_iso.py @@ -0,0 +1,313 @@ +"""jc - JSON Convert ISO 8601 Datetime string parser + +This parser supports standard ISO 8601 strings that include both date and +time. If no timezone or offset information is available in the sring, then +UTC timezone is used. + +Usage (cli): + + $ echo "2022-07-20T14:52:45Z" | jc --iso-datetime + +Usage (module): + + import jc + result = jc.parse('iso_datetime', iso_8601_string) + +Schema: + + { + "year": integer, + "month": string, + "month_num": integer, + "day": integer, + "weekday": string, + "weekday_num": integer, + "hour": integer, + "hour_24": integer, + "minute": integer, + "second": integer, + "microsecond": integer, + "period": string, + "utc_offset": string, + "day_of_year": integer, + "week_of_year": integer, + "iso": string, + "timestamp": integer # [0] + } + + [0] timezone aware UNIX timestamp expressed in UTC + +Examples: + + $ echo "2022-07-20T14:52:45Z" | jc --iso-datetime -p + { + "year": 2022, + "month": "Jul", + "month_num": 7, + "day": 20, + "weekday": "Wed", + "weekday_num": 3, + "hour": 2, + "hour_24": 14, + "minute": 52, + "second": 45, + "microsecond": 0, + "period": "PM", + "utc_offset": "+0000", + "day_of_year": 201, + "week_of_year": 29, + "iso": "2022-07-20T14:52:45+00:00", + "timestamp": 1658328765 + } +""" +import datetime +import re +import typing +from decimal import Decimal +import jc.utils + + +class info(): + """Provides parser metadata (version, author, etc.)""" + version = '1.0' + description = 'ISO 8601 Datetime string parser' + author = 'Kelly Brazil' + author_email = 'kellyjonbrazil@gmail.com' + details = 'Using the pyiso8601 library from https://github.com/micktwomey/pyiso8601/releases/tag/1.0.2' + compatible = ['linux', 'aix', 'freebsd', 'darwin', 'win32', 'cygwin'] + + +__version__ = info.version + + +#################################################### +""" +pyiso8601 library from https://github.com/micktwomey/pyiso8601/releases/tag/1.0.2 +""" + +""" +Copyright (c) 2007 - 2022 Michael Twomey + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +""" + +"""ISO 8601 date time string parsing +Basic usage: +>>> import iso8601 +>>> iso8601._parse_date("2007-01-25T12:00:00Z") +datetime.datetime(2007, 1, 25, 12, 0, tzinfo=) +>>> +""" + +# __all__ = ["_parse_date", "_ParseError", "UTC", "_FixedOffset"] + +# Adapted from http://delete.me.uk/2005/03/iso8601.html +ISO8601_REGEX = re.compile( + r""" + (?P[0-9]{4}) + ( + ( + (-(?P[0-9]{1,2})) + | + (?P[0-9]{2}) + (?!$) # Don't allow YYYYMM + ) + ( + ( + (-(?P[0-9]{1,2})) + | + (?P[0-9]{2}) + ) + ( + ( + (?P[ T]) + (?P[0-9]{2}) + (:{0,1}(?P[0-9]{2})){0,1} + ( + :{0,1}(?P[0-9]{1,2}) + ([.,](?P[0-9]+)){0,1} + ){0,1} + (?P + Z + | + ( + (?P[-+]) + (?P[0-9]{2}) + :{0,1} + (?P[0-9]{2}){0,1} + ) + ){0,1} + ){0,1} + ) + ){0,1} # YYYY-MM + ){0,1} # YYYY only + $ + """, + re.VERBOSE, +) + + +class _ParseError(ValueError): + """Raised when there is a problem parsing a date string""" + + +UTC = datetime.timezone.utc + + +def _FixedOffset( + offset_hours: float, offset_minutes: float, name: str +) -> datetime.timezone: + return datetime.timezone( + datetime.timedelta(hours=offset_hours, minutes=offset_minutes), name + ) + + +def _parse_timezone( + matches: typing.Dict[str, str], + default_timezone: typing.Optional[datetime.timezone] = UTC, +) -> typing.Optional[datetime.timezone]: + """Parses ISO 8601 time zone specs into tzinfo offsets""" + tz = matches.get("timezone", None) + if tz == "Z": + return UTC + # This isn't strictly correct, but it's common to encounter dates without + # timezones so I'll assume the default (which defaults to UTC). + # Addresses issue 4. + if tz is None: + return default_timezone + sign = matches.get("tz_sign", None) + hours = int(matches.get("tz_hour", 0)) + minutes = int(matches.get("tz_minute", 0)) + description = f"{sign}{hours:02d}:{minutes:02d}" + if sign == "-": + hours = -hours + minutes = -minutes + return _FixedOffset(hours, minutes, description) + + +def _parse_date( + datestring: str, default_timezone: typing.Optional[datetime.timezone] = UTC +) -> datetime.datetime: + """Parses ISO 8601 dates into datetime objects + The timezone is parsed from the date string. However it is quite common to + have dates without a timezone (not strictly correct). In this case the + default timezone specified in default_timezone is used. This is UTC by + default. + :param datestring: The date to parse as a string + :param default_timezone: A datetime tzinfo instance to use when no timezone + is specified in the datestring. If this is set to + None then a naive datetime object is returned. + :returns: A datetime.datetime instance + :raises: _ParseError when there is a problem parsing the date or + constructing the datetime instance. + """ + try: + m = ISO8601_REGEX.match(datestring) + except Exception as e: + raise _ParseError(e) + + if not m: + raise _ParseError(f"Unable to parse date string {datestring!r}") + + # Drop any Nones from the regex matches + # TODO: check if there's a way to omit results in regexes + groups: typing.Dict[str, str] = { + k: v for k, v in m.groupdict().items() if v is not None + } + + try: + return datetime.datetime( + year=int(groups.get("year", 0)), + month=int(groups.get("month", groups.get("monthdash", 1))), + day=int(groups.get("day", groups.get("daydash", 1))), + hour=int(groups.get("hour", 0)), + minute=int(groups.get("minute", 0)), + second=int(groups.get("second", 0)), + microsecond=int( + Decimal(f"0.{groups.get('second_fraction', 0)}") * Decimal("1000000.0") + ), + tzinfo=_parse_timezone(groups, default_timezone=default_timezone), + ) + except Exception as e: + raise _ParseError(e) + +#################################################### + + +def _process(proc_data): + """ + Final processing to conform to the schema. + + Parameters: + + proc_data: (Dictionary) raw structured data to process + + Returns: + + Dictionary. Structured data to conform to the schema. + """ + # no further processing + return proc_data + + +def parse(data, raw=False, quiet=False): + """ + Main text parsing function + + Parameters: + + data: (string) text data to parse + raw: (boolean) unprocessed output if True + quiet: (boolean) suppress warning messages if True + + Returns: + + Dictionary. Raw or processed structured data. + """ + jc.utils.compatibility(__name__, info.compatible, quiet) + jc.utils.input_type_check(data) + + raw_output = {} + + if jc.utils.has_data(data): + + dt = _parse_date(data) + + raw_output = { + 'year': dt.year, + 'month': dt.strftime('%b'), + 'month_num': dt.month, + 'day': dt.day, + 'weekday': dt.strftime('%a'), + 'weekday_num': dt.isoweekday(), + 'hour': int(dt.strftime('%I')), + 'hour_24': dt.hour, + 'minute': dt.minute, + 'second': dt.second, + 'microsecond': dt.microsecond, + 'period': dt.strftime('%p').upper(), + 'utc_offset': dt.strftime('%z') or None, + 'day_of_year': int(dt.strftime('%j')), + 'week_of_year': int(dt.strftime('%W')), + 'iso': dt.isoformat(), + 'timestamp': int(dt.timestamp()) + } + + return raw_output if raw else _process(raw_output) diff --git a/jc/parsers/iso_datetime.py b/jc/parsers/iso_datetime.py index 3b11eb79..23906ae1 100644 --- a/jc/parsers/iso_datetime.py +++ b/jc/parsers/iso_datetime.py @@ -1,275 +1,32 @@ """jc - JSON Convert ISO 8601 Datetime string parser -This parser supports standard ISO 8601 strings that include both date and -time. If no timezone or offset information is available in the sring, then -UTC timezone is used. +This parser has been renamed to datetime-iso (cli) or datetime_iso (module). -Usage (cli): - - $ echo "2022-07-20T14:52:45Z" | jc --iso-datetime - -Usage (module): - - import jc - result = jc.parse('iso_datetime', iso_8601_string) - -Schema: - - { - "year": integer, - "month": string, - "month_num": integer, - "day": integer, - "weekday": string, - "weekday_num": integer, - "hour": integer, - "hour_24": integer, - "minute": integer, - "second": integer, - "microsecond": integer, - "period": string, - "utc_offset": string, - "day_of_year": integer, - "week_of_year": integer, - "iso": string, - "timestamp": integer # [0] - } - - [0] timezone aware UNIX timestamp expressed in UTC - -Examples: - - $ echo "2022-07-20T14:52:45Z" | jc --iso-datetime -p - { - "year": 2022, - "month": "Jul", - "month_num": 7, - "day": 20, - "weekday": "Wed", - "weekday_num": 3, - "hour": 2, - "hour_24": 14, - "minute": 52, - "second": 45, - "microsecond": 0, - "period": "PM", - "utc_offset": "+0000", - "day_of_year": 201, - "week_of_year": 29, - "iso": "2022-07-20T14:52:45+00:00", - "timestamp": 1658328765 - } +This parser will be removed in a future version, so please start using +the new parser name. """ -import datetime -import re -import typing -from decimal import Decimal +from jc.parsers import datetime_iso import jc.utils class info(): """Provides parser metadata (version, author, etc.)""" version = '1.0' - description = 'ISO 8601 Datetime string parser' + description = 'Deprecated - please use datetime-iso' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' - details = 'Using the pyiso8601 library from https://github.com/micktwomey/pyiso8601/releases/tag/1.0.2' + details = 'Deprecated - please use datetime-iso' compatible = ['linux', 'aix', 'freebsd', 'darwin', 'win32', 'cygwin'] + hidden = True __version__ = info.version -#################################################### -""" -pyiso8601 library from https://github.com/micktwomey/pyiso8601/releases/tag/1.0.2 -""" - -""" -Copyright (c) 2007 - 2022 Michael Twomey - -Permission is hereby granted, free of charge, to any person obtaining a -copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be included -in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -""" - -"""ISO 8601 date time string parsing -Basic usage: ->>> import iso8601 ->>> iso8601._parse_date("2007-01-25T12:00:00Z") -datetime.datetime(2007, 1, 25, 12, 0, tzinfo=) ->>> -""" - -# __all__ = ["_parse_date", "_ParseError", "UTC", "_FixedOffset"] - -# Adapted from http://delete.me.uk/2005/03/iso8601.html -ISO8601_REGEX = re.compile( - r""" - (?P[0-9]{4}) - ( - ( - (-(?P[0-9]{1,2})) - | - (?P[0-9]{2}) - (?!$) # Don't allow YYYYMM - ) - ( - ( - (-(?P[0-9]{1,2})) - | - (?P[0-9]{2}) - ) - ( - ( - (?P[ T]) - (?P[0-9]{2}) - (:{0,1}(?P[0-9]{2})){0,1} - ( - :{0,1}(?P[0-9]{1,2}) - ([.,](?P[0-9]+)){0,1} - ){0,1} - (?P - Z - | - ( - (?P[-+]) - (?P[0-9]{2}) - :{0,1} - (?P[0-9]{2}){0,1} - ) - ){0,1} - ){0,1} - ) - ){0,1} # YYYY-MM - ){0,1} # YYYY only - $ - """, - re.VERBOSE, -) - - -class _ParseError(ValueError): - """Raised when there is a problem parsing a date string""" - - -UTC = datetime.timezone.utc - - -def _FixedOffset( - offset_hours: float, offset_minutes: float, name: str -) -> datetime.timezone: - return datetime.timezone( - datetime.timedelta(hours=offset_hours, minutes=offset_minutes), name - ) - - -def _parse_timezone( - matches: typing.Dict[str, str], - default_timezone: typing.Optional[datetime.timezone] = UTC, -) -> typing.Optional[datetime.timezone]: - """Parses ISO 8601 time zone specs into tzinfo offsets""" - tz = matches.get("timezone", None) - if tz == "Z": - return UTC - # This isn't strictly correct, but it's common to encounter dates without - # timezones so I'll assume the default (which defaults to UTC). - # Addresses issue 4. - if tz is None: - return default_timezone - sign = matches.get("tz_sign", None) - hours = int(matches.get("tz_hour", 0)) - minutes = int(matches.get("tz_minute", 0)) - description = f"{sign}{hours:02d}:{minutes:02d}" - if sign == "-": - hours = -hours - minutes = -minutes - return _FixedOffset(hours, minutes, description) - - -def _parse_date( - datestring: str, default_timezone: typing.Optional[datetime.timezone] = UTC -) -> datetime.datetime: - """Parses ISO 8601 dates into datetime objects - The timezone is parsed from the date string. However it is quite common to - have dates without a timezone (not strictly correct). In this case the - default timezone specified in default_timezone is used. This is UTC by - default. - :param datestring: The date to parse as a string - :param default_timezone: A datetime tzinfo instance to use when no timezone - is specified in the datestring. If this is set to - None then a naive datetime object is returned. - :returns: A datetime.datetime instance - :raises: _ParseError when there is a problem parsing the date or - constructing the datetime instance. - """ - try: - m = ISO8601_REGEX.match(datestring) - except Exception as e: - raise _ParseError(e) - - if not m: - raise _ParseError(f"Unable to parse date string {datestring!r}") - - # Drop any Nones from the regex matches - # TODO: check if there's a way to omit results in regexes - groups: typing.Dict[str, str] = { - k: v for k, v in m.groupdict().items() if v is not None - } - - try: - return datetime.datetime( - year=int(groups.get("year", 0)), - month=int(groups.get("month", groups.get("monthdash", 1))), - day=int(groups.get("day", groups.get("daydash", 1))), - hour=int(groups.get("hour", 0)), - minute=int(groups.get("minute", 0)), - second=int(groups.get("second", 0)), - microsecond=int( - Decimal(f"0.{groups.get('second_fraction', 0)}") * Decimal("1000000.0") - ), - tzinfo=_parse_timezone(groups, default_timezone=default_timezone), - ) - except Exception as e: - raise _ParseError(e) - -#################################################### - - -def _process(proc_data): - """ - Final processing to conform to the schema. - - Parameters: - - proc_data: (Dictionary) raw structured data to process - - Returns: - - Dictionary. Structured data to conform to the schema. - """ - # no further processing - return proc_data - - def parse(data, raw=False, quiet=False): """ - Main text parsing function + This parser is deprecated and calls datetime_iso. Please use datetime_iso + directly. This parser will be removed in the future. Parameters: @@ -281,33 +38,8 @@ def parse(data, raw=False, quiet=False): Dictionary. Raw or processed structured data. """ - jc.utils.compatibility(__name__, info.compatible, quiet) - jc.utils.input_type_check(data) + jc.utils.warning_message([ + 'iso-datetime parser is deprecated. Please use datetime-iso instead.' + ]) - raw_output = {} - - if jc.utils.has_data(data): - - dt = _parse_date(data) - - raw_output = { - 'year': dt.year, - 'month': dt.strftime('%b'), - 'month_num': dt.month, - 'day': dt.day, - 'weekday': dt.strftime('%a'), - 'weekday_num': dt.isoweekday(), - 'hour': int(dt.strftime('%I')), - 'hour_24': dt.hour, - 'minute': dt.minute, - 'second': dt.second, - 'microsecond': dt.microsecond, - 'period': dt.strftime('%p').upper(), - 'utc_offset': dt.strftime('%z') or None, - 'day_of_year': int(dt.strftime('%j')), - 'week_of_year': int(dt.strftime('%W')), - 'iso': dt.isoformat(), - 'timestamp': int(dt.timestamp()) - } - - return raw_output if raw else _process(raw_output) + return datetime_iso.parse(data, raw=raw, quiet=quiet) diff --git a/man/jc.1 b/man/jc.1 index d83b9500..f5e91a98 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -1,4 +1,4 @@ -.TH jc 1 2022-10-08 1.22.1 "JSON Convert" +.TH jc 1 2022-10-09 1.22.1 "JSON Convert" .SH NAME \fBjc\fP \- JSON Convert JSONifies the output of many CLI tools, file-types, and strings .SH SYNOPSIS @@ -110,6 +110,11 @@ CSV file streaming parser \fB--date\fP `date` command parser +.TP +.B +\fB--datetime-iso\fP +ISO 8601 Datetime string parser + .TP .B \fB--df\fP @@ -258,7 +263,7 @@ IPv4 and IPv6 Address string parser .TP .B \fB--iso-datetime\fP -ISO 8601 Datetime string parser +Deprecated - please use datetime-iso .TP .B