박지환

Merge branch 'feature/holiday-counter'

{
// IntelliSense를 사용하여 가능한 특성에 대해 알아보세요.
// 기존 특성에 대한 설명을 보려면 가리킵니다.
// 자세한 내용을 보려면 https://go.microsoft.com/fwlink/?linkid=830387을(를) 방문하세요.
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\app.js"
}
]
}
\ No newline at end of file
const today = new Date("2022-05-12"); //오늘 날짜 입력
const holiday = new Date("2022-10-01"); //공휴일 날짜 입력
const diffDate= today.getTime() - holiday.getTime();
const dday= Math.abs(diffDate / (1000 * 3600 * 24));
console.log(dday);
\ No newline at end of file
......@@ -13,13 +13,11 @@ var operation = "getHoliDeInfo";
// Do not modify the values
var SERVEICE_KEY =
"qBtJy2Prw8CCnAiijUM7VkuaA9MZozHuiQI4FbEGYdUDPz4%2FM%2FuxegGjNBWK0aWQHvSslVHwIZQwNWh57WgRTA%3D%3D";
var url =
"http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/" +
operation;
var url = "http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/";
var queryParams = "?" + "solYear" + "=" + year;
queryParams += "&" + "solMonth" + "=" + month;
queryParams += "&" + "ServiceKey" + "=" + SERVEICE_KEY;
let requestUrl = url + queryParams;
let requestUrl = url + operation + queryParams;
// Empty variables
var text = "";
......@@ -31,7 +29,55 @@ var tempArr = [];
app.set("views", __dirname + "/views");
app.set("view engine", "ejs");
function init() {
// To get today date by using Date
function getTodayDate() {
var today = new Date();
var year = today.getFullYear();
var month = ("0" + (today.getMonth() + 1)).slice(-2);
var day = ("0" + today.getDate()).slice(-2);
var dateString = year + "-" + month + "-" + day;
// return "2022-10-04"; // 테스트용 날짜를 입력하세요. 테스트가 끝나면 주석처리 하세요. ex) 2022-09-12, 2022-09-15, 2022-10-04
return dateString;
}
// To get modified date from locdate
function getModifiedDate(locdate) {
return (
locdate.substr(0, 4) +
"-" +
locdate.substr(4, 2) +
"-" +
locdate.substr(6, 2)
);
}
// To get remaining days from locdate
function getLeftDate(dateName, locdate) {
var today = new Date(getTodayDate()); // today date
var holiday = new Date(getModifiedDate(locdate)); // holiday date
var diffDate = today.getTime() - holiday.getTime();
var dday = Math.abs(diffDate / (1000 * 3600 * 24));
return dday;
}
// To change parameter
function changeParams(year, month, operation) {
queryParams = "?" + "solYear" + "=" + year;
queryParams += "&" + "solMonth" + "=" + month;
queryParams += "&" + "ServiceKey" + "=" + SERVEICE_KEY;
requestUrl = url + operation + queryParams;
}
// To check is this left holiday
function cmpDate(date) {
var today = getTodayDate().replace(/\-/g, "");
if (Number(today) <= Number(date)) {
return true;
}
}
// To get data from holiday api by using parmas
function getData() {
request.get(requestUrl, (err, res, body) => {
if (err) {
console.log("err => " + err);
......@@ -40,24 +86,18 @@ function init() {
// Read url success
var result = body;
var xmlToJson = convert.xml2json(result, { compact: true, spaces: 4 });
console.log(result);
console.log(xmlToJson);
fs.writeFileSync("holi.xml", result); // Create/Modify holi.xml
fs.writeFileSync("holi.json", xmlToJson); // Create/Modify holi.json
var parser = new xml2js.Parser();
parser.parseString(result, function (err, res) {
console.log(res);
text = JSON.stringify(res);
console.log(text);
// Get dataName method
dateName = [];
var idx = text.indexOf("dateName", 0);
while (idx != -1) {
console.log(idx);
var start = text.indexOf("[", idx) + 2;
var end = text.indexOf("]", idx) - 1;
var tempStr = text.substring(start, end);
console.log(tempStr);
dateName.push(tempStr);
idx = text.indexOf("dateName", idx + 1);
}
......@@ -66,40 +106,70 @@ function init() {
locdate = [];
idx = text.indexOf("locdate", 0);
while (idx != -1) {
console.log(idx);
var start = text.indexOf("[", idx) + 2;
var end = text.indexOf("]", idx) - 1;
var tempStr = text.substring(start, end);
console.log(tempStr);
locdate.push(tempStr);
idx = text.indexOf("locdate", idx + 1);
}
let i = 0;
let length = dateName.length;
while (i < length){
var temptoday = dateName[i];
var tempdate = locdate[i];
var modifiedDate = tempdate.substr(0,4) + '-' + tempdate.substr(4,2) + '-' + tempdate.substr(6,2);
var today = new Date("2022-05-13"); //오늘 날짜 입력
var holiday = new Date(modifiedDate); //공휴일 날짜 입력
var diffDate= today.getTime() - holiday.getTime();
var dday= Math.abs(diffDate / (1000 * 3600 * 24));
console.log(temptoday + "까지 " + dday +"일 남았습니다.");
i++;
}
console.log(locdate);
// Create tempArr to save dateName and locdate at once
var holiArr = [];
for (var i = 0; i < dateName.length; i++) {
holiArr.push(getLeftDate(dateName[i], locdate[i]));
}
console.log(holiArr);
// Create tempArr to save dateName and locdate and leftDate at once
tempArr = [];
tempArr.push(dateName);
tempArr.push(locdate);
// To check left holiday
var check = false;
for (var i = 0; i < locdate.length; i++) {
if (cmpDate(locdate[i])) {
check = true;
tempArr.push(dateName[i]); // Get recent holiday name
tempArr.push(locdate[i]); // Get recent holiday date
tempArr.push(holiArr[i]); // Get leftDate through locdate
break;
}
}
console.log(tempArr);
if (!check) {
// If there are no holidays left this month
console.log(
"이번 달에는 남은 공휴일이 없습니다. 다음달 데이터를 불러옵니다."
);
// Get next month data
month = String(Number(month) + 1).padStart(2, "0");
changeParams(year, month, operation);
getData();
}
console.log("* api로부터 데이터를 불러왔습니다.");
console.log("오늘의 날짜는", getTodayDate(), "입니다.");
console.log(
"가장 가까운 공휴일인 [" +
tempArr[0] +
"]의 날짜는 [" +
tempArr[1] +
"]이고, [" +
tempArr[2] +
"]일 남았습니다."
);
});
}
}
});
}
// To initialize datas when calling webpage
function init() {
// Intialize Year, Month
var date = getTodayDate().split("-");
year = date[0];
month = date[1];
operation = "getHoliDeInfo";
changeParams(year, month, operation);
// Get data from holiday api
getData();
}
app.get("/", function (req, res) {
init();
......
<!-- data.ejs -->
<!DOCTYPE html>
<html lang="en">
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
......@@ -9,10 +9,8 @@
<title>Document</title>
</head>
<body>
<% for(var i = 0; i < data[0].length; i++) {%>
<p>
<h1><%= data[0][i] %> <%= data[1][i] %></h1>
</p>
<% } %>
<p>
<h1>[<%= data[0] %>]까지 <%= data[2] %>일 남았습니다. (<%= data[1] %>)</h1>
</p>
</body>
</html>
\ No newline at end of file
......