Skip to content

Commit

Permalink
support display of timezone for datetime and datetime64
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasmine-ge committed Sep 27, 2024
1 parent c01bc3f commit ecde553
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/DataTypes/Serializations/SerializationDateTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ void SerializationDateTime::serializeText(const IColumn & column, size_t row_num
switch (settings.date_time_output_format)
{
case FormatSettings::DateTimeOutputFormat::Simple:
writeDateTimeText(value, ostr, time_zone);
if (has_explicit_time_zone)
writeDateTimeTextWIthZone(value, ostr, time_zone);
else
writeDateTimeText(value, ostr, time_zone);
return;
case FormatSettings::DateTimeOutputFormat::UnixTimestamp:
writeIntText(value, ostr);
Expand Down
5 changes: 4 additions & 1 deletion src/DataTypes/Serializations/SerializationDateTime64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ void SerializationDateTime64::serializeText(const IColumn & column, size_t row_n
switch (settings.date_time_output_format)
{
case FormatSettings::DateTimeOutputFormat::Simple:
writeDateTimeText(value, scale, ostr, time_zone);
if (has_explicit_time_zone)
writeDateTimeTextWIthZone(value, scale, ostr, time_zone);
else
writeDateTimeText(value, scale, ostr, time_zone);
return;
case FormatSettings::DateTimeOutputFormat::UnixTimestamp:
writeDateTimeUnixTimestamp(value, scale, ostr);
Expand Down
27 changes: 27 additions & 0 deletions src/IO/WriteHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,19 @@ inline void writeDateTimeText(time_t datetime, WriteBuffer & buf, const DateLUTI
writeDateTimeText<date_delimeter, time_delimeter, between_date_time_delimiter>(LocalDateTime(datetime, time_zone), buf);
}

/// In the format YYYY-MM-DD HH:MM:SS+time zone, according to the specified time zone.
template <char date_delimeter = '-', char time_delimeter = ':', char between_date_time_delimiter = ' '>
inline void writeDateTimeTextWIthZone(time_t datetime, WriteBuffer & buf, const DateLUTImpl & time_zone = DateLUT::instance())
{
writeDateTimeText<date_delimeter, time_delimeter, between_date_time_delimiter>(LocalDateTime(datetime, time_zone), buf);
int hour = time_zone.timezoneOffset(datetime) / 3600;
buf.write(hour >= 0 ? '+' : '-');
int abs_hour = std::abs(hour);
buf.write(&digits100[abs_hour * 2], 2);
buf.write(':');
buf.write(&digits100[0], 2);
}

/// In the format YYYY-MM-DD HH:MM:SS.NNNNNNNNN, according to the specified time zone.
template <char date_delimeter = '-', char time_delimeter = ':', char between_date_time_delimiter = ' ', char fractional_time_delimiter = '.'>
inline void writeDateTimeText(DateTime64 datetime64, UInt32 scale, WriteBuffer & buf, const DateLUTImpl & time_zone = DateLUT::instance())
Expand Down Expand Up @@ -794,6 +807,20 @@ inline void writeDateTimeText(DateTime64 datetime64, UInt32 scale, WriteBuffer &
}
}


/// In the format YYYY-MM-DD HH:MM:SS.NNNNNNNNN+time zone, according to the specified time zone.
template <char date_delimeter = '-', char time_delimeter = ':', char between_date_time_delimiter = ' '>
inline void writeDateTimeTextWIthZone(DateTime64 datetime64, UInt32 scale, WriteBuffer & buf, const DateLUTImpl & time_zone = DateLUT::instance())
{
writeDateTimeText(datetime64, scale, buf, time_zone);
int hour = time_zone.timezoneOffset(datetime64) / 3600;
buf.write(hour >= 0 ? '+' : '-');
int abs_hour = std::abs(hour);
buf.write(&digits100[abs_hour * 2], 2);
buf.write(':');
buf.write(&digits100[0], 2);
}

/// In the RFC 1123 format: "Tue, 03 Dec 2019 00:11:50 GMT". You must provide GMT DateLUT.
/// This is needed for HTTP requests.
inline void writeDateTimeTextRFC1123(time_t datetime, WriteBuffer & buf, const DateLUTImpl & time_zone = DateLUT::instance())
Expand Down
70 changes: 70 additions & 0 deletions tests/stream/test_stream_smoke/0098_fixed_issues2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,73 @@ tests:
- query_id: fixed-issues2-1
expected_results:
- [2, '2024-05-16 21:32:00+00:00', 1]

- id: 4
tags:
- stateless_function
name: issue 835
description: display timezone information for datetime
steps:
- statements:
- client: python
query_type: table
query: drop stream if exists fixed_issues2_stream;

- client: python
query_type: table
exists: fixed_issues2_stream
exists_wait: 2
wait: 1
query: create stream fixed_issues2_stream(t1 datetime('Europe/London'), t2 datetime, t3 datetime('Australia/Sydney'));

- client: python
query_type: table
depends_on_stream: fixed_issues2_stream
wait: 2
query: insert into fixed_issues2_stream (t1, t2, t3) values (to_time('6/9/2024 13:28','Europe/London'), to_time('6/9/2024 13:28','Europe/London'), to_time('6/9/2024 13:28','Europe/London'));

- client: python
query_id: fixed-issues2-1
query_type: table
wait: 2
query: select t1, t2, t3 from table(fixed_issues2_stream);

expected_results:
- query_id: fixed-issues2-1
expected_results:
- ['2024-06-09 13:28:00+01:00', '2024-06-09 20:28:00', '2024-06-09 22:28:00+10:00']

- id: 5
tags:
- stateless_function
name: issue 835
description: display timezone information for datetime
steps:
- statements:
- client: python
query_type: table
query: drop stream if exists fixed_issues2_stream;

- client: python
query_type: table
exists: fixed_issues2_stream
exists_wait: 2
wait: 1
query: create stream fixed_issues2_stream(t1 datetime64(3, 'Europe/London'), t2 datetime64(3, 'America/Vancouver'), t3 datetime64(3));

- client: python
query_type: table
depends_on_stream: fixed_issues2_stream
wait: 2
query: insert into fixed_issues2_stream (t1, t2, t3) values (to_time('6/9/2024 13:28','Europe/London'), to_time('6/9/2024 13:28','Europe/London'), to_time('6/9/2024 13:28','Europe/London'));

- client: python
query_id: fixed-issues2-1
query_type: table
wait: 2
query: select t1, t2, t3 from table(fixed_issues2_stream);

expected_results:
- query_id: fixed-issues2-1
expected_results:
- ['2024-06-09 13:28:00+01:00', '2024-06-09 05:28:00-07:00', '2024-06-09 20:28:00']

0 comments on commit ecde553

Please sign in to comment.