Added standalone function to get server's own timezone

Fix missing global_context
This commit is contained in:
zvonand 2023-02-23 13:38:13 +01:00
parent 393830ecdc
commit c61aff7cac
2 changed files with 27 additions and 2 deletions

View File

@ -42,6 +42,16 @@ public:
if (!implicit_timezone.empty()) if (!implicit_timezone.empty())
return instance(implicit_timezone); return instance(implicit_timezone);
} }
const auto global_context = DB::CurrentThread::get().getGlobalContext();
if (global_context)
{
auto implicit_timezone = extractTimezoneFromContext(global_context);
if (!implicit_timezone.empty())
return instance(implicit_timezone);
}
} }
return *date_lut.default_impl.load(std::memory_order_acquire); return *date_lut.default_impl.load(std::memory_order_acquire);
} }

View File

@ -60,13 +60,22 @@ namespace
}; };
/// Returns the server time zone. /// Returns default timezone for current session.
class FunctionTimezone : public FunctionConstantBase<FunctionTimezone, String, DataTypeString> class FunctionTimezone : public FunctionConstantBase<FunctionTimezone, String, DataTypeString>
{ {
public: public:
static constexpr auto name = "timezone"; static constexpr auto name = "timezone";
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionTimezone>(context); } static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionTimezone>(context); }
explicit FunctionTimezone(ContextPtr context) : FunctionConstantBase(String{DateLUT::instance().getTimeZone()}, context->isDistributed()) {} explicit FunctionTimezone(ContextPtr context) : FunctionConstantBase(String{DateLUT::instance("").getTimeZone()}, context->isDistributed()) {}
};
/// Returns the server time zone (timezone in which server runs).
class FunctionServerTimezone : public FunctionConstantBase<FunctionServerTimezone, String, DataTypeString>
{
public:
static constexpr auto name = "serverTimezone";
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionServerTimezone>(context); }
explicit FunctionServerTimezone(ContextPtr context) : FunctionConstantBase(String{DateLUT::instance().getTimeZone()}, context->isDistributed()) {}
}; };
@ -155,6 +164,12 @@ REGISTER_FUNCTION(Timezone)
factory.registerAlias("timeZone", "timezone"); factory.registerAlias("timeZone", "timezone");
} }
REGISTER_FUNCTION(ServerTimezone)
{
factory.registerFunction<FunctionServerTimezone>();
factory.registerAlias("serverTimeZone", "serverTimezone");
}
REGISTER_FUNCTION(Uptime) REGISTER_FUNCTION(Uptime)
{ {
factory.registerFunction<FunctionUptime>(); factory.registerFunction<FunctionUptime>();