Skip to content

Commit 80d7a8a

Browse files
INTEG-946 Changed upper boundary of timestamp to include microseconds to end_date (#21)
* INTEG-946 Changed upper boundary of timestamp to include microseconds when time is not specified * INTEG-946 : Refactor change method name and refactor parse_timestamp * INTEG-946 Added Changelog
1 parent 5a0964c commit 80d7a8a

File tree

4 files changed

+36
-18
lines changed

4 files changed

+36
-18
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
The intended audience of this file is for py42 consumers -- as such, changes that don't affect
99
how a consumer would use the library (e.g. adding unit tests, updating documentation, etc) are not captured here.
1010

11+
###Fixed
12+
13+
- Add milliseconds to end timestamp, to represent end of day with milliseconds precision.
14+
1115
## 0.4.3 - 2020-03-17
1216

1317
### Added

src/code42cli/securitydata/date_helper.py

+30-16
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,54 @@ def create_event_timestamp_filter(begin_date=None, end_date=None):
1515
begin_date: The begin date for the range.
1616
end_date: The end date for the range.
1717
"""
18-
end_date = _get_end_date_with_eod_time_if_needed(end_date)
18+
1919
if begin_date and end_date:
20-
return _create_in_range_filter(begin_date, end_date)
20+
min_timestamp = _parse_min_timestamp(begin_date)
21+
max_timestamp = _parse_max_timestamp(end_date)
22+
return _create_in_range_filter(min_timestamp, max_timestamp)
2123
elif begin_date and not end_date:
22-
return _create_on_or_after_filter(begin_date)
24+
min_timestamp = _parse_min_timestamp(begin_date)
25+
return _create_on_or_after_filter(min_timestamp)
2326
elif end_date and not begin_date:
24-
return _create_on_or_before_filter(end_date)
27+
max_timestamp = _parse_max_timestamp(end_date)
28+
return _create_on_or_before_filter(max_timestamp)
29+
30+
31+
def _parse_max_timestamp(end_date):
32+
if len(end_date) == 1:
33+
end_date = _get_end_date_with_eod_time_if_needed(end_date)
34+
max_time = _parse_timestamp(end_date)
35+
max_time = _add_milliseconds(max_time)
36+
else:
37+
max_time = _parse_timestamp(end_date)
38+
39+
return convert_datetime_to_timestamp(max_time)
40+
41+
42+
def _add_milliseconds(max_time):
43+
return max_time + timedelta(milliseconds=999)
2544

2645

27-
def _create_in_range_filter(begin_date, end_date):
28-
min_timestamp = _parse_min_timestamp(begin_date)
29-
max_timestamp = _parse_timestamp(end_date)
46+
def _create_in_range_filter(min_timestamp, max_timestamp):
3047
_verify_timestamp_order(min_timestamp, max_timestamp)
3148
return EventTimestamp.in_range(min_timestamp, max_timestamp)
3249

3350

34-
def _create_on_or_after_filter(begin_date):
35-
min_timestamp = _parse_min_timestamp(begin_date)
51+
def _create_on_or_after_filter(min_timestamp):
3652
return EventTimestamp.on_or_after(min_timestamp)
3753

3854

39-
def _create_on_or_before_filter(end_date):
40-
max_timestamp = _parse_timestamp(end_date)
55+
def _create_on_or_before_filter(max_timestamp):
4156
return EventTimestamp.on_or_before(max_timestamp)
4257

4358

4459
def _get_end_date_with_eod_time_if_needed(end_date):
45-
if end_date and len(end_date) == 1:
46-
return end_date[0], "23:59:59"
47-
return end_date
60+
return end_date[0], "23:59:59"
4861

4962

5063
def _parse_min_timestamp(begin_date_str):
51-
min_timestamp = _parse_timestamp(begin_date_str)
64+
min_time = _parse_timestamp(begin_date_str)
65+
min_timestamp = convert_datetime_to_timestamp(min_time)
5266
boundary_date = datetime.utcnow() - timedelta(days=_MAX_LOOK_BACK_DAYS)
5367
boundary = convert_datetime_to_timestamp(boundary_date)
5468
if min_timestamp and min_timestamp < boundary:
@@ -68,9 +82,9 @@ def _parse_timestamp(date_and_time):
6882
date_str = _join_date_and_time(date_and_time)
6983
date_format = u"%Y-%m-%d" if len(date_and_time) == 1 else u"%Y-%m-%d %H:%M:%S"
7084
time = datetime.strptime(date_str, date_format)
85+
return time
7186
except ValueError:
7287
raise ValueError(_FORMAT_VALUE_ERROR_MESSAGE)
73-
return convert_datetime_to_timestamp(time)
7488

7589

7690
def _join_date_and_time(date_and_time):

tests/securitydata/test_date_helper.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_create_event_timestamp_filter_when_given_begin_with_time_builds_expecte
3737
def test_create_event_timestamp_filter_when_given_end_builds_expected_query():
3838
ts_range = create_event_timestamp_filter(begin_date_list, end_date_list)
3939
actual = get_filter_value_from_json(ts_range, filter_index=1)
40-
expected = "{0}T23:59:59.000Z".format(end_date_list[0])
40+
expected = "{0}T23:59:59.999Z".format(end_date_list[0])
4141
assert actual == expected
4242

4343

tests/securitydata/test_extraction.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def test_extract_when_given_end_date_uses_expected_query(logger, namespace_with_
213213
namespace_with_begin.end_date = get_test_date_str(days_ago=10)
214214
extraction_module.extract(logger, namespace_with_begin)
215215
actual = get_filter_value_from_json(extractor.extract.call_args[0][0], filter_index=1)
216-
expected = "{0}T23:59:59.000Z".format(namespace_with_begin.end_date)
216+
expected = "{0}T23:59:59.999Z".format(namespace_with_begin.end_date)
217217
assert actual == expected
218218

219219

0 commit comments

Comments
 (0)