1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-07-13 01:20:24 +02:00

finish date parser

This commit is contained in:
Kelly Brazil
2020-08-03 09:26:37 -07:00
parent f8c4948a09
commit 88b9d5068c
6 changed files with 74 additions and 15 deletions

View File

@ -377,6 +377,24 @@ cat homes.csv | jc --csv -p
}
]
```
### date
```bash
date | jc --date -p # or: jc -p date
```
```json
{
"year": 2020,
"month_num": 7,
"day": 31,
"hour": 16,
"minute": 48,
"second": 11,
"month": "Jul",
"weekday": "Fri",
"weekday_num": 6,
"timezone": "PDT"
}
```
### df
```bash
df | jc --df -p # or: jc -p df

View File

@ -119,6 +119,7 @@ The JSON output can be compact (default) or pretty formatted with the `-p` optio
- `--crontab` enables the `crontab` command and file parser
- `--crontab-u` enables the `crontab` file parser with user support
- `--csv` enables the `CSV` file parser
- `--date` enables the `date` command parser
- `--df` enables the `df` command parser
- `--dig` enables the `dig` command parser
- `--dmidecode` enables the `dmidecode` command parser

View File

@ -15,12 +15,14 @@ Examples:
$ date | jc --date -p
{
"year": 2020,
"month": "Jul",
"month_num": 7,
"day": 31,
"hour": 16,
"minute": 48,
"second": 11,
"month": "Jul",
"weekday": "Fri",
"hour": 14,
"minute": 35,
"second": 55,
"weekday_num": 6,
"timezone": "PDT"
}
@ -30,9 +32,9 @@ Examples:
"month": "Jul",
"day": "31",
"weekday": "Fri",
"hour": "14",
"minute": "36",
"second": "14",
"hour": "16",
"minute": "50",
"second": "01",
"timezone": "PDT"
}
@ -59,14 +61,16 @@ Returns:
Dictionary. Structured data with the following schema:
{
"year": integer,
"month": string,
"day": integer,
"weekday": string,
"hour": integer,
"minute": integer,
"second": integer,
"timezone": string
"year": integer,
"month_num": integer,
"day": integer,
"hour": integer,
"minute": integer,
"second": integer,
"month": string,
"weekday": string,
"weekday_num": integer,
"timezone": string
}

1
tests/fixtures/generic/date.json vendored Normal file
View File

@ -0,0 +1 @@
{"year": 2020, "month_num": 8, "day": 3, "hour": 9, "minute": 12, "second": 51, "month": "Aug", "weekday": "Mon", "weekday_num": 2, "timezone": "PDT"}

1
tests/fixtures/generic/date.out vendored Normal file
View File

@ -0,0 +1 @@
Mon Aug 3 09:12:51 PDT 2020

34
tests/test_date.py Normal file
View File

@ -0,0 +1,34 @@
import os
import json
import unittest
import jc.parsers.date
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
class MyTests(unittest.TestCase):
def setUp(self):
# input
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/date.out'), 'r', encoding='utf-8') as f:
self.generic_date = f.read()
# output
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/date.json'), 'r', encoding='utf-8') as f:
self.generic_date_json = json.loads(f.read())
def test_date_nodata(self):
"""
Test 'date' with no data
"""
self.assertEqual(jc.parsers.date.parse('', quiet=True), {})
def test_date(self):
"""
Test 'date'
"""
self.assertEqual(jc.parsers.date.parse(self.generic_date, quiet=True), self.generic_date_json)
if __name__ == '__main__':
unittest.main()