forked from embulk/embulk-input-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySQLTimeZoneBuilder.java
49 lines (43 loc) · 1.55 KB
/
MySQLTimeZoneBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package org.embulk.input;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Locale;
import java.util.TimeZone;
public class MySQLTimeZoneBuilder
{
private static final int ONE_HOUR_SEC = 3600;
private static final int ONE_MIN_SEC = 60;
public static TimeZone fromSystemTimeZone(Connection connection)
throws SQLException
{
//
// First, I used `@@system_time_zone`. but It return non Time Zone Abbreviations name on a specific platform.
// So, This method calculate GMT offset with query.
//
String query = "select TIME_TO_SEC(timediff(now(),utc_timestamp()));";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
if (rs.next()) {
int offset_seconds = rs.getInt(1);
return fromGMTOffsetSeconds(offset_seconds);
}
else {
// TODO Error check.
return null;
}
}
public static TimeZone fromGMTOffsetSeconds(int offset_seconds)
{
if( offset_seconds == 0 ) {
return TimeZone.getTimeZone("UTC");
}
String sign = offset_seconds > 0 ? "+" : "-";
int abs_offset_sec = Math.abs(offset_seconds);
int tz_hour = abs_offset_sec / ONE_HOUR_SEC;
int tz_min = abs_offset_sec % ONE_HOUR_SEC / ONE_MIN_SEC;
String tz_name = String.format(Locale.ENGLISH, "GMT%s%02d:%02d", sign, tz_hour, tz_min);
return TimeZone.getTimeZone(tz_name);
}
}