1DROP DATABASE IF EXISTS mysqltest_db1; 2CREATE DATABASE mysqltest_db1; 3USE mysqltest_db1; 4SET GLOBAL EVENT_SCHEDULER= OFF; 5SET @save_time_zone= @@TIME_ZONE; 6CREATE TABLE t_step (step INT); 7INSERT INTO t_step VALUES (@step); 8CREATE FUNCTION round_to_step(i INT, n INT) RETURNS INT 9BEGIN 10DECLARE step INT; 11SELECT * INTO step FROM t_step; 12# We add 0.1 as a protection from inexact division. 13RETURN FLOOR((i % (step * n) + 0.1) / step); 14END// 15SET @step3= @step * 3; 16SET @step6= @step * 6; 17SET @unix_time= UNIX_TIMESTAMP() - 1; 18SET @unix_time= @unix_time - @unix_time % @step6; 19INSERT INTO mysql.time_zone VALUES (NULL, 'N'); 20SET @tzid= LAST_INSERT_ID(); 21INSERT INTO mysql.time_zone_transition_type 22VALUES (@tzid, 0, 0, 0, 'b16420_0'); 23INSERT INTO mysql.time_zone_transition_type 24VALUES (@tzid, 1, @step3 - @step, 1, 'b16420_1'); 25INSERT INTO mysql.time_zone_name VALUES ('<TZ_NAME_1>', @tzid); 26CREATE TABLE t1 (count INT, unix_time INT, local_time INT, comment CHAR(80)); 27CREATE TABLE t2 (count INT); 28INSERT INTO t2 VALUES (1); 29CREATE FUNCTION f1(comment CHAR(80)) RETURNS INT 30BEGIN 31DECLARE orig_tz CHAR(64); 32DECLARE unix_time INT; 33DECLARE local_now DATETIME; 34DECLARE utc_now DATETIME; 35DECLARE local_time INT; 36SET unix_time= UNIX_TIMESTAMP(); 37SET local_now= FROM_UNIXTIME(unix_time); 38SET orig_tz= @@TIME_ZONE; 39SET TIME_ZONE = '+00:00'; 40SET utc_now= FROM_UNIXTIME(unix_time); 41SET TIME_ZONE= orig_tz; 42SET local_time = unix_time + TIMESTAMPDIFF(SECOND, utc_now, local_now); 43SET unix_time= round_to_step(unix_time, 6); 44SET local_time= round_to_step(local_time, 6); 45INSERT INTO t1 VALUES ((SELECT count FROM t2), 46unix_time, local_time, comment); 47RETURN 0; 48END// 49SET TIME_ZONE= '+00:00'; 50CREATE EVENT e1 ON SCHEDULE EVERY @step SECOND 51STARTS FROM_UNIXTIME(@unix_time) DO SELECT f1("<e1>"); 52SET TIME_ZONE= '<TZ_NAME_1>'; 53CREATE EVENT e2 ON SCHEDULE EVERY @step SECOND 54STARTS FROM_UNIXTIME(@unix_time) DO SELECT f1("<e2>"); 55SET GLOBAL EVENT_SCHEDULER= ON; 56SELECT SLEEP(@step / 2); 57SLEEP(@step / 2) 580 59SET GLOBAL EVENT_SCHEDULER= OFF; 60SELECT * FROM t1 ORDER BY count, comment; 61count unix_time local_time comment 621 1 1 <e1> 631 1 3 <e2> 641 1 3 e2 should be executed 652 2 2 <e1> 662 2 4 <e2> 672 2 4 e2 should be executed 683 3 3 <e1> 693 3 3 Second pass after backward -2 step shift, e2 should not be executed 704 4 4 <e1> 714 4 4 Second pass after backward -2 step shift, e2 should not be executed 725 5 5 <e1> 735 5 5 <e2> 745 5 5 e2 should be executed 756 0 0 <e1> 766 0 2 <e2> 776 0 2 Forward +2 step shift, local 0, 1 are skipped, e2 should be executed 787 1 1 <e1> 797 1 3 <e2> 807 1 3 e2 should be executed 81SET TIME_ZONE= @save_time_zone; 82DROP EVENT e2; 83DROP EVENT e1; 84DROP FUNCTION f1; 85DROP TABLE t1, t2; 86DELETE FROM mysql.time_zone_name WHERE time_zone_id = @tzid; 87DELETE FROM mysql.time_zone_transition_type WHERE time_zone_id = @tzid; 88DELETE FROM mysql.time_zone_transition WHERE time_zone_id = @tzid; 89DELETE FROM mysql.time_zone WHERE time_zone_id = @tzid; 90ALTER TABLE mysql.time_zone AUTO_INCREMENT = 6; 91SET TIME_ZONE= '+00:00'; 92CREATE TABLE t1 (event CHAR(2), dt DATE, offset INT); 93INSERT INTO mysql.time_zone VALUES (NULL, 'N'); 94SET @tzid= LAST_INSERT_ID(); 95SET @now= UNIX_TIMESTAMP(); 96SET @offset_month_01= UNIX_TIMESTAMP('2030-01-31 12:00:00') - @now; 97SET @offset_month_02= UNIX_TIMESTAMP('2030-02-28 12:00:00') - @now - 5*@step; 98SET @offset_month_03= UNIX_TIMESTAMP('2030-03-31 12:00:00') - @now - 5*@step; 99SET @offset_month_04= UNIX_TIMESTAMP('2030-04-30 12:00:00') - @now - 13*@step; 100INSERT INTO mysql.time_zone_transition_type 101VALUES (@tzid, 0, @offset_month_01, 0, 'b16420_0'); 102INSERT INTO mysql.time_zone_transition_type 103VALUES (@tzid, 1, @offset_month_02, 1, 'b16420_1'); 104INSERT INTO mysql.time_zone_transition_type 105VALUES (@tzid, 2, @offset_month_03, 1, 'b16420_2'); 106INSERT INTO mysql.time_zone_transition_type 107VALUES (@tzid, 3, @offset_month_04, 1, 'b16420_3'); 108INSERT INTO mysql.time_zone_transition 109VALUES (@tzid, @now, 0); 110INSERT INTO mysql.time_zone_transition 111VALUES (@tzid, @now + 3 * @step, 1); 112INSERT INTO mysql.time_zone_transition 113VALUES (@tzid, @now + 7 * @step, 2); 114INSERT INTO mysql.time_zone_transition 115VALUES (@tzid, @now + 12 * @step, 3); 116INSERT INTO mysql.time_zone_name VALUES ('<TZ_NAME_2>', @tzid); 117SET TIME_ZONE= '<TZ_NAME_2>'; 118SET GLOBAL EVENT_SCHEDULER= ON; 119SET GLOBAL EVENT_SCHEDULER= OFF; 120Below we should see the following: 121- On Jan 31 only e2 is executed, because we started later than 122e1 should have been executed. Offset of e2 is 0 because of 123the late start, not 1. 124- The next execution is on Feb 28 (last day of Feb). Both events 125are executed in their times, offsets are -1 and 1. 126- The next time is Mar 31. Because the time of event 127execution was skipped over, events are executed right away, 128offsets are 2 and 2. 129- The next time is Apr 30. Events are again executed in their 130appointed times, offsets are -1 and 1. 131SELECT * FROM t1 ORDER BY dt, event; 132event dt offset 133e2 2030-01-31 0 134e1 2030-02-28 -1 135e2 2030-02-28 1 136e1 2030-03-31 2 137e2 2030-03-31 2 138e1 2030-04-30 -1 139e2 2030-04-30 1 140DROP EVENT e2; 141DROP EVENT e1; 142DROP TABLE t1; 143SET TIME_ZONE= @save_time_zone; 144DELETE FROM mysql.time_zone_name WHERE time_zone_id = @tzid; 145DELETE FROM mysql.time_zone_transition_type WHERE time_zone_id = @tzid; 146DELETE FROM mysql.time_zone_transition WHERE time_zone_id = @tzid; 147DELETE FROM mysql.time_zone WHERE time_zone_id = @tzid; 148ALTER TABLE mysql.time_zone AUTO_INCREMENT = 6; 149DROP FUNCTION round_to_step; 150DROP TABLE t_step; 151DROP DATABASE mysqltest_db1; 152End of 5.1 tests. 153