All Products
Search
Document Center

Lindorm:Specify a time zone for a LindormTable connection

Last Updated:Dec 17, 2024

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 yyyy-mm-dd format is used, which indicates the year, month, and day. Example: 2024-08-30.

A date string, which is unrelated to time zones.

TIME

The hh:mm:ss format is used, which indicates hours, minutes, and seconds. Example: 08:11:15.

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:

  • yyyy-mm-dd hh:mm:ss.SSS, which is accurate to the millisecond. Example: 2024-08-30 08:11:15.354.

  • An epoch timestamp of the LONG (long integer) type. Example: 1724976675345. Unit: milliseconds.

  • A date and time string, which is related to time zones and indicates the time and date in the current time zone.

  • A timestamp string of the LONG type, which indicates the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC. Unit: milliseconds.

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.

Important

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

Important
  • 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.

  1. Query the time zone .

    SELECT @@time_zone;

    Sample result:

    +-------------+
    | @@time_zone |
    +-------------+
    | +08:00      |
    +-------------+

    The result indicates that the current time zone is UTC+8.

  2. 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 |
    +------+------------+----------+-------------------------+
  3. Change the time zone to UTC.

    SET @@time_zone='UTC';
  4. Check whether the time zone is changed as expected.

    SELECT @@time_zone;

    Sample result:

    +-------------+
    | @@time_zone |
    +-------------+
    | UTC         |
    +-------------+
  5. 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 and c2 columns of the DATE and TIME types remain unchanged because they are unrelated to time zones. The data format in the c3 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 set time_zone statement to specify the value of the connectionTimeZone parameter for the server. If your business runs in a region that does not use the UTC+8 time zone, we recommend that you add this parameter.

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

'UTC'

We recommend that you set this parameter to the system time zone in which the business runs. You can use the set time_zone statement to specify the time zone for the server.

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 parseTime parameter to true, the time data of the DATE and TIMESTAMP types is returned as time data of thetime.Time type.

loc

Local

Specify the time zone in which the time data of the time.Time type is parsed. A value of Local indicates the system time zone.

Note

The parameter takes effect only if you set the parseTime parameter to true.

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())
}