Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The short answer is:</p> <ul> <li>add "default-time-zone=utc" to my.cnf</li> <li>in your code, always "think" in UTC, except when displaying dates for your users</li> <li><p>when getting/setting dates or timestamps with JDBC, always use the Calendar parameter, set to UTC:</p> <p>resultset.getTimestamp("my_date", Calendar.getInstance(TimeZone.getTimeZone("UTC")));</p></li> <li>either synchronize your servers with NTP, or rely only on the database server to tell you what time it is.</li> </ul> <hr> <p>The long answer is this:</p> <p>When dealing with dates and timezones in any database and with any client code, I usually recommend the following policy:</p> <ol> <li><p><strong>Configure your database to use UTC timezone</strong>, instead of using the server's local timezone (unless it is UTC of course).</p> <ul> <li><p>How to do so depends on your database server. Instructions for MySQL can be found here: <a href="http://dev.mysql.com/doc/refman/5.0/en/time-zone-support.html" rel="noreferrer">http://dev.mysql.com/doc/refman/5.0/en/time-zone-support.html</a>. Basically you need to write this in my.cnf: default-time-zone=utc</p></li> <li><p>This way you can host your database servers anywhere, change your hosting location easily, and more generally manipulate dates on your servers without any ambiguity.</p></li> <li>If you really prefer to use a local timezone, I recommend at least turning off Daylight Saving Time, because having ambiguous dates in your database can be a real nightmare. <ul> <li><em>For example, if you are building a telephony service and you are using Daylight Saving Time on your database server then you are asking for trouble: there will be no way to tell whether a customer who called from "2008-10-26 02:30:00" to "2008-10-26 02:35:00" actually called for 5 minutes or for 1 hour and 5 minutes (supposing Daylight Saving occurred on Oct. 26th at 3am)!</em></li> </ul></li> </ul></li> <li><p><strong>Inside your application code, always use UTC dates, except when displaying dates to your users.</strong></p> <ul> <li>In Java, when reading from the database, always use:</li> </ul> <p>Timestamp myDate = resultSet.getTimestamp("my_date", Calendar.getInstance(TimeZone.getTimeZone("UTC")));</p> <ul> <li>If you do not do this, the timestamp will be assumed to be in your local TimeZone, instead of UTC.</li> </ul></li> <li><p><strong>Synchronize your servers or only rely on the database server's time</strong></p> <ul> <li><p>If you have your Web server on one server (or more) and your database server on some other server, then I strongly recommend you synchronize their clocks with NTP.</p></li> <li><p>OR, only rely on one server to tell you what time it is. Usually, the database server is the best one to ask for time. In other words, avoid code such as this:</p></li> </ul> <p>preparedStatement = connection.prepareStatement("UPDATE my_table SET my_time = ? WHERE [...]");<br> java.util.Date now = new java.util.Date(); // local time! :-(<br> preparedStatement.setTimestamp(1, new Timestamp(now.getTime()));<br> int result = preparedStatement.execute();</p> <ul> <li>Instead, rely on the database server's time:</li> </ul> <p>preparedStatement = connection.prepareStatement("UPDATE my_table SET my_time = NOW() WHERE [...]");<br> int result = preparedStatement.execute();</p></li> </ol> <p>Hope this helps! :-)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload