From cd371110adaf23eea7dfcceda85947e52fda2bd1 Mon Sep 17 00:00:00 2001 From: Cory Levy Date: Mon, 29 Aug 2022 20:40:11 -0400 Subject: [PATCH] Fix timezone offset formatting --- src/Functions/formatDateTime.cpp | 7 +++++-- tests/queries/0_stateless/00718_format_datetime.sql | 9 +++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Functions/formatDateTime.cpp b/src/Functions/formatDateTime.cpp index 5c4e4bfdfe4..09071c5c1a0 100644 --- a/src/Functions/formatDateTime.cpp +++ b/src/Functions/formatDateTime.cpp @@ -276,10 +276,13 @@ private: { auto offset = TimezoneOffsetImpl::execute(source, timezone); if (offset < 0) + { *target = '-'; + offset = -offset; + } - writeNumber2(target + 1, ToHourImpl::execute(offset, timezone)); - writeNumber2(target + 3, ToSecondImpl::execute(offset, timezone)); + writeNumber2(target + 1, offset / 3600); + writeNumber2(target + 3, offset % 3600 / 60); } static void quarter(char * target, Time source, const DateLUTImpl & timezone) diff --git a/tests/queries/0_stateless/00718_format_datetime.sql b/tests/queries/0_stateless/00718_format_datetime.sql index 2c008b14a73..5cc3ff533e2 100644 --- a/tests/queries/0_stateless/00718_format_datetime.sql +++ b/tests/queries/0_stateless/00718_format_datetime.sql @@ -42,8 +42,9 @@ SELECT formatDateTime(toDateTime('2018-01-02 22:33:44'), 'no formatting pattern' SELECT formatDateTime(toDate('2018-01-01'), '%F %T'); SELECT formatDateTime(toDateTime('2018-01-01 01:00:00', 'UTC'), '%F %T', 'UTC'), - formatDateTime(toDateTime('2018-01-01 01:00:00', 'UTC'), '%F %T', 'Asia/Istanbul') + formatDateTime(toDateTime('2018-01-01 01:00:00', 'UTC'), '%F %T', 'Asia/Istanbul'); -SELECT formatDateTime(toDateTime('2020-01-01 01:00:00', 'UTC'), '%z') -SELECT formatDateTime(toDateTime('2020-01-01 01:00:00', 'US/Samoa'), '%z') -SELECT formatDateTime(toDateTime('2020-01-01 01:00:00', 'Europe/Moscow'), '%z') +SELECT formatDateTime(toDateTime('2020-01-01 01:00:00', 'UTC'), '%z'); +SELECT formatDateTime(toDateTime('2020-01-01 01:00:00', 'US/Samoa'), '%z'); +SELECT formatDateTime(toDateTime('2020-01-01 01:00:00', 'Europe/Moscow'), '%z'); +SELECT formatDateTime(toDateTime('1970-01-01 00:00:00', 'IST'), '%z');