MySQL automatic daylight savings time (DST) computation [duplicate] - mysql

I'm using the America/New York timezone. In the Fall we "fall back" an hour -- effectively "gaining" one hour at 2am. At the transition point the following happens:
it's 01:59:00 -04:00
then 1 minute later it becomes:
01:00:00 -05:00
So if you simply say "1:30am" it's ambiguous as to whether or not you're referring to the first time 1:30 rolls around or the second. I'm trying to save scheduling data to a MySQL database and can't determine how to save the times properly.
Here's the problem:
"2009-11-01 00:30:00" is stored internally as 2009-11-01 00:30:00 -04:00
"2009-11-01 01:30:00" is stored internally as 2009-11-01 01:30:00 -05:00
This is fine and fairly expected. But how do I save anything to 01:30:00 -04:00? The documentation does not show any support for specifying the offset and, accordingly, when I've tried specifying the offset it's been duly ignored.
The only solutions I've thought of involve setting the server to a timezone that doesn't use daylight savings time and doing the necessary transformations in my scripts (I'm using PHP for this). But that doesn't seem like it should be necessary.
Many thanks for any suggestions.

I've got it figured out for my purposes. I'll summarize what I learned (sorry, these notes are verbose; they're as much for my future referral as anything else).
Contrary to what I said in one of my previous comments, DATETIME and TIMESTAMP fields do behave differently. TIMESTAMP fields (as the docs indicate) take whatever you send them in "YYYY-MM-DD hh:mm:ss" format and convert it from your current timezone to UTC time. The reverse happens transparently whenever you retrieve the data. DATETIME fields do not make this conversion. They take whatever you send them and just store it directly.
Neither the DATETIME nor the TIMESTAMP field types can accurately store data in a timezone that observes DST. If you store "2009-11-01 01:30:00" the fields have no way to distinguish which version of 1:30am you wanted -- the -04:00 or -05:00 version.
Ok, so we must store our data in a non DST timezone (such as UTC). TIMESTAMP fields are unable to handle this data accurately for reasons I'll explain: if your system is set to a DST timezone then what you put into TIMESTAMP may not be what you get back out. Even if you send it data that you've already converted to UTC, it will still assume the data's in your local timezone and do yet another conversion to UTC. This TIMESTAMP-enforced local-to-UTC-back-to-local roundtrip is lossy when your local timezone observes DST (since "2009-11-01 01:30:00" maps to 2 different possible times).
With DATETIME you can store your data in any timezone you want and be confident that you'll get back whatever you send it (you don't get forced into the lossy roundtrip conversions that TIMESTAMP fields foist on you). So the solution is to use a DATETIME field and before saving to the field convert from your system time zone into whatever non-DST zone you want to save it in (I think UTC is probably the best option). This allows you to build the conversion logic into your scripting language so that you can explicitly save the UTC equivalent of "2009-11-01 01:30:00 -04:00" or ""2009-11-01 01:30:00 -05:00".
Another important thing to note is that MySQL's date/time math functions don't work properly around DST boundaries if you store your dates in a DST TZ. So all the more reason to save in UTC.
In a nutshell I now do this:
When retrieving the data from the database:
Explicitly interpret the data from the database as UTC outside of MySQL in order to get an accurate Unix timestamp. I use PHP's strtotime() function or its DateTime class for this. It can not be reliably done inside of MySQL using MySQL's CONVERT_TZ() or UNIX_TIMESTAMP() functions because CONVERT_TZ will only output a 'YYYY-MM-DD hh:mm:ss' value which suffers from ambiguity problems, and UNIX_TIMESTAMP() assumes its input is in the system timezone, not the timezone the data was ACTUALLY stored in (UTC).
When storing the data to the database:
Convert your date to the precise UTC time that you desire outside of MySQL. For example: with PHP's DateTime class you can specify "2009-11-01 1:30:00 EST" distinctly from "2009-11-01 1:30:00 EDT", then convert it to UTC and save the correct UTC time to your DATETIME field.
Phew. Thanks so much for everyone's input and help. Hopefully this saves someone else some headaches down the road.
BTW, I am seeing this on MySQL 5.0.22 and 5.0.27

MySQL's date types are, frankly, broken and cannot store all times correctly unless your system is set to a constant offset timezone, like UTC or GMT-5. (I'm using MySQL 5.0.45)
This is because you can't store any time during the hour before Daylight Saving Time ends. No matter how you input dates, every date function will treat these times as if they are during the hour after the switch.
My system's timezone is America/New_York. Let's try storing 1257051600 (Sun, 01 Nov 2009 06:00:00 +0100).
Here's using the proprietary INTERVAL syntax:
SELECT UNIX_TIMESTAMP('2009-11-01 00:00:00' + INTERVAL 3599 SECOND); # 1257051599
SELECT UNIX_TIMESTAMP('2009-11-01 00:00:00' + INTERVAL 3600 SECOND); # 1257055200
SELECT UNIX_TIMESTAMP('2009-11-01 01:00:00' - INTERVAL 1 SECOND); # 1257051599
SELECT UNIX_TIMESTAMP('2009-11-01 01:00:00' - INTERVAL 0 SECOND); # 1257055200
Even FROM_UNIXTIME() won't return the accurate time.
SELECT UNIX_TIMESTAMP(FROM_UNIXTIME(1257051599)); # 1257051599
SELECT UNIX_TIMESTAMP(FROM_UNIXTIME(1257051600)); # 1257055200
Oddly enough, DATETIME will still store and return (in string form only!) times within the "lost" hour when DST starts (e.g. 2009-03-08 02:59:59). But using these dates in any MySQL function is risky:
SELECT UNIX_TIMESTAMP('2009-03-08 01:59:59'); # 1236495599
SELECT UNIX_TIMESTAMP('2009-03-08 02:00:00'); # 1236495600
# ...
SELECT UNIX_TIMESTAMP('2009-03-08 02:59:59'); # 1236495600
SELECT UNIX_TIMESTAMP('2009-03-08 03:00:00'); # 1236495600
The takeaway: If you need to store and retrieve every time in the year, you have a few undesirable options:
Set system timezone to GMT + some constant offset. E.g. UTC
Store dates as INTs (as Aaron discovered, TIMESTAMP isn't even reliable)
Pretend the DATETIME type has some constant offset timezone. E.g. If you're in America/New_York, convert your date to GMT-5 outside of MySQL, then store as a DATETIME (this turns out to be essential: see Aaron's answer). Then you must take great care using MySQL's date/time functions, because some assume your values are of the system timezone, others (esp. time arithmetic functions) are "timezone agnostic" (they may behave as if the times are UTC).
Aaron and I suspect that auto-generating TIMESTAMP columns are also broken. Both 2009-11-01 01:30 -0400 and 2009-11-01 01:30 -0500 will be stored as the ambiguous 2009-11-01 01:30.

I think micahwittman's link has the best practical solution to these MySQL limitations: Set the session timezone to UTC when you connect:
SET SESSION time_zone = '+0:00'
Then you just send it Unix timestamps and everything should be fine.

But how do I save anything to 01:30:00
-04:00?
You can convert to UTC like:
SELECT CONVERT_TZ('2009-11-29 01:30:00','-04:00','+00:00');
Even better, save the dates as a TIMESTAMP field. That's always stored in UTC, and UTC doesn't know about summer/winter time.
You can convert from UTC to localtime using CONVERT_TZ:
SELECT CONVERT_TZ(UTC_TIMESTAMP(),'+00:00','SYSTEM');
Where '+00:00' is UTC, the from timezone , and 'SYSTEM' is the local timezone of the OS where MySQL runs.

Mysql inherently solves this problem using time_zone_name table from mysql db.
Use CONVERT_TZ while CRUD to update the datetime without worrying about daylight savings time.
SELECT
CONVERT_TZ('2019-04-01 00:00:00','Europe/London','UTC') AS time1,
CONVERT_TZ('2019-03-01 00:00:00','Europe/London','UTC') AS time2;

This thread made me freak since we use TIMESTAMP columns with On UPDATE CURRENT_TIMESTAMP (ie: recordTimestamp timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP) to track changed records and ETL to a datawarehouse.
In case someone wonder, in this case, TIMESTAMP behave correctly and you can differentiate between the two similar dates by converting the TIMESTAMP to unix timestamp:
select TestFact.*, UNIX_TIMESTAMP(recordTimestamp) from TestFact;
id recordTimestamp UNIX_TIMESTAMP(recordTimestamp)
1 2012-11-04 01:00:10.0 1352005210
2 2012-11-04 01:00:10.0 1352008810

I was working on logging counts of visits of pages and displaying the counts in graph (using Flot jQuery plugin). I filled the table with test data and everything looked fine, but I noticed that at the end of the graph the points were one day off according to labels on x-axis. After examination I noticed that the view count for day 2015-10-25 was retrieved twice from the database and passed to Flot, so every day after this date was moved by one day to right.
After looking for a bug in my code for a while I realized that this date is when the DST takes place. Then I came to this SO page...
...but the suggested solutions was an overkill for what I needed or they had other disadvantages. I am not very worried about not being able to distinguish between ambiguous timestamps. I just need to count and display records per days.
First, I retrieve the date range:
SELECT
DATE(MIN(created_timestamp)) AS min_date,
DATE(MAX(created_timestamp)) AS max_date
FROM page_display_log
WHERE item_id = :item_id
Then, in a for loop, starting with min_date, ending with max_date, by step of one day (60*60*24), I'm retrieving the counts:
for( $day = $min_date_timestamp; $day <= $max_date_timestamp; $day += 60 * 60 * 24 ) {
$query = "
SELECT COUNT(*) AS count_per_day
FROM page_display_log
WHERE
item_id = :item_id AND
(
created_timestamp BETWEEN
'" . date( "Y-m-d 00:00:00", $day ) . "' AND
'" . date( "Y-m-d 23:59:59", $day ) . "'
)
";
//execute query and do stuff with the result
}
My final and quick solution to my problem was this:
$min_date_timestamp += 60 * 60 * 2; // To avoid DST problems
for( $day = $min_date_timestamp; $day <= $max_da.....
So I am not staring the loop in the beginning of the day, but two hours later. The day is still the same, and I am still retrieving correct counts, since I explicitly ask the database for records between 00:00:00 and 23:59:59 of the day, regardless of the actual time of the timestamp. And when the time jumps by one hour, I am still in the correct day.
Note: I know this is 5 year old thread, and I know this is not an answer to OPs question, but it might help people like me who encountered this page looking for solution to the problem I described.

Related

Modifying the query to solve the Problem 2038 in MariaDB

I have a SQL query:
update party set left_time=(next_dose-dose)/power,takeoff=from_unixtime(unix_timestamp()+left_time);
How can I modify it without using unix time to get the dates further than 2038-01-19?
If you want just the UTC time that is left_time seconds from now, just do:
utc_timestamp() + interval left_time second
But that's not what from_unixtime does; from_unixtime will produce a time in the session's timezone. If that is what you need, you could naively do
current_timestamp() + interval left_time second
but that will not produce correct results if there is a daylight savings transition upcoming, so you have to do:
convert_tz(utc_timestamp() + interval left_time second, '+00:00', ##SESSION.time_zone)
(An example of why you should always just store UTC times and only convert them for display.) If takeoff is a timestamp type instead of a datetime, you have to do this, since it automatically converts to/from the session timezone whenever you read/update it, though it actually stores a utc time.

How to convert a JSON date to an Oracle date in local time

Let's say today is 30-NOV-2016 at 00:00:00 in Europe (GMT+1) and a frontend JavaScript application JSON.stringify(new Date(2016, 11-1, 30)) send a JSON (stringified) date to an Oracle backend.
This date would arrive as the string "2016-11-29T23:00:00.000Z" and now I would like to convert this string to a proper Oracle DATE in the (again) local timezone (GMT+1).
I expected
SELECT CAST(TO_TIMESTAMP_TZ('2016-11-29T23:00:00.000Z', 'FXYYYY-MM-DD"T"HH24:MI:SS.FXFF3"Z"') AT LOCAL AS DATE) FROM DUAL;
to do the trick, but this actually returns me the UTC date 29.11.2016 23:00:00 and not the correct local date 30.11.2016 00:00:00.
This should be quite straightforward but I cannot seem to figure out, what I'm doing wrong?
When you use to_timestamp_tz() but don't actually specify the time zone in the conversion it defaults to your system time zone, which presumably isn't UTC. The timestamp with time zone you are generating is therefore already in your local system time zone, so at local isn't doing anything.
You can convert to a plain timestamp instead, and declare the time zone as UTC with the from_tz() function; then still use the AT LOCAL expression to change it to your local time zone:
alter session set time_zone = 'Europe/Vienna';
SELECT CAST(
FROM_TZ(TO_TIMESTAMP('2016-11-29T23:00:00.000Z', 'FXYYYY-MM-DD"T"HH24:MI:SS.FXFF3"Z"'),
'UTC') AT LOCAL AS DATE) FROM DUAL;
CAST(FROM_TZ(TO_TIM
-------------------
2016-11-30 00:00:00
Breaking that down a bit:
TO_TIMESTAMP('2016-11-29T23:00:00.000Z', 'FXYYYY-MM-DD"T"HH24:MI:SS.FXFF3"Z"') converts your string value to a plain TIMESTAMP value, with no time zone information.
FROM_TZ(..., 'UTC') converts that plain timestamp to a time stamp with time zone with the time zone part as UTC - no adjustment is done to any of the date/time elements, it just states those represent a UTC value.
... AT LOCAL converts to your session time zone (which might not be the same as your system time zone).
CAST(... AS DATE) converts the value in your local time zone to a date; again no adjustment is done to the element values, but you lose the fractional seconds and time zone information.
You could also stick with to_timestamp_tz() but include the UTC code:
SELECT CAST(
TO_TIMESTAMP_TZ('2016-11-29T23:00:00.000Z' || 'UTC',
'FXYYYY-MM-DD"T"HH24:MI:SS.FXFF3"Z"TZR')
AT LOCAL AS DATE) FROM DUAL;
or replace the Z with UTC:
SELECT CAST(
TO_TIMESTAMP_TZ(REPLACE('2016-11-29T23:00:00.000Z', 'Z', 'UTC'),
'FXYYYY-MM-DD"T"HH24:MI:SS.FXFF3TZR')
AT LOCAL AS DATE) FROM DUAL;
All of these assume - correctly, I believe - that the JSON string will always be UTC and the Z can be assumed to be there and to mean that (as it should do, of course).

FROM_UNIXTIME does not match DATE_ADD with seconds

Could somebody please explain why this differs?
SELECT
FROM_UNIXTIME(547164000),
DATE_ADD(FROM_UNIXTIME(0), INTERVAL 547164000 SECOND),
##session.time_zone
returns
1987-05-05 00:00:00
1987-05-04 23:00:00
SYSTEM
system's timezone is CEST, +0200 (obtained by command date +%z).
I am using this DATE_ADD method to handle negative timestamp as FROM_UNIXTIME does not support negative values.
A MySQL server can be set up with its own time_zone, such that it doesn't behave like PHP's time(). That is, storing a time() value into a TIMESTAMP field and then displaying the resulting formatted date and time may not give the expected results. You have to be careful not to mix SQL and non-SQL timestamp usage, but stick to one or the other. Why exactly it gives different results for the two expressions is not clear to me, but could point up a bug in MySQL. You would think that adding N seconds to 0 time would give the same results as a plain N time... are you sure that they are formatting under exactly the same time_zone and DST/Summer Time rules?

FROM_UNIXTIME and performance

Does FROM_UNIXTIME heavily affect the performance of a query like this:
(
(calltasks.task_dueDate = '".$date."') # Tasks of this date
OR
(calltasks.task_dueDate < '".$date."' AND calltasks.task_status = 'scheduled') # Tasks still available on this date
OR
(FROM_UNIXTIME(calltasks.task_executionTime, '%Y-%m-%d') = '".$date."') # Tasks finished this date
)
Or I should be fine with it ??
Rather than converting your UNIX time to a formatted string, then comparing those (as you currently are), you would be better to use UNIX_TIMESTAMP() to convert your $date variable to a UNIX time and then compare integers (checking the difference is no more than 24 hours). Integer comparison will always be faster than string comparison:
(calltasks.task_executionTime - UNIX_TIMESTAMP('$date')) BETWEEN 0 AND 86400
Alternatively, you may find that you already have the UNIX timestamp value of $date somewhere readily available in the language from which you are invoking this query: PHP, for example, stores dates as UNIX timestamps.
Note that you should be absolutely certain that $date cannot be manipulated to contain SQL if you are inserting it into your query in this fashion; if there is any doubt, you should use a prepared statement: if you don't know why, or what they are, read about Bobby Tables.

Which MySQL data type to use for scheduling?

I want to record the start and end times of, say, movies playing at the cinemas. I need to store the date (e.g. Jan 1, 2011), start time (e.g. 9:00am), and end time (e.g. 11am). Should I used DateTime? I've seen some implementations wherein an INT is used to represent time in minutes (e.g. 0 = 12 midnight, 1 = 12:01am, etc.). I'm not sure if there is some sort of benefit to using INT over DateTime.
Suggestions?
EDIT 1:
Expanding on the idea of using INT as described above, the table would probably have the following columns:
date (e.g. Jan 1, 2011)
start_time (e.g. 540 - to indicate 9am)
end_time (e.g. 660 - to indicate 11am)
In my opinion a person should use the datatype that better fits and describes the reality. In this case I would like to use Datetime.
For me better is to use to DateTime columns, because INT have hidden interpretation. Without documentation you don't know what is it really: second, minutes, months, ...
It is only design approach.
When using DATETIME for start and end fields take into consideration that you may have trouble when "calculationg how long process take", because you should also think about timezones, daylight saving, etc.
Use DATETIME or INT
I prefer to store date&time in unix timestamp (INT), because i can use it in PHP functions like date
echo "Movie will start at ".date("H:i", $row['start_time']);
And it's easy to manipulate with it:
echo "Movie will start in ".(time() - $row['start_time'])." seconds";
Current date and time: http://www.unixtimestamp.com/
If you want to copy movies to next day:
$sql = "SELECT * FROM movies WHERE start_time >= ".strtotime("today 00:00")." AND start_time <= ".strtotime("today 23:59");
// query..
foreach($results as $row) {
$new_start = strtotime("+1 day", $row['start_time'];
// insert
}
I would use 'timestamp' for both columns.
Datetime uses 4 bytes whereas Timestamp uses 8 bytes therefore more efficient.
Comparisons of timestamps is significantly faster than of datetimes.
When it comes to time zones leap years and daylight saving you may run into trouble with the int technique as you will have to take those changes into account manually. Maybe this won't be an issue for you.
I'd say you have two appropriate formats:
"start time - duration" (datetime, int) or (timestamp, int)
"start time - end time" (datetime, datetime) or (timestamp, timestamp)
One of the big problems with date-time stuff is the DST jumps twice a year, throwing off your calculations by an hour. Traveling between timezones is equally confusing. If you use the time/time notation, your duration may vary. If you use the time/duration notation, your end time may vary. Whichever one of these two representations is the most appropriate therefore depends on your usage (you may need to use a hybrid approach).
For movie show times, I'd say storing a start time and a duration in minutes is the more appropriate format. The run-time of the movie is a given, the end time comes after the fact.