Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL - how to store time with correct timezone? (from Java)
    primarykey
    data
    text
    <p>I'm using MySQL 5.0. I need to store date-time information in one column. I mean to use <code>DATETIME</code> or <code>TIMESTAMP</code> column type. But I have problem with summer-winter time change.</p> <p>My timezone is CET. In summer we use summer-time (CEST, it is GMT+2) and in winter we use winter-time (CET, it is GMT+1). In the day when the summer time changes into winter time (in this year 2012 it was on 28th of October) we have time 2:00AM two-times. The sequence of time in this day is:</p> <pre><code>... -&gt; 1:00 CEST -&gt; 2:00 CEST -&gt; 2:00 CET (= 3:00 CEST) -&gt; 3:00 CET -&gt; ... </code></pre> <p>So if I have timestamp/datetime '2012-10-28 02:00:00' I'm not able to say correctly if this time represents 2:00AM in summer time or in winter time.</p> <p>I have two different Java dates:</p> <pre><code>Date d1 = new Date(1351382400000); // 2:00 CEST (summer-time) Date d2 = new Date(1351386000000); // 2:00 CET (winter-time) </code></pre> <p>And when I store them in the database using standard timestamp/datetime format (ie. <code>'yyyy-MM-dd HH:mm:ss'</code>), both of them store the same data '2012-10-28 02:00:00'. But when I get these values from database back into Date variables I get two same dates. So input dates were different but output dates are equal.<br /> The same occurs if I use the <code>FROM_UNIXTIME</code> function to store date value: <code>FROM_UNIXTIME(1351382400)</code> and <code>FROM_UNIXTIME(1351386000)</code>. Values are stored in the database column (DATETIME or TIMESTAMP type) as equal values. So when I get these values into Java's Date object I get two equal dates again (in winter time).</p> <p>Is there any way to store the timezone in MySQL, or how to handle with timezone information within DATETIME/TIMESTAMP columns?<br /> Of course I can store BIGINT values in the database with unix-timestamp. But I'm wondering if there's any way how to solve this problem with any MySQL date-time type.</p> <p>Any help or trick is appreciated ... :)</p> <p>Thanks a lot.</p> <p>Honza (sporak)</p> <p><strong>EDIT #1:</strong><br /> If I tried to store timestamp values and then fetch this timestamp values into java's Date, I get faulty Date again. Let's say I have simple table with TIMESTAMP column. I store data in this table this way:</p> <pre><code>mysql&gt; INSERT INTO `tab` (timestamp_column) VALUES (FROM_UNIXTIME(1351382400)), // 2:00 CEST (summer-time) (FROM_UNIXTIME(1351386000)); // 2:00 CET (winter-time) Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 </code></pre> <p>When I fetch these rows into java Date object, I'm getting two same dates that shows 2:00 CET. And what's more - when I fetch these rows in MySQL I get wrong values again in MySQL:</p> <pre><code>mysql&gt; SELECT UNIX_TIMESTAMP(timestamp_column) from tab; +--------------------+ | UNIX_TIMESTAMP(ts) | +--------------------+ | 1351386000 | | 1351386000 | +--------------------+ 2 rows in set (0.00 sec) </code></pre> <p>So TIMESTAMP seems to me to be little useless.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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. 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