LindormTable with versions earlier than 2.7.6 supports only the UTC+8 zone, which is the time zone of Java Virtual Machine (JVM). LindormTable 2.7.6 and later versions provide the cross-time zone feature. This topic describes how to specify a time zone for a LindormTable connection to ensure that time data is returned as expected.
Background information
Lindorm supports the TIMESTAMP, DATE, and TIME data types. Only the TIMESTAMP type is related to time zones. The following table describes the preceding data types.
Data type | Format | How the data type is related to time zones |
DATE | The | A date string, which is unrelated to time zones. |
TIME | The | A time string, which is unrelated to time zones. |
TIMESTAMP | Formats that indicate the year, month, day, hours, minutes, seconds, and milliseconds are used. The following formats are supported:
|
|
Prerequisites
The LindormTable version is 2.7.6 or later. For information about how to view or upgrade the version of LindormTable, see Release notes of LindormTable and Upgrade the minor engine version of a Lindorm instance.
If you cannot upgrade the LindormTable version of your instance to 2.7.6 or later in the Lindorm console, contact technical support (DingTalk ID: s0s3eg3).
Limits
Only the MySQL protocol allows you to specify a time zone for a LindormTable connection. For more information, see Use the MySQL protocol to develop applications (recommended).
Specify the time zone
The default time zone for Lindorm is UTC+8.
The time zone that you specify takes effect only for the current LindormTable connection.
Use the SET
syntax to specify a time zone for the current LindormTable connection. The time zone that you specify takes effect only for the current LindormTable connection. For more information about the syntax, see Session variables.
Specify the UTC time zone for the current LindormTable connection.
SET @@time_zone='UTC';
Specify the +08:00 time zone for the current LindormTable connection.
SET @@time_zone='+08:00';
Specify the Shanghai time zone which is the local time of Shanghai, for the current Lindorm connection.
SET @@time_zone='Asia/Shanghai';
Query the current time zone
SELECT @@time_zone;
Examples
MySQL command-line tool
For information about how to use the MySQL command-line tool to connect to LindormTable, see Use a MySQL client to connect to and use LindormTable.
In this example, a table named tb
is created and populated by executing the following statements:
-- Create the table.
CREATE TABLE tb(p1 int, c1 date, c2 time, c3 timestamp(3), PRIMARY KEY(p1));
-- Insert data into the table.
UPSERT INTO tb(p1,c1,c2,c3) VALUES(1, '2024-08-30', '08:11:15', '2024-08-30 08:11:15.354');
You can query and change the time zone.
Query the time zone .
SELECT @@time_zone;
Sample result:
+-------------+ | @@time_zone | +-------------+ | +08:00 | +-------------+
The result indicates that the current time zone is UTC+8.
Query the time data format in the current time zone.
SELECT * FROM tb;
Sample result:
+------+------------+----------+-------------------------+ | p1 | c1 | c2 | c3 | +------+------------+----------+-------------------------+ | 1 | 2024-08-30 | 08:11:15 | 2024-08-30 08:11:15.354 | +------+------------+----------+-------------------------+
Change the time zone to UTC.
SET @@time_zone='UTC';
Check whether the time zone is changed as expected.
SELECT @@time_zone;
Sample result:
+-------------+ | @@time_zone | +-------------+ | UTC | +-------------+
Recheck the time data format in the current time zone to check whether the time zone is changed as expected.
SELECT * FROM tb;
Sample result:
+------+------------+----------+-------------------------+ | p1 | c1 | c2 | c3 | +------+------------+----------+-------------------------+ | 1 | 2024-08-30 | 08:11:15 | 2024-08-30 00:11:15.354 | +------+------------+----------+-------------------------+
The data formats of the
c1
andc2
columns of the DATE and TIME types remain unchanged because they are unrelated to time zones. The data format in thec3
column of the TIMESTAMP type is changed to the date and time in the UTC time zone.
Java
For information about how to use Java Database Connectivity (JDBC) to connect to LindormTable, see Use Java JDBC APIs to develop applications.
If your business runs in a region that does not use the UTC+8 time zone, we recommend that you add the forceConnectionTimeZoneToSession=true
configuration to the connection string to specify connection time zone.
Parameter | Sample value | Description |
connectionTimeZone | UTC | Specify the connection time zone. The default value is the time zone of JVM on the client, which is also the dedault time zone used by the virtual machine that runs the Java program. If you do not have special requirements, ignore this parameter. |
forceConnectionTimeZoneToSession | true | Use the |
Sample connection code
String url = "jdbc:mysql://ld-uf6k8yqb741t3****-proxy-sql-lindorm-public.lindorm.rds.aliyuncs.com:33060/" + database + "?sslMode=disabled&allowPublicKeyRetrieval=true&useServerPrepStmts=true&useLocalSessionState=true&rewriteBatchedStatements=true&cachePrepStmts=true&prepStmtCacheSize=100&prepStmtCacheSqlLimit=50000000&forceConnectionTimeZoneToSession=true";
Properties properties = new Properties();
properties.put("user", username);
properties.put("password", password);
//Obtain the established connection.
Connection connection = DriverManager.getConnection(url, properties);
Python
For information about how to use the mysql-connector-python driver to connect to LindormTable, see Use mysql-connector-python to develop applications.
If your business runs in a region that uses the UTC time zone, you must add the time_zone parameter to the connection string to change the connection time zone. For example, add the time_zone='UTC'
string to the connection string.
Parameter | Sample value | Description |
time_zone |
| We recommend that you set this parameter to the system time zone in which the business runs. You can use the |
Sample code:
connection = mysql.connector.connect(host='<LindormTable endpoint for MySQL>', port=33060, user='<Username>', passwd='<Password>', database='<Database name>', time_zone='<Time zone>')
Go
For information about how to use the Golang MySQL Driver to connect to LindormTable, see Use Go to develop applications.
If your business runs in a region that uses the UTC time zone, you must add the loc=Locall&time_zone=%27UTC%27
configuration to the connection string.
Parameter | Sample value | Description |
parseTime | true | When you set the |
loc | Local | Specify the time zone in which the time data of the Note The parameter takes effect only if you set the |
time_zone | %27UTC%27 | Specify the system time zone in which the business runs. |
For more information, see System Variables.
Sample code:
urlString := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?timeout=%s&parseTime=true&loc=Local", user, password, host, port, database, connectTimeout)
timeZoneValue := url.QueryEscape("'UTC'")
urlString = urlString + "&time_zone=" + timeZoneValue
db, err := sql.Open("mysql", urlString)
if err != nil {
panic(err.Error())
}