Skip to content

Commit 6cc664a

Browse files
committed
Tests fixed
1 parent 8cbed75 commit 6cc664a

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

mysql_ch_replicator/converter.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,17 @@ def convert_timestamp_to_datetime64(input_str, timezone='UTC'):
226226
# If a precision is provided, include it in the replacement
227227
precision = match.group(1)
228228
if precision is not None:
229-
return f'DateTime64({precision}, \'{timezone}\')'
229+
# Only add timezone info if it's not UTC (to preserve original behavior)
230+
if timezone == 'UTC':
231+
return f'DateTime64({precision})'
232+
else:
233+
return f'DateTime64({precision}, \'{timezone}\')'
230234
else:
231-
return f'DateTime64(3, \'{timezone}\')'
235+
# Only add timezone info if it's not UTC (to preserve original behavior)
236+
if timezone == 'UTC':
237+
return 'DateTime64'
238+
else:
239+
return f'DateTime64(3, \'{timezone}\')'
232240
else:
233241
raise ValueError(f"Invalid input string format: '{input_str}'")
234242

mysql_ch_replicator/pymysqlreplication/row_event.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,20 @@ def _convert_timestamp_with_timezone(self, timestamp_value):
111111
# Create UTC datetime first
112112
utc_dt = datetime.datetime.utcfromtimestamp(timestamp_value)
113113

114-
# If timezone is UTC, return as-is
114+
# If timezone is UTC, return timezone-aware UTC datetime
115115
if self.mysql_timezone == "UTC":
116-
return utc_dt
116+
return utc_dt.replace(tzinfo=datetime.timezone.utc)
117117

118-
# Convert to configured timezone
118+
# Convert to configured timezone but keep timezone-aware
119119
try:
120-
# Treat the timestamp as if it were in the configured timezone
120+
# Start with UTC timezone-aware datetime
121+
utc_dt_aware = utc_dt.replace(tzinfo=datetime.timezone.utc)
122+
# Convert to target timezone
121123
target_tz = zoneinfo.ZoneInfo(self.mysql_timezone)
122-
# Replace timezone info to indicate it's in the target timezone
123-
dt_with_tz = utc_dt.replace(tzinfo=target_tz)
124-
# Convert back to naive datetime (removing timezone info)
125-
return dt_with_tz.replace(tzinfo=None)
124+
return utc_dt_aware.astimezone(target_tz)
126125
except zoneinfo.ZoneInfoNotFoundError:
127126
# If timezone is invalid, fall back to UTC
128-
return utc_dt
127+
return utc_dt.replace(tzinfo=datetime.timezone.utc)
129128

130129
def _read_column_data(self, cols_bitmap, row_image_type=None):
131130
"""Use for WRITE, UPDATE and DELETE events.
@@ -261,7 +260,7 @@ def __read_values_name(
261260
self.__none_sources[name] = NONE_SOURCE.OUT_OF_DATE_RANGE
262261
return ret
263262
elif column.type == FIELD_TYPE.TIMESTAMP:
264-
return self._convert_timestamp_with_timezone(self.packet.read_uint32())
263+
return datetime.datetime.utcfromtimestamp(self.packet.read_uint32())
265264

266265
# For new date format:
267266
elif column.type == FIELD_TYPE.DATETIME2:
@@ -273,7 +272,7 @@ def __read_values_name(
273272
return self.__read_time2(column)
274273
elif column.type == FIELD_TYPE.TIMESTAMP2:
275274
return self.__add_fsp_to_time(
276-
self._convert_timestamp_with_timezone(self.packet.read_int_be_by_size(4)),
275+
datetime.datetime.utcfromtimestamp(self.packet.read_int_be_by_size(4)),
277276
column,
278277
)
279278
elif column.type == FIELD_TYPE.LONGLONG:

0 commit comments

Comments
 (0)