Skip to content

Commit

Permalink
MDEV-31684 Add timezone information to DATE_FORMAT
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mariadb-RuchaDeodhar committed Oct 11, 2023
1 parent 5fc19e7 commit 94eb819
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 59 deletions.
46 changes: 46 additions & 0 deletions mysql-test/main/date_formats.result
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,49 @@ time_format('01 02:02:02', '%T')
select time_format('2001-01-01 02:02:02', '%T');
time_format('2001-01-01 02:02:02', '%T')
02:02:02
#
# Beginning of 11.3 test
#
# MDEV-31684: Add timezone information to DATE_FORMAT
#
SET @old_timezone= @@time_zone;
# Using named timezones
SET TIME_ZONE='Japan';
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%z %Z') AS current_timezone;
current_timezone
+0900 JST
SET @@time_zone='Europe/Moscow';
SELECT DATE_FORMAT('1965-02-17 22:23:00', '%z %Z') AS current_timezone;
current_timezone
+0300 MSK
SELECT DATE_FORMAT('1965-12-31 22:23:00', '%z %Z') AS current_timezone;
current_timezone
+0300 MSK
SELECT DATE_FORMAT('1985-06-01', '%z %Z');
DATE_FORMAT('1985-06-01', '%z %Z')
+0400 MSD
# Using positive and negative offset
SET TIME_ZONE= '-05:30';
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%z %Z') AS current_timezone;
current_timezone
-0530 -05:30
SET TIME_ZONE= '+04:30';
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%z %Z') AS current_timezone;
current_timezone
+0430 +04:30
# Using UTC
SET TIME_ZONE='UTC';
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%z %Z') AS current_timezone;
current_timezone
+0000 UTC
# using system time
#
# output depends on system time information. Hence replacing
# to avoid result diff test failures.
#
SET @@time_zone= default;
SELECT DATE_FORMAT('2009-10+|-HH:MM ABC22:23:00', '%z %Z') AS current_timezone;
current_timezone
+|-HH:MM ABC
SET @@time_zone= @old_timezone;
# End of 11.3 test
48 changes: 48 additions & 0 deletions mysql-test/main/date_formats.test
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,51 @@ select time_format('2001-01-01 02:02:02', '%d %T');
select time_format('01 02:02:02', '%d %T');
select time_format('01 02:02:02', '%T');
select time_format('2001-01-01 02:02:02', '%T');

--echo #
--echo # Beginning of 11.3 test
--echo #
--echo # MDEV-31684: Add timezone information to DATE_FORMAT
--echo #

SET @old_timezone= @@time_zone;


--echo # Using named timezones

SET TIME_ZONE='Japan';
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%z %Z') AS current_timezone;

SET @@time_zone='Europe/Moscow';
SELECT DATE_FORMAT('1965-02-17 22:23:00', '%z %Z') AS current_timezone;
SELECT DATE_FORMAT('1965-12-31 22:23:00', '%z %Z') AS current_timezone;
SELECT DATE_FORMAT('1985-06-01', '%z %Z');

--echo # Using positive and negative offset

SET TIME_ZONE= '-05:30';
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%z %Z') AS current_timezone;

SET TIME_ZONE= '+04:30';
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%z %Z') AS current_timezone;


--echo # Using UTC

SET TIME_ZONE='UTC';
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%z %Z') AS current_timezone;

--echo # using system time
--echo #
--echo # output depends on system time information. Hence replacing
--echo # to avoid result diff test failures.
--echo #

SET @@time_zone= default;

--replace_regex /[+-][0-9]* [A-Z]*/+|-HH:MM ABC/
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%z %Z') AS current_timezone;

SET @@time_zone= @old_timezone;

--echo # End of 11.3 test
12 changes: 6 additions & 6 deletions mysql-test/main/mysqldump-system,win.rdiff
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
@@ -449,9 +449,9 @@
Table Checksum
mysql.roles_mapping 2510045525
mysql.time_zone_transition 3895294076
mysql.time_zone_transition 3719776009
-mysql.plugin 1587119305
+mysql.plugin 2184891911
mysql.servers 2079085450
-mysql.func 3241572444
+mysql.func 310494789
mysql.innodb_table_stats 347867921
mysql.table_stats 664320059
mysql.innodb_table_stats 1285726777
mysql.table_stats 2836905944
# Opps....
@@ -484,9 +484,9 @@
Table Checksum
mysql.roles_mapping 2510045525
mysql.time_zone_transition 3895294076
mysql.time_zone_transition 3719776009
-mysql.plugin 1587119305
+mysql.plugin 2184891911
mysql.servers 2079085450
-mysql.func 3241572444
+mysql.func 310494789
mysql.innodb_table_stats 347867921
mysql.table_stats 664320059
mysql.innodb_table_stats 1285726777
mysql.table_stats 2836905944
DROP FUNCTION IF EXISTS metaphon;

0 comments on commit 94eb819

Please sign in to comment.