DEV Community

Geoffrey Kim
Geoffrey Kim

Posted on

Managing Time Zones in Linux and MySQL

Working with time zones is a crucial aspect of server management and application development, ensuring that your applications handle date and time correctly across different geographical locations. This post will guide you through setting the time zone on a Linux server and configuring the MySQL time zone setting, complete with examples to help you manage your systems effectively.

Setting the Time Zone on a Linux Server

To ensure your server operates on the correct time, you might need to set its time zone to match your local time or the time zone of your application's user base. Here's how to set your Linux server to use the Korean time zone, Asia/Seoul.

Checking the Current Time Zone

Before making any changes, you might want to check the current time zone setting:

timedatectl
Enter fullscreen mode Exit fullscreen mode

Or for more detailed information:

timedatectl status
Enter fullscreen mode Exit fullscreen mode

Changing the Time Zone

To change the time zone:

sudo timedatectl set-timezone Asia/Seoul
Enter fullscreen mode Exit fullscreen mode

Verify the change:

timedatectl status
Enter fullscreen mode Exit fullscreen mode

This will set your server's time zone to Seoul, ensuring that all time-related functions operate according to the Korean Standard Time (KST).

Configuring MySQL Time Zone

MySQL's handling of time zones can significantly affect how time-based data is stored and retrieved. You can configure the time zone on a per-connection basis or globally for the MySQL server.

Checking MySQL's Time Zone

  1. Connect to MySQL:
mysql -u username -p
Enter fullscreen mode Exit fullscreen mode
  1. Check the Current Time Zone:

For the current session:

SHOW VARIABLES LIKE 'time_zone';
Enter fullscreen mode Exit fullscreen mode

For the global setting:

SHOW GLOBAL VARIABLES LIKE 'time_zone';
Enter fullscreen mode Exit fullscreen mode

Changing MySQL's Time Zone

You can change the time zone for the current session or globally for all new connections.

  • For the Current Session:
SET time_zone = 'Asia/Seoul';
Enter fullscreen mode Exit fullscreen mode
  • Globally:
SET GLOBAL time_zone = 'Asia/Seoul';
Enter fullscreen mode Exit fullscreen mode

Note: Remember to replace 'Asia/Seoul' with your desired time zone.

Permanently Changing MySQL's Time Zone

Edit the MySQL configuration file (my.cnf or my.ini) and add:

[mysqld]
default-time-zone='Asia/Seoul'
Enter fullscreen mode Exit fullscreen mode

Restart MySQL to apply the changes:

sudo systemctl restart mysql
Enter fullscreen mode Exit fullscreen mode

Conclusion

Correctly managing time zones in your Linux server and MySQL database ensures that your application handles time-based data accurately, avoiding potential issues with time calculations and comparisons. Whether you're setting the server time zone or adjusting MySQL's time zone settings, it's important to test these changes in a development environment before applying them to production systems.


Following up on a keen observation by one of our readers, Simon Green, about handling time zones in MySQL 5, I've decided to delve deeper into this topic and explore how MySQL has evolved in its handling of time-related data types. Simon's insights provide a perfect springboard for discussing the differences between DATETIME, TIMESTAMP, and UNIX_TIMESTAMP, and how these differences play out in real-world applications.

MySQL Evolution: Time Zone Handling from MySQL 5 to Newer Versions

MySQL 5 brought attention to the nuanced ways different data types handle time zones, particularly DATETIME and TIMESTAMP. In newer versions of MySQL, enhancements have been made to offer more flexibility and control over time zone settings at both the global and session levels. These versions maintain the fundamental differences between DATETIME and TIMESTAMP but improve time zone support and the accuracy of time-related functions.

Understanding DATETIME, TIMESTAMP, and UNIX_TIMESTAMP

DATETIME is like a snapshot of a calendar and clock, capturing a specific moment without any concern for where in the world that moment is happening. It's static, not changing no matter where you or your server might find yourselves. Imagine logging the exact time and date of a historical event; DATETIME is your go-to.

TIMESTAMP, on the other hand, is more like a chameleon, adapting to the time zone of the MySQL server. It's stored as the number of seconds since the Unix Epoch (January 1, 1970, 12:00 AM UTC), but when you retrieve it, MySQL converts it to the server's current time zone setting. This makes TIMESTAMP ideal for records that need to reflect the local time of the server, like log entries.

UNIX_TIMESTAMP is the ultimate time traveler, offering a universal point of reference by counting seconds since the Unix Epoch, unaffected by time zones. It's incredibly useful when you need to compare times across different locales or ensure a timestamp's consistency no matter where your users are.

Real-World Usage Stories

Consider a global online marketplace with sellers and buyers from around the world. DATETIME is perfect for product listings – if a seller posts an item at a specific time, that time shouldn't shift based on who's viewing it. Whether a buyer is in New York or New Delhi, the listing time remains constant, providing a fixed reference point.

For TIMESTAMP, think about a user's last login time. A service operating globally might want to show this in the local time zone of each user. By storing these times as TIMESTAMP, the system automatically adjusts this value based on the user's or server's locale, making the data immediately relevant and understandable to the user.

And UNIX_TIMESTAMP? It's ideal for system-level logging across different servers located in various time zones. Regardless of where your servers are, a UNIX_TIMESTAMP provides a consistent timestamp that's easy to compare and calculate with, essential for syncing logs, events, or records across a distributed system.

Conclusion

Simon's comment opened up an excellent opportunity to explore the nuanced handling of time in MySQL, shedding light on how DATETIME, TIMESTAMP, and UNIX_TIMESTAMP serve different needs and scenarios. As MySQL continues to evolve, understanding these differences becomes key to building robust, time-aware applications that serve a global user base. Whether you're logging events, scheduling posts, or recording transactions, choosing the right time type for the job ensures your data remains accurate, relevant, and consistent, no matter where in the world it's accessed.

Top comments (2)

Collapse
 
simongreennet profile image
Simon Green

MySQL 5 does not handle time zones at all well. The DATETIME type is a time on a clock (date and time). The TIMESTAMP type is stored internally as non-leap seconds since Epoch (1/1/1970 12am UTC), but natively will convert this to a datetime type of the servers time zone before returning it. Thankfully there is a UNIX_TIMESTAMP function to return a specific point in time.

Collapse
 
mochafreddo profile image
Geoffrey Kim

Sure thing, Simon! Big thanks for shedding light on the whole time zone thing in MySQL 5. You're spot on about how DATETIME and TIMESTAMP work differently. DATETIME is like setting a clock without worrying about where in the world it is, while TIMESTAMP is all about syncing with the world clock (UTC) and then adjusting to whatever the server's watch says. And yeah, that UNIX_TIMESTAMP function is a real lifesaver for when you want to keep things straightforward with time points.

For anyone dabbling in MySQL, especially the fifth version, getting a grip on this can really make or break how you deal with time in your apps, especially if you've got users from all over the globe. Knowing the ins and outs of time zone management can save a lot of headaches down the road.

Cheers for the pro tip, Simon! It’s super useful and makes navigating the time zone maze a whole lot easier for everyone here.