Skip to content

Commit

Permalink
Enable datatime format for negative timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
hairrrrr committed Dec 16, 2024
1 parent 4a051b0 commit dac0b9e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
21 changes: 17 additions & 4 deletions dlt/common/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,30 @@ def detect_datetime_format(value: str) -> Optional[str]:
): "%Y-%m-%dT%H:%M:%S.%fZ", # UTC with fractional seconds
re.compile(
r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+\d{2}:\d{2}$"
): "%Y-%m-%dT%H:%M:%S%z", # Timezone offset
): "%Y-%m-%dT%H:%M:%S%z", # Positive timezone offset
re.compile(
r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+\d{4}$"
): "%Y-%m-%dT%H:%M:%S%z", # Timezone without colon
# Full datetime with fractional seconds and timezone
): "%Y-%m-%dT%H:%M:%S%z", # Positive timezone without colon
# Full datetime with fractional seconds and positive timezone offset
re.compile(
r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+\+\d{2}:\d{2}$"
): "%Y-%m-%dT%H:%M:%S.%f%z",
re.compile(
r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+\+\d{4}$"
): "%Y-%m-%dT%H:%M:%S.%f%z", # Timezone without colon
): "%Y-%m-%dT%H:%M:%S.%f%z", # Positive timezone without colon
re.compile(
r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}-\d{2}:\d{2}$"
): "%Y-%m-%dT%H:%M:%S%z", # Negative timezone offset
re.compile(
r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}-\d{4}$"
): "%Y-%m-%dT%H:%M:%S%z", # Negative timezone without colon
# Full datetime with fractional seconds and negative timezone offset
re.compile(
r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+-\d{2}:\d{2}$"
): "%Y-%m-%dT%H:%M:%S.%f%z",
re.compile(
r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+-\d{4}$"
): "%Y-%m-%dT%H:%M:%S.%f%z", # Negative Timezone without colon
# Datetime without timezone
re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$"): "%Y-%m-%dT%H:%M:%S", # No timezone
re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$"): "%Y-%m-%dT%H:%M", # Minute precision
Expand Down
11 changes: 9 additions & 2 deletions tests/common/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,15 @@ def test_datetime_to_timestamp_helpers(
[
("2024-10-20T15:30:00Z", "%Y-%m-%dT%H:%M:%SZ"), # UTC 'Z'
("2024-10-20T15:30:00.123456Z", "%Y-%m-%dT%H:%M:%S.%fZ"), # UTC 'Z' with fractional seconds
("2024-10-20T15:30:00+02:00", "%Y-%m-%dT%H:%M:%S%z"), # Timezone offset
("2024-10-20T15:30:00+0200", "%Y-%m-%dT%H:%M:%S%z"), # Timezone without colon
("2024-10-20T15:30:00+02:00", "%Y-%m-%dT%H:%M:%S%z"), # Positive timezone offset
("2024-10-20T15:30:00+0200", "%Y-%m-%dT%H:%M:%S%z"), # Positive timezone offset (no colon)
("2024-10-20T15:30:00.123456+02:00", "%Y-%m-%dT%H:%M:%S.%f%z"), # Positive timezone offset with fractional seconds
("2024-10-20T15:30:00.123456+0200", "%Y-%m-%dT%H:%M:%S.%f%z"), # Positive timezone offset with fractional seconds (no colon)
("2024-10-20T15:30:00-02:00", "%Y-%m-%dT%H:%M:%S%z"), # Negative timezone offset
("2024-10-20T15:30:00-0200", "%Y-%m-%dT%H:%M:%S%z"), # Negative timezone offset (no colon)
("2024-10-20T15:30:00.123456-02:00", "%Y-%m-%dT%H:%M:%S.%f%z"), # Negative timezone offset with fractional seconds
("2024-10-20T15:30:00.123456-0200", "%Y-%m-%dT%H:%M:%S.%f%z"), # Negative timezone offset with fractional seconds (no colon)
("2024-10-20T15:30:00", "%Y-%m-%dT%H:%M:%S"), # No timezone
("2024-10-20T15:30", "%Y-%m-%dT%H:%M"), # Minute precision
("2024-10-20T15", "%Y-%m-%dT%H"), # Hour precision
Expand Down

0 comments on commit dac0b9e

Please sign in to comment.