Introduction to MySQL Timezone
The timezones and different settings to manage and manipulate the date and time-related fields in order to store and retrieve zone related times is supported by MySQL. By default, whenever the MySQL is installed on the machine the timezone variables are set such that the timezone set in the system that is the operating system of the machine is referred. We can further, change and manipulate these variables as per our convenience and necessity to store and manipulate date and time values in MySQL. We can change the timezone values by adding or subtracting the required time in hours and minutes from UTC or by specifying the timezone in the named format as per region and zones.
In this article, we will study the working of timezones in MySQL and study some of the related variables and how we can get and set the values of those variables to manipulate timezones. Further, we will see how the timezone related tables can be loaded manually in MySQL and also see some of the examples related to timezones.
Functions of MySQL Timezone
- Whenever a MySQL is started it goes for the searching of the timezone set for the system and sets the value of the system variable named system_time_zone to the timezone value retrieved from the host machine. Once this is done during the server startup value of this variable remains unchanged.
- We can change the system’s time zone at the time of server startup by setting the value of the environment variable named TZ that is to be done prior to running of mysqld command.
- Besides that, there is one more method to set the system time zone in case if you are using mysqld_safe mode for your server, you can set the value for the –timezone option.
- The values that are allowed to be set to the TZ variable or –timezone option depend on the operating system that you are using. Refer documentation of your operating system for further details of acceptable timezone values.
- The time zone that is being used by the MySQL server is determined by the values of current and global values for the time_zone system variable. By default, the value at the beginning of the time_zone variable is SYSTEM which means that the time zone of the host machine’s operating system is used by MySQL.
- We can set the value of the time_zone global variable only if we have SUPER privileges assigned or SYSTEM_VARIABLE_ADMIN privilege using the SET GLOBAL time_zone command.
- The session variable for each of the client sessions that are being used can override the value of time zone set to SYSTEM or global time zone to the required time zone value for that particular session using the SET time_zone command.
- All the values that are zone-sensitive are affected by the time zone set at the session-level. The values displayed by the Now() and CURTIME() functions as well as the columns that have TIMESTAMP as the datatype are affected when it comes to storage and retrieval of such values.
- The values that are stored and retrieved in the columns with datatype DATE, TIME, or DATETIME are not affected by the session time zone settings. Along with that functions such as UTC_TIMESTAMP() also remains unaffected.
- The time zone can be set at the global level or session level in either of the two formats that are defining the offset from the UTC in string format in hours and minutes format hh: mm by adding like ‘-5:30’, ‘+5:30’, etc or subtracting that value or named time zones like America or New_York for America or Asia/Kolkata for Indian Standard Time i.e UTC+05:30.
- The second format of specifying the time zones in the named format needs that the information tables related to the time zones are loaded and populated in your MySQL server.
Populating Information Tables Related to the Timezone
Whenever the MySQL is installed on your machine various tables related to the time zones are created for storing time zone related information inside the schema named MySQL. However, during installation only the creation of such tables in done, while for using the named time zones and many such similar facilities, it is important for these tables to be loaded and populated.
We can load the time zone tables manually. In some operating systems such as macOS, Solaris, Linux, and FreeBSD, they have their own database that stores the zone information. In this case, we can use this file to store the same information into our MySQL schema’s zone tables. The path where you will most often find this file is /usr/share/zoneinfo directory. We can use mysql_tzinfo_to_sql program for populating zone tables from system zone information database using the following command:
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u username -p password
which considers that you are using the account with username as the name and password as the password for that account and have write privileges on MySQL schema of MySQL. For operating systems that do not have such zonal information, you can use downloadable packages.
Examples of MySQL Timezone
To get the global time zone value, you can make the use of the following query statement.
SELECT @@GLOBAL.time_zone;
The execution of the above query statement gives the following output with SYSTEM as the value specifying that the MySQL global time zone is the same as that of the time zone of the operating system.
To get the session time zone value, you can make the use of the following query statement.
SELECT @@SESSION.time_zone;
The execution of the above query statement gives the following output with SYSTEM as the value specifying that the MySQL time zone is the same as that of the time zone of the operating system.
Let us retrieve the current date and time using the NOW() function that gives the following output:
SELECT NOW();
Let us change the time zone for the current session using the following command:
SET time_zone = '+13:00';
that gives the following output:
Let us again execute the same command to retrieve the current date and time:
SELECT NOW();
that gives the following output:
Conclusion
MySQL provides us with various variables that help us to maintain and manage the time zone related information in MySQL. We can get and set the values of the time zone by changing its value on the global or session-level or inside the configuration file.
Recommended Articles
This is a guide to MySQL Timezone. Here we also discuss the introduction and working of mysql timezone along with different examples and its code implementation. You may also have a look at the following articles to learn more –
12 Online Courses | 10 Hands-on Projects | 92+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses