Skip to content

Commit 4844e8d

Browse files
author
Corne Bester
committed
feat: add psql ms sanitizer function, bump ver
1 parent 3227c82 commit 4844e8d

4 files changed

Lines changed: 91 additions & 4 deletions

File tree

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,18 @@ unix_millis = get_timestamp_unix_millis(timestamp) # create unix timestamp from
2929

3030
## install from location
3131

32-
python -m pip install "C:\Users\corne\Documents\GitHub\python_time_functions\dist\pythom_time_functions-2.0.0-py3-none-any.whl"
32+
python -m pip install "C:\Users\corne\Documents\GitHub\python_time_functions\dist\pythom_time_functions-2.1.0-py3-none-any.whl"
3333

3434

3535
### other
3636

37-
pip3 freeze > requirements.txt
37+
pip3 freeze > requirements.txt
38+
39+
## bad formats I dreamed up for iot/psql. fix with sanitizer fx
40+
41+
>>> time_functions.timestamp_sanitizer_psql(1729755232184)
42+
1729755232.184
43+
>>> time_functions.timestamp_sanitizer_psql(1729765227856.3)
44+
1729765227.856
45+
>>> time_functions.timestamp_sanitizer_psql(1729765228327.051)
46+
1729765228.327

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "pythom_time_functions"
7-
version = "2.0.0"
7+
version = "2.1.0"
88
description = "Common Python functions for time handling"
99
authors = [{name = "Corne Bester", email = "corne.bester@gmail.com"}]
1010
dependencies = [

tests/test_time_functions.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,24 @@ def test_convertions_from_sample32():
157157
'''
158158
bad timeformat
159159
'''
160-
assert time_functions.timestamp_parser_psql_millis(1742742625002.908) is None
160+
assert time_functions.timestamp_parser_psql_millis(1742742625002.908) is None
161+
162+
163+
def test_convertions_from_sample34():
164+
'''
165+
bad timeformat
166+
'''
167+
assert time_functions.timestamp_sanitizer_psql(1729755232184) == 1729755232.184
168+
169+
def test_convertions_from_sample35():
170+
'''
171+
bad timeformat
172+
'''
173+
assert time_functions.timestamp_sanitizer_psql(1729765227856.3) == 1729765227.856
174+
175+
176+
def test_convertions_from_sample36():
177+
'''
178+
bad timeformat
179+
'''
180+
assert time_functions.timestamp_sanitizer_psql(1729765228327.051) == 1729765228.327

time_functions.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,61 @@ def timestamp_parser_psql_millis(timestamp):
236236
unix_with_subseconds = get_timestamp_unix_subseconds_decimal(obj_from_iso)
237237
timestamp = str(unix_with_subseconds)[:14]
238238
return float(timestamp)
239+
240+
241+
def timestamp_sanitizer_psql(input):
242+
'''
243+
Normalize unix timestamps to postgresl friendly float with millisecond decimals. 14 digits
244+
Attemps to fix/sanitize bad timestamps
245+
handle timestamnp as:
246+
unix micros or millis in decimal format
247+
unix millis or micros in unix format
248+
iso8601
249+
'''
250+
try:
251+
if isinstance(input, int):
252+
if len(str(input)) == 10:
253+
logger.debug("Not millsecond precision: %s", input)
254+
return input
255+
elif len(str(input)) == 13:
256+
normalized = str((input/1000))[:14]
257+
logger.debug("Normalizing timestamp for postgresql from %s to %s", input, normalized)
258+
return float(normalized)
259+
elif len(str(input)) == 16:
260+
normalized = str(input/1000000)[:14]
261+
logger.debug("Normalizing timestamp for postgresql from %s to %s", input, normalized)
262+
return float(normalized)
263+
elif isinstance(input, float):
264+
# elif '.' in str(input):
265+
decimal_index = str.index(str(input), '.')
266+
if decimal_index == 13:
267+
normalized = str(input/1000)[:14] # remove microsecond precision
268+
logger.debug("Normalizing timestamp for postgresql from %s to %s", input, normalized)
269+
return float(normalized)
270+
elif decimal_index == 10:
271+
normalized = str(input)[:14] # remove microsecond precision
272+
logger.debug("Normalizing timestamp for postgresql from %s to %s", input, normalized)
273+
return float(normalized)
274+
elif isinstance(input, str):
275+
if len(input) == 20:
276+
obj_from_iso = convert_iso_string_to_datetime_obj(input)
277+
unix_with_subseconds = get_timestamp_unix_subseconds_decimal(obj_from_iso)
278+
normalized = unix_with_subseconds
279+
logger.debug("Normalizing timestamp for postgresql from %s to %s", input, normalized)
280+
return float(normalized)
281+
if len(input) == 24:
282+
obj_from_iso = convert_iso_string_to_datetime_obj(input)
283+
unix_with_subseconds = get_timestamp_unix_subseconds_decimal(obj_from_iso)
284+
normalized = unix_with_subseconds
285+
logger.debug("Normalizing timestamp for postgresql from %s to %s", input, normalized)
286+
return float(normalized)
287+
elif len(input) == 27:
288+
obj_from_iso = convert_iso_string_to_datetime_obj(input)
289+
unix_with_subseconds = get_timestamp_unix_subseconds_decimal(obj_from_iso)
290+
normalized = str(unix_with_subseconds)[:14]
291+
logger.debug("Normalizing timestamp for postgresql from %s to %s", input, normalized)
292+
return float(normalized)
293+
else:
294+
logger.error("Unexcpected timestamp format")
295+
except Exception as msg:
296+
logger.exception(msg)

0 commit comments

Comments
 (0)