1
+ // Copyright(C) 2025 InfiniFlow, Inc. All rights reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // https://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ module;
15
+ #include < chrono>
16
+ module current_timestamp;
17
+ import stl;
18
+ import catalog;
19
+ import status;
20
+ import logical_type;
21
+ import infinity_exception;
22
+ import scalar_function;
23
+ import scalar_function_set;
24
+ import third_party;
25
+ import internal_types;
26
+ import data_type;
27
+ import column_vector;
28
+
29
+ namespace infinity {
30
+ using namespace std ::chrono;
31
+ struct CurrentTimestampFunction {
32
+ const char * defaultTZ = " Asia/Shanghai" ;
33
+ template <typename TA, typename TB>
34
+ static inline void Run (TA &left, TB &result) {
35
+ Status status = Status::NotSupport (" Not implemented" );
36
+ RecoverableError (status);
37
+ }
38
+ static inline void TimeZoneConvertHelper (VarcharT &left) {
39
+ const char * tzValue = std::getenv (" TZ" );
40
+ const std::string str = left.ToString ();
41
+ const char * newTZ = str.c_str ();
42
+ if ( tzValue == newTZ) {
43
+ return ;
44
+ }
45
+ if (setenv (" TZ" , newTZ, 1 ) != 0 ) {
46
+ const char * newTZ = " Asia/Shanghai" ;
47
+ setenv (" TZ" , newTZ, 1 );
48
+ }
49
+ tzset ();
50
+ return ;
51
+ }
52
+
53
+ static inline void TimeZoneResetHelper () {
54
+ const char * tzValue = std::getenv (" TZ" );
55
+ if (tzValue == CurrentTimestampFunction ().defaultTZ ) {
56
+ return ;
57
+ }
58
+ setenv (" TZ" , CurrentTimestampFunction ().defaultTZ , 1 );
59
+ tzset ();
60
+ return ;
61
+ }
62
+ };
63
+
64
+ template <>
65
+ inline void CurrentTimestampFunction::Run (VarcharT &left, TimestampT &result) {
66
+ TimeZoneConvertHelper (left);
67
+ auto now = system_clock::now ();
68
+ auto sys_days = std::chrono::floor <std::chrono::days>(now);
69
+ auto sys_secs = std::chrono::floor <std::chrono::seconds>(now);
70
+ result.time .value = static_cast <i32>(sys_secs.time_since_epoch ().count () - sys_days.time_since_epoch ().count ());
71
+ result.date .value = static_cast <i32>(sys_days.time_since_epoch ().count ());
72
+ TimeZoneResetHelper ();
73
+ }
74
+
75
+ void RegisterCurrentTimestampFunction (const UniquePtr<Catalog> &catalog_ptr) {
76
+ String func_name = " currenttimestamp" ;
77
+
78
+ SharedPtr<ScalarFunctionSet> function_set_ptr = MakeShared<ScalarFunctionSet>(func_name);
79
+
80
+ ScalarFunction current_timestamp_function (func_name,
81
+ {DataType (LogicalType::kVarchar )},
82
+ DataType (LogicalType::kTimestamp ),
83
+ &ScalarFunction::UnaryFunction<VarcharT, TimestampT, CurrentTimestampFunction>);
84
+ function_set_ptr->AddFunction (current_timestamp_function);
85
+
86
+ Catalog::AddFunctionSet (catalog_ptr.get (), function_set_ptr);
87
+ }
88
+
89
+ } // namespace infinity
0 commit comments