Skip to content

Commit c61a050

Browse files
committed
Debugging function to get current datetime
1 parent 96a8796 commit c61a050

File tree

5 files changed

+116
-41
lines changed

5 files changed

+116
-41
lines changed

queries/datetime-variables-example.xml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<queries maxRows="1000">
3-
<excel db="sampleDB" output="output/datetime_variables_example.xlsx" style="modern">
3+
<excel db="sampleDB" output="d:/temp/datetime-variables-example.xlsx" style="modern">
44
</excel>
55

66
<vars>
77
<var name="reportTitle">시각 변수 사용 예제</var>
88
<var name="department">IT</var>
99
</vars>
1010

11-
<sheets>
1211
<!-- 기본 시각 함수들 -->
1312
<sheet name="기본시각함수" use="true">
1413
<![CDATA[
@@ -70,7 +69,6 @@
7069
'요일: ${WEEKDAY_KR}' as 요일정보,
7170
'파일명 형식: Report_${DATE_YYYYMMDD}_${department}' as 파일명예시,
7271
'WHERE 조건 예시: created_date >= ''${DATE_YYYY_MM_DD}''' as 쿼리예시
73-
]]>
74-
</sheet>
75-
</sheets>
72+
]]>
73+
</sheet>
7674
</queries>

queries/test-datetime-simple.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"excel": {
3+
"db": "sampleDB",
4+
"output": "d:/temp/test-datetime.xlsx"
5+
},
6+
"vars": {
7+
"testVar": "테스트"
8+
},
9+
"sheets": [
10+
{
11+
"name": "DateTimeTest",
12+
"use": true,
13+
"query": "SELECT '${testVar}' as 테스트변수, '${KST_NOW}' as 한국시간, '${KOREAN_DATE}' as 한국날짜, 1 as 숫자"
14+
}
15+
]
16+
}

src/mssql-helper.js

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,58 @@ class MSSQLHelper {
7272
* @returns {Object} 타임스탬프 함수들
7373
*/
7474
getTimestampFunctions() {
75-
const now = new Date();
76-
const koreaTime = new Date(now.getTime() + (9 * 60 * 60 * 1000)); // UTC+9 (한국 시간)
77-
7875
return {
7976
// 기본 시각 함수들
80-
'CURRENT_TIMESTAMP': () => now.toISOString().slice(0, 19).replace('T', ' '), // YYYY-MM-DD HH:mm:ss
81-
'CURRENT_DATETIME': () => now.toISOString().slice(0, 19).replace('T', ' '), // YYYY-MM-DD HH:mm:ss
82-
'NOW': () => now.toISOString().slice(0, 19).replace('T', ' '), // YYYY-MM-DD HH:mm:ss
83-
'CURRENT_DATE': () => now.toISOString().slice(0, 10), // YYYY-MM-DD
84-
'CURRENT_TIME': () => now.toTimeString().slice(0, 8), // HH:mm:ss
85-
'GETDATE': () => now.toISOString().slice(0, 19).replace('T', ' '), // SQL Server GETDATE() equivalent
77+
'CURRENT_TIMESTAMP': () => {
78+
const now = new Date();
79+
return now.toISOString().slice(0, 19).replace('T', ' ');
80+
}, // YYYY-MM-DD HH:mm:ss
81+
'CURRENT_DATETIME': () => {
82+
const now = new Date();
83+
return now.toISOString().slice(0, 19).replace('T', ' ');
84+
}, // YYYY-MM-DD HH:mm:ss
85+
'NOW': () => {
86+
const now = new Date();
87+
return now.toISOString().slice(0, 19).replace('T', ' ');
88+
}, // YYYY-MM-DD HH:mm:ss
89+
'CURRENT_DATE': () => {
90+
const now = new Date();
91+
return now.toISOString().slice(0, 10);
92+
}, // YYYY-MM-DD
93+
'CURRENT_TIME': () => {
94+
const now = new Date();
95+
return now.toTimeString().slice(0, 8);
96+
}, // HH:mm:ss
97+
'GETDATE': () => {
98+
const now = new Date();
99+
return now.toISOString().slice(0, 19).replace('T', ' ');
100+
}, // SQL Server GETDATE() equivalent
86101

87102
// 한국 시간대 함수들
88-
'KST_NOW': () => koreaTime.toISOString().slice(0, 19).replace('T', ' '), // 한국 시간 YYYY-MM-DD HH:mm:ss
89-
'KST_DATE': () => koreaTime.toISOString().slice(0, 10), // 한국 날짜 YYYY-MM-DD
90-
'KST_TIME': () => koreaTime.toISOString().slice(11, 19), // 한국 시간 HH:mm:ss
91-
'KST_DATETIME': () => koreaTime.toISOString().slice(0, 19).replace('T', ' '), // 한국 날짜시간
103+
'KST_NOW': () => {
104+
const now = new Date();
105+
const koreaTime = new Date(now.getTime() + (9 * 60 * 60 * 1000));
106+
return koreaTime.toISOString().slice(0, 19).replace('T', ' ');
107+
}, // 한국 시간 YYYY-MM-DD HH:mm:ss
108+
'KST_DATE': () => {
109+
const now = new Date();
110+
const koreaTime = new Date(now.getTime() + (9 * 60 * 60 * 1000));
111+
return koreaTime.toISOString().slice(0, 10);
112+
}, // 한국 날짜 YYYY-MM-DD
113+
'KST_TIME': () => {
114+
const now = new Date();
115+
const koreaTime = new Date(now.getTime() + (9 * 60 * 60 * 1000));
116+
return koreaTime.toISOString().slice(11, 19);
117+
}, // 한국 시간 HH:mm:ss
118+
'KST_DATETIME': () => {
119+
const now = new Date();
120+
const koreaTime = new Date(now.getTime() + (9 * 60 * 60 * 1000));
121+
return koreaTime.toISOString().slice(0, 19).replace('T', ' ');
122+
}, // 한국 날짜시간
92123

93124
// 한국식 날짜 형식
94125
'KOREAN_DATE': () => {
126+
const now = new Date();
95127
const kst = new Date(now.getTime() + (9 * 60 * 60 * 1000));
96128
return kst.toLocaleDateString('ko-KR', {
97129
year: 'numeric',
@@ -100,6 +132,7 @@ class MSSQLHelper {
100132
});
101133
}, // YYYY년 M월 D일
102134
'KOREAN_DATETIME': () => {
135+
const now = new Date();
103136
const kst = new Date(now.getTime() + (9 * 60 * 60 * 1000));
104137
return kst.toLocaleString('ko-KR', {
105138
year: 'numeric',
@@ -112,6 +145,7 @@ class MSSQLHelper {
112145
});
113146
}, // YYYY년 M월 D일 HH:mm:ss
114147
'KOREAN_DATE_SHORT': () => {
148+
const now = new Date();
115149
const kst = new Date(now.getTime() + (9 * 60 * 60 * 1000));
116150
return kst.toLocaleDateString('ko-KR', {
117151
year: 'numeric',
@@ -122,20 +156,23 @@ class MSSQLHelper {
122156

123157
// 다양한 형식들
124158
'DATE_YYYYMMDD': () => {
159+
const now = new Date();
125160
const kst = new Date(now.getTime() + (9 * 60 * 60 * 1000));
126161
const year = kst.getFullYear();
127162
const month = String(kst.getMonth() + 1).padStart(2, '0');
128163
const day = String(kst.getDate()).padStart(2, '0');
129164
return `${year}${month}${day}`;
130165
}, // YYYYMMDD
131166
'DATE_YYYY_MM_DD': () => {
167+
const now = new Date();
132168
const kst = new Date(now.getTime() + (9 * 60 * 60 * 1000));
133169
const year = kst.getFullYear();
134170
const month = String(kst.getMonth() + 1).padStart(2, '0');
135171
const day = String(kst.getDate()).padStart(2, '0');
136172
return `${year}-${month}-${day}`;
137173
}, // YYYY-MM-DD (한국 시간)
138174
'DATETIME_YYYYMMDD_HHMMSS': () => {
175+
const now = new Date();
139176
const kst = new Date(now.getTime() + (9 * 60 * 60 * 1000));
140177
const year = kst.getFullYear();
141178
const month = String(kst.getMonth() + 1).padStart(2, '0');
@@ -149,27 +186,38 @@ class MSSQLHelper {
149186
// 타임스탬프 함수들
150187
'UNIX_TIMESTAMP': () => Math.floor(Date.now() / 1000), // Unix timestamp
151188
'TIMESTAMP_MS': () => Date.now(), // Milliseconds timestamp
152-
'ISO_TIMESTAMP': () => now.toISOString(), // ISO 8601 format
153-
'KST_ISO_TIMESTAMP': () => koreaTime.toISOString(), // 한국 시간 ISO 8601
189+
'ISO_TIMESTAMP': () => {
190+
const now = new Date();
191+
return now.toISOString();
192+
}, // ISO 8601 format
193+
'KST_ISO_TIMESTAMP': () => {
194+
const now = new Date();
195+
const koreaTime = new Date(now.getTime() + (9 * 60 * 60 * 1000));
196+
return koreaTime.toISOString();
197+
}, // 한국 시간 ISO 8601
154198

155199
// 요일 정보
156200
'WEEKDAY_KR': () => {
201+
const now = new Date();
157202
const kst = new Date(now.getTime() + (9 * 60 * 60 * 1000));
158203
const weekdays = ['일요일', '월요일', '화요일', '수요일', '목요일', '금요일', '토요일'];
159204
return weekdays[kst.getDay()];
160205
}, // 한국어 요일
161206
'WEEKDAY_EN': () => {
207+
const now = new Date();
162208
const kst = new Date(now.getTime() + (9 * 60 * 60 * 1000));
163209
const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
164210
return weekdays[kst.getDay()];
165211
}, // 영어 요일
166212

167213
// 월 정보
168214
'MONTH_KR': () => {
215+
const now = new Date();
169216
const kst = new Date(now.getTime() + (9 * 60 * 60 * 1000));
170217
return `${kst.getMonth() + 1}월`;
171218
}, // N월
172219
'YEAR_KR': () => {
220+
const now = new Date();
173221
const kst = new Date(now.getTime() + (9 * 60 * 60 * 1000));
174222
return `${kst.getFullYear()}년`;
175223
} // YYYY년

src/variable-processor.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,27 @@ class VariableProcessor {
205205
}
206206
});
207207

208-
// 일반 변수 치환 (기존 방식)
208+
// 현재 시각 함수 치환 (MSSQL 헬퍼 사용) - 일반 변수보다 먼저 처리
209+
const timestampFunctions = this.mssqlHelper.getTimestampFunctions();
210+
211+
// 현재 시각 함수 패턴 매칭 및 치환
212+
Object.entries(timestampFunctions).forEach(([funcName, funcImpl]) => {
213+
const pattern = new RegExp(`\\$\\{${funcName}\\}`, 'g');
214+
const beforeReplace = result;
215+
216+
try {
217+
result = result.replace(pattern, funcImpl());
218+
219+
if (debugVariables && beforeReplace !== result) {
220+
console.log(`시각 함수 [${funcName}] 치환: ${funcImpl()}`);
221+
}
222+
} catch (error) {
223+
console.log(`시각 함수 [${funcName}] 치환 중 오류: ${error.message}`);
224+
// 오류 발생 시 원본 유지
225+
}
226+
});
227+
228+
// 일반 변수 치환 (기존 방식) - 시각 함수 치환 후 처리
209229
result = result.replace(/\$\{(\w+)\}/g, (_, v) => {
210230
const value = mergedVars[v];
211231
if (value === undefined || value === null) return '';
@@ -227,26 +247,6 @@ class VariableProcessor {
227247
}
228248
});
229249

230-
// 현재 시각 함수 치환 (MSSQL 헬퍼 사용)
231-
const timestampFunctions = this.mssqlHelper.getTimestampFunctions();
232-
233-
// 현재 시각 함수 패턴 매칭 및 치환
234-
Object.entries(timestampFunctions).forEach(([funcName, funcImpl]) => {
235-
const pattern = new RegExp(`\\$\\{${funcName}\\}`, 'g');
236-
const beforeReplace = result;
237-
238-
try {
239-
result = result.replace(pattern, funcImpl());
240-
241-
if (debugVariables && beforeReplace !== result) {
242-
console.log(`시각 함수 [${funcName}] 치환: ${funcImpl()}`);
243-
}
244-
} catch (error) {
245-
console.log(`시각 함수 [${funcName}] 치환 중 오류: ${error.message}`);
246-
// 오류 발생 시 원본 유지
247-
}
248-
});
249-
250250
// 환경 변수 치환
251251
const envPattern = /\$\{(\w+)\}/g;
252252
const remainingMatches = [...result.matchAll(envPattern)];

test-final-datetime.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"excel": {
3+
"db": "sampleDB",
4+
"output": "d:/temp/final-datetime-test.xlsx"
5+
},
6+
"sheets": [
7+
{
8+
"name": "DateTimeTest",
9+
"use": true,
10+
"query": "SELECT 'Test' as message, '${KST_NOW}' as korean_time, '${KOREAN_DATE}' as korean_date, '${DATE_YYYYMMDD}' as number_date, 123 as test_number"
11+
}
12+
]
13+
}

0 commit comments

Comments
 (0)