Skip to content

Commit 94eb819

Browse files
MDEV-31684 Add timezone information to DATE_FORMAT
Before starting to go over the format string, prepare the current time zone information incase '%z' or '%Z' is encountered. This information can be obtained as given below: A) If timezone is not set ( meaning we are working with system timezone): Get the MYSQL_TIME representation for current time and GMT time using current thread variable for timezone and timezone variable for UTC respectively. This MYSQL_TIME variable will be used to calculate time difference. Also convert current time in second to tm structure to get system timezone information. B) If timezone is set as offset: Get timezone information using current timezone information and store in appropriate variable. C) If timezone is set as some place (example: Europe/Berlin) Get timezone information by searching the timezone. During internal timezone search, information like timeoffset from UTC and abbrevation is stored in another relevant structure. Hence use the same information.
1 parent 5fc19e7 commit 94eb819

10 files changed

+292
-59
lines changed

mysql-test/main/date_formats.result

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,3 +555,49 @@ time_format('01 02:02:02', '%T')
555555
select time_format('2001-01-01 02:02:02', '%T');
556556
time_format('2001-01-01 02:02:02', '%T')
557557
02:02:02
558+
#
559+
# Beginning of 11.3 test
560+
#
561+
# MDEV-31684: Add timezone information to DATE_FORMAT
562+
#
563+
SET @old_timezone= @@time_zone;
564+
# Using named timezones
565+
SET TIME_ZONE='Japan';
566+
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%z %Z') AS current_timezone;
567+
current_timezone
568+
+0900 JST
569+
SET @@time_zone='Europe/Moscow';
570+
SELECT DATE_FORMAT('1965-02-17 22:23:00', '%z %Z') AS current_timezone;
571+
current_timezone
572+
+0300 MSK
573+
SELECT DATE_FORMAT('1965-12-31 22:23:00', '%z %Z') AS current_timezone;
574+
current_timezone
575+
+0300 MSK
576+
SELECT DATE_FORMAT('1985-06-01', '%z %Z');
577+
DATE_FORMAT('1985-06-01', '%z %Z')
578+
+0400 MSD
579+
# Using positive and negative offset
580+
SET TIME_ZONE= '-05:30';
581+
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%z %Z') AS current_timezone;
582+
current_timezone
583+
-0530 -05:30
584+
SET TIME_ZONE= '+04:30';
585+
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%z %Z') AS current_timezone;
586+
current_timezone
587+
+0430 +04:30
588+
# Using UTC
589+
SET TIME_ZONE='UTC';
590+
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%z %Z') AS current_timezone;
591+
current_timezone
592+
+0000 UTC
593+
# using system time
594+
#
595+
# output depends on system time information. Hence replacing
596+
# to avoid result diff test failures.
597+
#
598+
SET @@time_zone= default;
599+
SELECT DATE_FORMAT('2009-10+|-HH:MM ABC22:23:00', '%z %Z') AS current_timezone;
600+
current_timezone
601+
+|-HH:MM ABC
602+
SET @@time_zone= @old_timezone;
603+
# End of 11.3 test

mysql-test/main/date_formats.test

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,51 @@ select time_format('2001-01-01 02:02:02', '%d %T');
256256
select time_format('01 02:02:02', '%d %T');
257257
select time_format('01 02:02:02', '%T');
258258
select time_format('2001-01-01 02:02:02', '%T');
259+
260+
--echo #
261+
--echo # Beginning of 11.3 test
262+
--echo #
263+
--echo # MDEV-31684: Add timezone information to DATE_FORMAT
264+
--echo #
265+
266+
SET @old_timezone= @@time_zone;
267+
268+
269+
--echo # Using named timezones
270+
271+
SET TIME_ZONE='Japan';
272+
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%z %Z') AS current_timezone;
273+
274+
SET @@time_zone='Europe/Moscow';
275+
SELECT DATE_FORMAT('1965-02-17 22:23:00', '%z %Z') AS current_timezone;
276+
SELECT DATE_FORMAT('1965-12-31 22:23:00', '%z %Z') AS current_timezone;
277+
SELECT DATE_FORMAT('1985-06-01', '%z %Z');
278+
279+
--echo # Using positive and negative offset
280+
281+
SET TIME_ZONE= '-05:30';
282+
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%z %Z') AS current_timezone;
283+
284+
SET TIME_ZONE= '+04:30';
285+
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%z %Z') AS current_timezone;
286+
287+
288+
--echo # Using UTC
289+
290+
SET TIME_ZONE='UTC';
291+
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%z %Z') AS current_timezone;
292+
293+
--echo # using system time
294+
--echo #
295+
--echo # output depends on system time information. Hence replacing
296+
--echo # to avoid result diff test failures.
297+
--echo #
298+
299+
SET @@time_zone= default;
300+
301+
--replace_regex /[+-][0-9]* [A-Z]*/+|-HH:MM ABC/
302+
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%z %Z') AS current_timezone;
303+
304+
SET @@time_zone= @old_timezone;
305+
306+
--echo # End of 11.3 test

mysql-test/main/mysqldump-system,win.rdiff

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33
@@ -449,9 +449,9 @@
44
Table Checksum
55
mysql.roles_mapping 2510045525
6-
mysql.time_zone_transition 3895294076
6+
mysql.time_zone_transition 3719776009
77
-mysql.plugin 1587119305
88
+mysql.plugin 2184891911
99
mysql.servers 2079085450
1010
-mysql.func 3241572444
1111
+mysql.func 310494789
12-
mysql.innodb_table_stats 347867921
13-
mysql.table_stats 664320059
12+
mysql.innodb_table_stats 1285726777
13+
mysql.table_stats 2836905944
1414
# Opps....
1515
@@ -484,9 +484,9 @@
1616
Table Checksum
1717
mysql.roles_mapping 2510045525
18-
mysql.time_zone_transition 3895294076
18+
mysql.time_zone_transition 3719776009
1919
-mysql.plugin 1587119305
2020
+mysql.plugin 2184891911
2121
mysql.servers 2079085450
2222
-mysql.func 3241572444
2323
+mysql.func 310494789
24-
mysql.innodb_table_stats 347867921
25-
mysql.table_stats 664320059
24+
mysql.innodb_table_stats 1285726777
25+
mysql.table_stats 2836905944
2626
DROP FUNCTION IF EXISTS metaphon;

0 commit comments

Comments
 (0)