|
5 | 5 | from stac_fastapi.types.rfc3339 import rfc3339_str_to_datetime |
6 | 6 |
|
7 | 7 |
|
8 | | -def format_datetime_range(date_str: str) -> str: |
9 | | - """ |
10 | | - Convert a datetime range string into a normalized UTC string for API requests using rfc3339_str_to_datetime. |
| 8 | +# def format_datetime_range(date_str: str) -> str: |
| 9 | +# """ |
| 10 | +# Convert a datetime range string into a normalized UTC string for API requests using rfc3339_str_to_datetime. |
| 11 | + |
| 12 | +# Args: |
| 13 | +# date_str (str): A string containing two datetime values separated by a '/'. |
| 14 | + |
| 15 | +# Returns: |
| 16 | +# str: A string formatted as 'YYYY-MM-DDTHH:MM:SSZ/YYYY-MM-DDTHH:MM:SSZ', with '..' used if any element is None. |
| 17 | +# """ |
| 18 | + |
| 19 | +# def normalize(dt): |
| 20 | +# dt = dt.strip() |
| 21 | +# if not dt or dt == "..": |
| 22 | +# return ".." |
| 23 | +# dt_obj = rfc3339_str_to_datetime(dt) |
| 24 | +# dt_utc = dt_obj.astimezone(timezone.utc) |
| 25 | +# return dt_utc.strftime("%Y-%m-%dT%H:%M:%SZ") |
| 26 | + |
| 27 | +# if not isinstance(date_str, str): |
| 28 | +# return "../.." |
| 29 | +# if "/" not in date_str: |
| 30 | +# return f"{normalize(date_str)}/{normalize(date_str)}" |
| 31 | +# try: |
| 32 | +# start, end = date_str.split("/", 1) |
| 33 | +# except Exception: |
| 34 | +# return "../.." |
| 35 | +# return f"{normalize(start)}/{normalize(end)}" |
11 | 36 |
|
12 | | - Args: |
13 | | - date_str (str): A string containing two datetime values separated by a '/'. |
14 | 37 |
|
15 | | - Returns: |
16 | | - str: A string formatted as 'YYYY-MM-DDTHH:MM:SSZ/YYYY-MM-DDTHH:MM:SSZ', with '..' used if any element is None. |
| 38 | +def format_datetime_range(date_str: str) -> str: |
| 39 | + """ |
| 40 | + Convert a datetime range string while preserving millisecond precision. |
17 | 41 | """ |
18 | | - |
19 | | - def normalize(dt): |
20 | | - dt = dt.strip() |
21 | | - if not dt or dt == "..": |
22 | | - return ".." |
23 | | - dt_obj = rfc3339_str_to_datetime(dt) |
24 | | - dt_utc = dt_obj.astimezone(timezone.utc) |
25 | | - return dt_utc.strftime("%Y-%m-%dT%H:%M:%SZ") |
26 | | - |
27 | 42 | if not isinstance(date_str, str): |
28 | 43 | return "../.." |
| 44 | + |
| 45 | + # If it's already a range with "..", return as-is to preserve precision |
| 46 | + if "/" in date_str and ".." in date_str: |
| 47 | + return ( |
| 48 | + date_str # <-- PRESERVE original format like "../2025-07-16T00:24:19.000Z" |
| 49 | + ) |
| 50 | + |
| 51 | + # Only apply normalization for closed ranges without ".." |
29 | 52 | if "/" not in date_str: |
30 | | - return f"{normalize(date_str)}/{normalize(date_str)}" |
| 53 | + # Single datetime - normalize with precision |
| 54 | + return normalize(date_str) |
| 55 | + |
| 56 | + # For closed ranges (start/end without ".."), normalize both parts |
31 | 57 | try: |
32 | 58 | start, end = date_str.split("/", 1) |
| 59 | + return f"{normalize(start)}/{normalize(end)}" |
33 | 60 | except Exception: |
34 | 61 | return "../.." |
35 | | - return f"{normalize(start)}/{normalize(end)}" |
| 62 | + |
| 63 | + |
| 64 | +def normalize(dt): |
| 65 | + dt = dt.strip() |
| 66 | + if not dt or dt == "..": |
| 67 | + return ".." |
| 68 | + dt_obj = rfc3339_str_to_datetime(dt) |
| 69 | + dt_utc = dt_obj.astimezone(timezone.utc) |
| 70 | + |
| 71 | + if dt_obj.microsecond > 0: |
| 72 | + return dt_utc.isoformat(timespec="milliseconds").replace("+00:00", "Z") |
| 73 | + else: |
| 74 | + return dt_utc.strftime("%Y-%m-%dT%H:%M:%SZ") |
36 | 75 |
|
37 | 76 |
|
38 | 77 | # Borrowed from pystac - https://github.com/stac-utils/pystac/blob/f5e4cf4a29b62e9ef675d4a4dac7977b09f53c8f/pystac/utils.py#L370-L394 |
|
0 commit comments