Skip to content

Commit 96a8796

Browse files
committed
Addition function to get current datetime
1 parent d567789 commit 96a8796

File tree

4 files changed

+335
-7
lines changed

4 files changed

+335
-7
lines changed

USER_MANUAL.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
- [Installation and Setup](#installation-and-setup)
66
- [Basic Usage](#basic-usage)
77
- [Query Definition File Structure](#query-definition-file-structure)
8+
- [Enhanced Dynamic Variables System](#enhanced-dynamic-variables-system)
9+
- [Automatic DateTime Variables](#automatic-datetime-variables)
810
- [Advanced Features](#advanced-features)
911
- [Template Style System](#template-style-system)
1012
- [Building and Deployment](#building-and-deployment)
@@ -409,6 +411,127 @@ WHERE CustomerID IN (${customerData.CustomerID})
409411
4. **Performance**: Variables are executed once and cached for the entire export
410412
5. **Debug Mode**: Enable with `DEBUG_VARIABLES=true` for detailed variable substitution
411413

414+
## 🕒 Automatic DateTime Variables
415+
416+
SQL2Excel provides built-in datetime variables that are automatically resolved to current time values. These variables can be used in queries, file names, and any text content.
417+
418+
### Basic DateTime Functions
419+
420+
| Variable | Description | Example Output |
421+
|----------|-------------|----------------|
422+
| `${CURRENT_TIMESTAMP}` | Current UTC timestamp | `2024-10-05 15:30:45` |
423+
| `${NOW}` | Current UTC timestamp | `2024-10-05 15:30:45` |
424+
| `${CURRENT_DATE}` | Current UTC date | `2024-10-05` |
425+
| `${CURRENT_TIME}` | Current UTC time | `15:30:45` |
426+
| `${GETDATE}` | SQL Server format | `2024-10-05 15:30:45` |
427+
428+
### Korean Time Zone Functions
429+
430+
| Variable | Description | Example Output |
431+
|----------|-------------|----------------|
432+
| `${KST_NOW}` | Korean Standard Time | `2024-10-06 00:30:45` |
433+
| `${KST_DATE}` | Korean date | `2024-10-06` |
434+
| `${KST_TIME}` | Korean time | `00:30:45` |
435+
| `${KST_DATETIME}` | Korean datetime | `2024-10-06 00:30:45` |
436+
437+
### Korean Localized Formats
438+
439+
| Variable | Description | Example Output |
440+
|----------|-------------|----------------|
441+
| `${KOREAN_DATE}` | Korean date format | `2024년 10월 6일` |
442+
| `${KOREAN_DATETIME}` | Korean datetime format | `2024년 10월 6일 00:30:45` |
443+
| `${KOREAN_DATE_SHORT}` | Short Korean date | `2024. 10. 06.` |
444+
| `${WEEKDAY_KR}` | Korean weekday | `일요일` |
445+
| `${MONTH_KR}` | Korean month | `10월` |
446+
| `${YEAR_KR}` | Korean year | `2024년` |
447+
448+
### Formatted Date/Time Functions
449+
450+
| Variable | Description | Example Output |
451+
|----------|-------------|----------------|
452+
| `${DATE_YYYYMMDD}` | Compact date format | `20241006` |
453+
| `${DATE_YYYY_MM_DD}` | Hyphenated date (KST) | `2024-10-06` |
454+
| `${DATETIME_YYYYMMDD_HHMMSS}` | Compact datetime | `20241006_003045` |
455+
| `${WEEKDAY_EN}` | English weekday | `Sunday` |
456+
457+
### Timestamp Functions
458+
459+
| Variable | Description | Example Output |
460+
|----------|-------------|----------------|
461+
| `${UNIX_TIMESTAMP}` | Unix timestamp | `1728140445` |
462+
| `${TIMESTAMP_MS}` | Milliseconds timestamp | `1728140445123` |
463+
| `${ISO_TIMESTAMP}` | ISO 8601 format | `2024-10-05T15:30:45.123Z` |
464+
| `${KST_ISO_TIMESTAMP}` | Korean ISO format | `2024-10-06T00:30:45.123Z` |
465+
466+
### Usage Examples
467+
468+
#### In XML Queries
469+
```xml
470+
<vars>
471+
<var name="reportDate">${KOREAN_DATE}</var>
472+
<var name="department">IT</var>
473+
</vars>
474+
475+
<sheets>
476+
<sheet name="DailyReport" use="true">
477+
<![CDATA[
478+
SELECT
479+
'${reportDate} 일일 리포트' as title,
480+
'${KST_NOW}' as generated_at,
481+
* FROM orders
482+
WHERE created_date >= '${DATE_YYYY_MM_DD}'
483+
AND department = '${department}'
484+
]]>
485+
</sheet>
486+
</sheets>
487+
```
488+
489+
#### In JSON Queries
490+
```json
491+
{
492+
"vars": {
493+
"reportTitle": "Daily Report - ${KOREAN_DATE}",
494+
"currentTime": "${KST_NOW}"
495+
},
496+
"sheets": [
497+
{
498+
"name": "Report_${DATE_YYYYMMDD}",
499+
"query": "SELECT '${reportTitle}' as title, '${currentTime}' as generated_at FROM users"
500+
}
501+
]
502+
}
503+
```
504+
505+
#### For File Naming
506+
```xml
507+
<excel db="sampleDB" output="output/report_${DATE_YYYYMMDD}_${DATETIME_YYYYMMDD_HHMMSS}.xlsx">
508+
```
509+
510+
#### In Query Conditions
511+
```sql
512+
-- Filter records from today (Korean time)
513+
SELECT * FROM orders
514+
WHERE order_date >= '${DATE_YYYY_MM_DD} 00:00:00'
515+
AND order_date < '${DATE_YYYY_MM_DD} 23:59:59'
516+
517+
-- Create backup table with timestamp
518+
CREATE TABLE backup_orders_${DATE_YYYYMMDD} AS
519+
SELECT * FROM orders WHERE created_at < '${KST_NOW}'
520+
```
521+
522+
### Debug Mode
523+
Enable debug mode to see datetime variable substitution:
524+
```bash
525+
DEBUG_VARIABLES=true node src/excel-cli.js export --xml queries/my-queries.xml
526+
```
527+
528+
This will show output like:
529+
```
530+
시각 함수 [KST_NOW] 치환: 2024-10-06 00:30:45
531+
시각 함수 [KOREAN_DATE] 치환: 2024년 10월 6일
532+
시각 함수 [DATE_YYYYMMDD] 치환: 20241006
533+
```
534+
412535
## 🎨 Advanced Features
413536

414537
### 1. Excel Styling
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"excel": {
3+
"db": "sampleDB",
4+
"output": "output/datetime_variables_example_json.xlsx",
5+
"style": "business"
6+
},
7+
"vars": {
8+
"reportTitle": "시각 변수 사용 예제 (JSON)",
9+
"department": "개발팀"
10+
},
11+
"sheets": [
12+
{
13+
"name": "기본시각함수",
14+
"use": true,
15+
"query": "SELECT '${reportTitle}' as 리포트제목, '${CURRENT_TIMESTAMP}' as 현재시각_UTC, '${NOW}' as 현재시각_NOW, '${CURRENT_DATE}' as 현재날짜, '${CURRENT_TIME}' as 현재시간, '${GETDATE}' as SQL서버형식"
16+
},
17+
{
18+
"name": "한국시간대",
19+
"use": true,
20+
"query": "SELECT '${KST_NOW}' as 한국현재시각, '${KST_DATE}' as 한국현재날짜, '${KST_TIME}' as 한국현재시간, '${KST_DATETIME}' as 한국날짜시간, '${KST_ISO_TIMESTAMP}' as 한국ISO형식"
21+
},
22+
{
23+
"name": "한국식날짜형식",
24+
"use": true,
25+
"query": "SELECT '${KOREAN_DATE}' as 한국식날짜, '${KOREAN_DATETIME}' as 한국식날짜시간, '${KOREAN_DATE_SHORT}' as 한국식날짜짧은형식, '${WEEKDAY_KR}' as 한국어요일, '${MONTH_KR}' as 한국어월, '${YEAR_KR}' as 한국어년도"
26+
},
27+
{
28+
"name": "파일명생성예제",
29+
"use": true,
30+
"query": "SELECT '${department}' as 부서명, 'Report_${DATE_YYYYMMDD}_${department}.xlsx' as 추천파일명, '${KOREAN_DATE} ${WEEKDAY_KR} 리포트' as 제목예시, 'WHERE created_date >= ''${DATE_YYYY_MM_DD}'' AND department = ''${department}''' as 쿼리조건예시"
31+
},
32+
{
33+
"name": "타임스탬프활용",
34+
"use": true,
35+
"query": "SELECT '${UNIX_TIMESTAMP}' as 유닉스타임스탬프, '${TIMESTAMP_MS}' as 밀리초타임스탬프, '${ISO_TIMESTAMP}' as ISO형식, '${DATETIME_YYYYMMDD_HHMMSS}' as 파일명용시각, 'backup_${DATE_YYYYMMDD}_${department}' as 백업파일명예시"
36+
}
37+
]
38+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<queries maxRows="1000">
3+
<excel db="sampleDB" output="output/datetime_variables_example.xlsx" style="modern">
4+
</excel>
5+
6+
<vars>
7+
<var name="reportTitle">시각 변수 사용 예제</var>
8+
<var name="department">IT</var>
9+
</vars>
10+
11+
<sheets>
12+
<!-- 기본 시각 함수들 -->
13+
<sheet name="기본시각함수" use="true">
14+
<![CDATA[
15+
SELECT
16+
'${reportTitle}' as 리포트제목,
17+
'${CURRENT_TIMESTAMP}' as 현재시각_UTC,
18+
'${NOW}' as 현재시각_NOW,
19+
'${CURRENT_DATE}' as 현재날짜,
20+
'${CURRENT_TIME}' as 현재시간,
21+
'${GETDATE}' as SQL서버형식
22+
]]>
23+
</sheet>
24+
25+
<!-- 한국 시간대 함수들 -->
26+
<sheet name="한국시간대" use="true">
27+
<![CDATA[
28+
SELECT
29+
'${KST_NOW}' as 한국현재시각,
30+
'${KST_DATE}' as 한국현재날짜,
31+
'${KST_TIME}' as 한국현재시간,
32+
'${KST_DATETIME}' as 한국날짜시간,
33+
'${KST_ISO_TIMESTAMP}' as 한국ISO형식
34+
]]>
35+
</sheet>
36+
37+
<!-- 한국식 날짜 형식 -->
38+
<sheet name="한국식날짜형식" use="true">
39+
<![CDATA[
40+
SELECT
41+
'${KOREAN_DATE}' as 한국식날짜,
42+
'${KOREAN_DATETIME}' as 한국식날짜시간,
43+
'${KOREAN_DATE_SHORT}' as 한국식날짜짧은형식,
44+
'${WEEKDAY_KR}' as 한국어요일,
45+
'${MONTH_KR}' as 한국어월,
46+
'${YEAR_KR}' as 한국어년도
47+
]]>
48+
</sheet>
49+
50+
<!-- 다양한 형식들 -->
51+
<sheet name="다양한형식" use="true">
52+
<![CDATA[
53+
SELECT
54+
'${DATE_YYYYMMDD}' as YYYYMMDD형식,
55+
'${DATE_YYYY_MM_DD}' as YYYY_MM_DD형식,
56+
'${DATETIME_YYYYMMDD_HHMMSS}' as 날짜시간조합형식,
57+
'${WEEKDAY_EN}' as 영어요일,
58+
'${UNIX_TIMESTAMP}' as 유닉스타임스탬프,
59+
'${TIMESTAMP_MS}' as 밀리초타임스탬프
60+
]]>
61+
</sheet>
62+
63+
<!-- 실제 사용 예제 -->
64+
<sheet name="실제사용예제" use="true">
65+
<![CDATA[
66+
SELECT
67+
'${department}' as 부서명,
68+
'리포트 생성일: ${KOREAN_DATE}' as 생성정보,
69+
'생성시각: ${KST_NOW}' as 상세시각,
70+
'요일: ${WEEKDAY_KR}' as 요일정보,
71+
'파일명 형식: Report_${DATE_YYYYMMDD}_${department}' as 파일명예시,
72+
'WHERE 조건 예시: created_date >= ''${DATE_YYYY_MM_DD}''' as 쿼리예시
73+
]]>
74+
</sheet>
75+
</sheets>
76+
</queries>

src/mssql-helper.js

Lines changed: 98 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,107 @@ 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+
7578
return {
76-
'CURRENT_TIMESTAMP': () => new Date().toISOString().slice(0, 19).replace('T', ' '), // YYYY-MM-DD HH:mm:ss
77-
'CURRENT_DATETIME': () => new Date().toISOString().slice(0, 19).replace('T', ' '), // YYYY-MM-DD HH:mm:ss
78-
'NOW': () => new Date().toISOString().slice(0, 19).replace('T', ' '), // YYYY-MM-DD HH:mm:ss
79-
'CURRENT_DATE': () => new Date().toISOString().slice(0, 10), // YYYY-MM-DD
80-
'CURRENT_TIME': () => new Date().toTimeString().slice(0, 8), // HH:mm:ss
79+
// 기본 시각 함수들
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
86+
87+
// 한국 시간대 함수들
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', ' '), // 한국 날짜시간
92+
93+
// 한국식 날짜 형식
94+
'KOREAN_DATE': () => {
95+
const kst = new Date(now.getTime() + (9 * 60 * 60 * 1000));
96+
return kst.toLocaleDateString('ko-KR', {
97+
year: 'numeric',
98+
month: 'long',
99+
day: 'numeric'
100+
});
101+
}, // YYYY년 M월 D일
102+
'KOREAN_DATETIME': () => {
103+
const kst = new Date(now.getTime() + (9 * 60 * 60 * 1000));
104+
return kst.toLocaleString('ko-KR', {
105+
year: 'numeric',
106+
month: 'long',
107+
day: 'numeric',
108+
hour: '2-digit',
109+
minute: '2-digit',
110+
second: '2-digit',
111+
hour12: false
112+
});
113+
}, // YYYY년 M월 D일 HH:mm:ss
114+
'KOREAN_DATE_SHORT': () => {
115+
const kst = new Date(now.getTime() + (9 * 60 * 60 * 1000));
116+
return kst.toLocaleDateString('ko-KR', {
117+
year: 'numeric',
118+
month: '2-digit',
119+
day: '2-digit'
120+
});
121+
}, // YYYY. MM. DD.
122+
123+
// 다양한 형식들
124+
'DATE_YYYYMMDD': () => {
125+
const kst = new Date(now.getTime() + (9 * 60 * 60 * 1000));
126+
const year = kst.getFullYear();
127+
const month = String(kst.getMonth() + 1).padStart(2, '0');
128+
const day = String(kst.getDate()).padStart(2, '0');
129+
return `${year}${month}${day}`;
130+
}, // YYYYMMDD
131+
'DATE_YYYY_MM_DD': () => {
132+
const kst = new Date(now.getTime() + (9 * 60 * 60 * 1000));
133+
const year = kst.getFullYear();
134+
const month = String(kst.getMonth() + 1).padStart(2, '0');
135+
const day = String(kst.getDate()).padStart(2, '0');
136+
return `${year}-${month}-${day}`;
137+
}, // YYYY-MM-DD (한국 시간)
138+
'DATETIME_YYYYMMDD_HHMMSS': () => {
139+
const kst = new Date(now.getTime() + (9 * 60 * 60 * 1000));
140+
const year = kst.getFullYear();
141+
const month = String(kst.getMonth() + 1).padStart(2, '0');
142+
const day = String(kst.getDate()).padStart(2, '0');
143+
const hour = String(kst.getHours()).padStart(2, '0');
144+
const minute = String(kst.getMinutes()).padStart(2, '0');
145+
const second = String(kst.getSeconds()).padStart(2, '0');
146+
return `${year}${month}${day}_${hour}${minute}${second}`;
147+
}, // YYYYMMDD_HHMMSS
148+
149+
// 타임스탬프 함수들
81150
'UNIX_TIMESTAMP': () => Math.floor(Date.now() / 1000), // Unix timestamp
82151
'TIMESTAMP_MS': () => Date.now(), // Milliseconds timestamp
83-
'ISO_TIMESTAMP': () => new Date().toISOString(), // ISO 8601 format
84-
'GETDATE': () => new Date().toISOString().slice(0, 19).replace('T', ' ') // SQL Server GETDATE() equivalent
152+
'ISO_TIMESTAMP': () => now.toISOString(), // ISO 8601 format
153+
'KST_ISO_TIMESTAMP': () => koreaTime.toISOString(), // 한국 시간 ISO 8601
154+
155+
// 요일 정보
156+
'WEEKDAY_KR': () => {
157+
const kst = new Date(now.getTime() + (9 * 60 * 60 * 1000));
158+
const weekdays = ['일요일', '월요일', '화요일', '수요일', '목요일', '금요일', '토요일'];
159+
return weekdays[kst.getDay()];
160+
}, // 한국어 요일
161+
'WEEKDAY_EN': () => {
162+
const kst = new Date(now.getTime() + (9 * 60 * 60 * 1000));
163+
const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
164+
return weekdays[kst.getDay()];
165+
}, // 영어 요일
166+
167+
// 월 정보
168+
'MONTH_KR': () => {
169+
const kst = new Date(now.getTime() + (9 * 60 * 60 * 1000));
170+
return `${kst.getMonth() + 1}월`;
171+
}, // N월
172+
'YEAR_KR': () => {
173+
const kst = new Date(now.getTime() + (9 * 60 * 60 * 1000));
174+
return `${kst.getFullYear()}년`;
175+
} // YYYY년
85176
};
86177
}
87178

0 commit comments

Comments
 (0)