Skip to content

Commit 43c5e79

Browse files
authored
allow partial time sections of the timestamp argument (#43)
* allow partial time sections of the timestamp argument * update test * update error message to reflect flexible time format * update readme
1 parent 51b3e19 commit 43c5e79

3 files changed

Lines changed: 39 additions & 20 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ To specify a begin/end time, you can pass a date or a date w/ time as a string:
7878
code42 security-data print -b '2020-02-02 12:51:00'
7979
```
8080

81+
```bash
82+
code42 security-data print -b '2020-02-02 12:30'
83+
```
84+
85+
```bash
86+
code42 security-data print -b '2020-02-02 12'
87+
```
88+
8189
```bash
8290
code42 security-data print -b 2020-02-02
8391
```

src/code42cli/cmds/securitydata/date_helper.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from py42.sdk.queries.fileevents.filters.event_filter import EventTimestamp
66

77
_MAX_LOOK_BACK_DAYS = 90
8-
_FORMAT_VALUE_ERROR_MESSAGE = u"input must be a date in YYYY-MM-DD or YYYY-MM-DD HH:MM:SS format, or a short value in days, hours, or minutes (e.g. 30d, 24h, 15m)"
8+
_FORMAT_VALUE_ERROR_MESSAGE = u"input must be a date/time string (e.g. 'YYYY-MM-DD', 'YY-MM-DD HH:MM', 'YY-MM-DD HH:MM:SS'), or a short value in days, hours, or minutes (e.g. 30d, 24h, 15m)"
99

1010

1111
class DateArgumentException(Exception):
@@ -91,7 +91,10 @@ def _parse_max_timestamp(end_date_str):
9191

9292
def _get_dt_from_date_time_pair(date, time):
9393
date_format = u"%Y-%m-%d %H:%M:%S"
94-
time = time or u"00:00:00"
94+
if time:
95+
time = u"{}:{}:{}".format(*time.split(":") + [u"00", u"00"])
96+
else:
97+
time = u"00:00:00"
9598
date_string = u"{} {}".format(date, time)
9699
try:
97100
dt = datetime.strptime(date_string, date_format)

tests/cmds/securitydata/test_date_helper.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ def test_create_event_timestamp_filter_when_given_both_begin_and_end_builds_expe
6565
assert actual_end == expected_end
6666

6767

68+
def test_create_event_timestamp_filter_when_given_short_time_args_builds_expected_query():
69+
begin_date = "{} 10".format(begin_date_str)
70+
end_date = "{} 12:37".format(end_date_str)
71+
ts_range = create_event_timestamp_filter(begin_date, end_date)
72+
actual_begin = get_filter_value_from_json(ts_range, filter_index=0)
73+
actual_end = get_filter_value_from_json(ts_range, filter_index=1)
74+
expected_begin = "{0}T10:00:00.000Z".format(begin_date_str)
75+
expected_end = "{0}T12:37:00.000Z".format(end_date_str)
76+
assert actual_begin == expected_begin
77+
assert actual_end == expected_end
78+
79+
6880
def test_create_event_timestamp_filter_when_begin_more_than_ninety_days_back_causes_value_error():
6981
begin_date_str = get_test_date_str(days_ago=91)
7082
with pytest.raises(DateArgumentException):
@@ -90,22 +102,18 @@ def test_create_event_timestamp_filter_when_args_are_magic_days_builds_expected_
90102
assert actual_end == expected_end
91103

92104

93-
def test_create_event_timestamp_filter_when_given_improperly_formatted_arg_raises_value_error():
94-
missing_seconds = "{} {}".format(get_test_date_str(days_ago=5), "12:00")
95-
month_first_date = "01-01-2020"
96-
time_typo = "{} {}".format(get_test_date_str(days_ago=5), "b20:30:00")
97-
bad_magic = "2months"
98-
bad_magic_2 = "100s"
99-
bad_magic_3 = "10 d"
100-
with pytest.raises(DateArgumentException):
101-
create_event_timestamp_filter(missing_seconds)
102-
with pytest.raises(DateArgumentException):
103-
create_event_timestamp_filter(month_first_date)
104-
with pytest.raises(DateArgumentException):
105-
create_event_timestamp_filter(time_typo)
106-
with pytest.raises(DateArgumentException):
107-
create_event_timestamp_filter(bad_magic)
108-
with pytest.raises(DateArgumentException):
109-
create_event_timestamp_filter(bad_magic_2)
105+
@pytest.mark.parametrize(
106+
"bad_date_param",
107+
[
108+
"01-01-2020",
109+
"{} {}".format(get_test_date_str(days_ago=5), "b20:30:00"),
110+
"2months",
111+
"100s",
112+
"10 d",
113+
],
114+
)
115+
def test_create_event_timestamp_filter_when_given_improperly_formatted_arg_raises_value_error(
116+
bad_date_param,
117+
):
110118
with pytest.raises(DateArgumentException):
111-
create_event_timestamp_filter(bad_magic_3)
119+
create_event_timestamp_filter(bad_date_param)

0 commit comments

Comments
 (0)