1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.db.protocol.mysql.packet.binlog.row.column.value.time;
19
20 import org.apache.shardingsphere.db.protocol.mysql.packet.binlog.row.column.MySQLBinlogColumnDef;
21 import org.apache.shardingsphere.db.protocol.mysql.packet.binlog.row.column.value.MySQLBinlogProtocolValue;
22 import org.apache.shardingsphere.db.protocol.mysql.payload.MySQLPacketPayload;
23
24 import java.io.Serializable;
25 import java.sql.Timestamp;
26 import java.time.LocalDateTime;
27 import java.util.Date;
28
29
30
31
32
33
34
35 public final class MySQLDatetimeBinlogProtocolValue implements MySQLBinlogProtocolValue {
36
37 @Override
38 public Serializable read(final MySQLBinlogColumnDef columnDef, final MySQLPacketPayload payload) {
39 long datetime = payload.readInt8();
40 return 0 == datetime ? MySQLTimeValueUtils.DATETIME_OF_ZERO : readDateTime(datetime);
41 }
42
43 private Date readDateTime(final long datetime) {
44 int date = (int) (datetime / 1000000);
45 int year = date / 10000;
46 int month = (date % 10000) / 100;
47 int day = date % 100;
48 int time = (int) (datetime % 1000000);
49 int hour = time / 10000;
50 int minute = (time % 10000) / 100;
51 int second = time % 100;
52 return Timestamp.valueOf(LocalDateTime.of(year, month, day, hour, minute, second));
53 }
54 }