|
| 1 | +import datetime |
1 | 2 | import click
|
2 | 3 | import logging
|
3 | 4 | import yaml
|
@@ -644,13 +645,11 @@ def infer_range(slot: dict, vals: set, types: dict, coerce=True) -> str:
|
644 | 645 | return 'boolean'
|
645 | 646 | if all(isfloat(v) for v in nn_vals):
|
646 | 647 | return 'float'
|
647 |
| - if all(is_date(v) for v in nn_vals): |
648 |
| - if all( |
649 |
| - not hasattr(parse(str(v)), 'hour') or |
650 |
| - (parse(str(v)).hour == 0 and parse(str(v)).minute == 0 and parse(str(v)).second == 0) |
651 |
| - for v in nn_vals |
652 |
| - ): # Check if values are just dates without time |
653 |
| - return 'date' |
| 648 | + parsed_datetimes = [is_date_or_datetime(v) for v in nn_vals] |
| 649 | + if all(pd == 'date' for pd in parsed_datetimes): |
| 650 | + return 'date' |
| 651 | + if all(pd in ('date', 'datetime') for pd in parsed_datetimes): |
| 652 | + # This selects datetime when values are mixed which may fail validation |
654 | 653 | return 'datetime'
|
655 | 654 | if is_all_measurement(nn_vals):
|
656 | 655 | return 'measurement'
|
@@ -697,6 +696,24 @@ def is_date(string, fuzzy=False):
|
697 | 696 | return False
|
698 | 697 |
|
699 | 698 |
|
| 699 | +def is_date_or_datetime(string, fuzzy=False): |
| 700 | + """ |
| 701 | + Return whether the string can be interpreted as a date or datetime. |
| 702 | +
|
| 703 | + :param string: str, string to check for date |
| 704 | + :param fuzzy: bool, ignore unknown tokens in string if True |
| 705 | + """ |
| 706 | + try: |
| 707 | + dt = parse(string, fuzzy=fuzzy) |
| 708 | + if dt.hour == 0 and dt.minute == 0 and dt.second == 0: |
| 709 | + return 'date' |
| 710 | + return 'datetime' |
| 711 | + except Exception: |
| 712 | + # https://stackoverflow.com/questions/4990718/how-can-i-write-a-try-except-block-that-catches-all-exceptions |
| 713 | + # we don't know all the different parse exceptions, we assume any error means this is not a date |
| 714 | + return False |
| 715 | + |
| 716 | + |
700 | 717 | @dataclass
|
701 | 718 | class Hit:
|
702 | 719 | term_id: str
|
|
0 commit comments