Skip to content

add ServerTimeZone to correct wrong time value #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public abstract class AbstractRowsEventDataDeserializer<T extends EventData> imp
private boolean deserializeDateAndTimeAsLong;
private boolean microsecondsPrecision;
private boolean deserializeCharAndBinaryAsByteArray;
private static int serverTimezoneDiff;

public AbstractRowsEventDataDeserializer(Map<Long, TableMapEventData> tableMapEventByTableId) {
this.tableMapEventByTableId = tableMapEventByTableId;
Expand All @@ -91,6 +92,10 @@ void setDeserializeCharAndBinaryAsByteArray(boolean value) {
this.deserializeCharAndBinaryAsByteArray = value;
}

void setServerTimezoneDiff(int serverTimezoneDiff) {
this.serverTimezoneDiff = serverTimezoneDiff;
}

protected Serializable[] deserializeRow(long tableId, BitSet includedColumns, ByteArrayInputStream inputStream)
throws IOException {
TableMapEventData tableMapEvent = tableMapEventByTableId.get(tableId);
Expand Down Expand Up @@ -538,7 +543,7 @@ public static long from(int year, int month, int day, int hour, int minute, int
long daysUpToMonth = isLeapYear(year) ? LEAP_YEAR_DAYS_BY_MONTH[month - 1] : YEAR_DAYS_BY_MONTH[month - 1];
timestamp += ((daysUpToMonth + day - 1) * 24 * 60 * 60) +
(hour * 60 * 60) + (minute * 60) + (second);
timestamp = timestamp * 1000 + millis;
timestamp = timestamp * 1000 + millis - serverTimezoneDiff;
return timestamp;
}

Expand All @@ -553,7 +558,7 @@ private static long fallbackToGC(int year, int month, int dayOfMonth, int hourOf
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, second);
c.set(Calendar.MILLISECOND, millis);
return c.getTimeInMillis();
return c.getTimeInMillis() - serverTimezoneDiff;
}

private static int leapYears(int from, int end) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.TimeZone;

/**
* @author <a href="mailto:[email protected]">Stanley Shyiko</a>
Expand Down Expand Up @@ -141,6 +142,23 @@ private void afterEventDataDeserializerSet(EventType eventType) {
public void setChecksumType(ChecksumType checksumType) {
this.checksumLength = checksumType.getLength();
}

/**
* @see set timezone setting to calculate unix_timestamp
*/
public void setServerTimeZone(String tzId) {
// Check valiad timezone, invalid timezone would be UTC
String tz = TimeZone.getTimeZone(tzId).getID();

for (EventDataDeserializer eventDataDeserializer : eventDataDeserializers.values()) {
if (eventDataDeserializer instanceof AbstractRowsEventDataDeserializer) {
AbstractRowsEventDataDeserializer deserializer =
(AbstractRowsEventDataDeserializer) eventDataDeserializer;
// calculate time diff from UTC
deserializer.setServerTimezoneDiff(TimeZone.getTimeZone(tz).getRawOffset());
}
}
}

/**
* @see CompatibilityMode
Expand Down