1#pragma once
2
3#include <Core/Types.h>
4#include <DataTypes/DataTypeNumberBase.h>
5
6class DateLUTImpl;
7
8namespace DB
9{
10
11/** Mixin-class that manages timezone info for timezone-aware DateTime implementations
12 *
13 * Must be used as a (second) base for class implementing IDateType-interface.
14 */
15class TimezoneMixin
16{
17public:
18 explicit TimezoneMixin(const String & time_zone_name = "");
19 TimezoneMixin(const TimezoneMixin &) = default;
20
21 const DateLUTImpl & getTimeZone() const { return time_zone; }
22
23protected:
24 bool has_explicit_time_zone;
25 const DateLUTImpl & time_zone;
26 const DateLUTImpl & utc_time_zone;
27};
28
29/** DateTime stores time as unix timestamp.
30 * The value itself is independent of time zone.
31 *
32 * In binary format it is represented as unix timestamp.
33 * In text format it is serialized to and parsed from YYYY-MM-DD hh:mm:ss format.
34 * The text format is dependent of time zone.
35 *
36 * To cast from/to text format, time zone may be specified explicitly or implicit time zone may be used.
37 *
38 * Time zone may be specified explicitly as type parameter, example: DateTime('Europe/Moscow').
39 * As it does not affect the internal representation of values,
40 * all types with different time zones are equivalent and may be used interchangingly.
41 * Time zone only affects parsing and displaying in text formats.
42 *
43 * If time zone is not specified (example: DateTime without parameter), then default time zone is used.
44 * Default time zone is server time zone, if server is doing transformations
45 * and if client is doing transformations, unless 'use_client_time_zone' setting is passed to client;
46 * Server time zone is the time zone specified in 'timezone' parameter in configuration file,
47 * or system time zone at the moment of server startup.
48 */
49class DataTypeDateTime final : public DataTypeNumberBase<UInt32>, public TimezoneMixin
50{
51public:
52 explicit DataTypeDateTime(const String & time_zone_name = "", const String & type_name_ = "DateTime");
53 explicit DataTypeDateTime(const TimezoneMixin & time_zone);
54
55 static constexpr auto family_name = "DateTime";
56
57 const char * getFamilyName() const override { return family_name; }
58 String doGetName() const override;
59 TypeIndex getTypeId() const override { return TypeIndex::DateTime; }
60
61 void serializeText(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override;
62 void deserializeWholeText(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const override;
63 void serializeTextEscaped(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override;
64 void deserializeTextEscaped(IColumn & column, ReadBuffer & istr, const FormatSettings &) const override;
65 void serializeTextQuoted(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override;
66 void deserializeTextQuoted(IColumn & column, ReadBuffer & istr, const FormatSettings &) const override;
67 void serializeTextJSON(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override;
68 void deserializeTextJSON(IColumn & column, ReadBuffer & istr, const FormatSettings &) const override;
69 void serializeTextCSV(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override;
70 void deserializeTextCSV(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const override;
71 void serializeProtobuf(const IColumn & column, size_t row_num, ProtobufWriter & protobuf, size_t & value_index) const override;
72 void deserializeProtobuf(IColumn & column, ProtobufReader & protobuf, bool allow_add_row, bool & row_added) const override;
73
74 bool canBeUsedAsVersion() const override { return true; }
75 bool canBeInsideNullable() const override { return true; }
76
77 bool equals(const IDataType & rhs) const override;
78private:
79 const String type_name;
80};
81
82}
83
84