JavaScript: Google spreadsheet scripts to convert dates between Korean lunar calendar and solar calendar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function test_lun2sol() { | |
Logger.log(lun2sol(2013, 1, 4, 0)); | |
} | |
function test_sol2lun() { | |
Logger.log(sol2lun(2013, 1, 26, 1)); | |
} | |
/** | |
* lun2sol 의 date 버전 | |
*/ | |
function lunar2solar(date, type, sepa) { | |
var datestr = Utilities.formatDate(date, SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "yyyyMMdd"); | |
return lun2sol(datestr.substr(0, 4), datestr.substr(4, 2), datestr.substr(6, 2), type, sepa); | |
} | |
/** | |
* sol2lun 의 date 버전 | |
*/ | |
function solar2lunar(date, sepa) { | |
var datestr = Utilities.formatDate(date, SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "yyyyMMdd"); | |
return sol2lun(datestr.substr(0, 4), datestr.substr(4, 2), datestr.substr(6, 2), sepa); | |
} | |
/** | |
* 음력일자를 받아서 양력일자를 리턴하는 함수 (한국천문연구원 사이트를 이용) | |
* 음력 10월 3일의 2013년 양력일자를 알고 싶은 경우 lun2sol(2013, 10, 3, 1) 으로 호출 | |
* 응답으로는 '2013/11/05' 와 같이 스트링을 리턴. | |
* type (윤달구분) : 1 - 평달, 2 - 윤달, 3 - 알 수 없음 | |
*/ | |
function lun2sol(y, m, d, type, sepa) { | |
return SolunConvert(y, m, d, type, 0, sepa); | |
} | |
/** | |
* 양력일자를 받아서 음력일자를 리턴하는 함수 (한국천문연구원 사이트를 이용) | |
* 양력 2013년 1월 26일의 음력일자를 알고 싶은 경우 sol2lun(2013, 1, 26) 으로 호출 | |
* 응답으로는 '2012/12/15 평달' 과 같이 스트링을 리턴. | |
*/ | |
function sol2lun(y, m, d, sepa) { | |
return SolunConvert(y, m, d, 0, 1, sepa); | |
} | |
/** | |
* 음력, 양력 상호변환 | |
* 기존 lun2sol, sol2lun 함수의 중복 부분이 많아 하나로 병합 | |
* cache 에 날짜 구분자를 넣지 않는다. | |
*/ | |
function SolunConvert(y, m, d, type, func, sepa) { | |
y = parseInt(y, 10); | |
m = parseInt(m, 10); | |
d = parseInt(d, 10); | |
if (1391 > y || y > 2050 | |
|| 1 > m || m > 12 | |
|| 1 > d || d > 31) { | |
return "ERROR"; | |
} | |
var funcname = (func == 0? "l2s": "s2l"); | |
var cachekey = funcname + "_" + y + "_" + m + "_" + d + "_" + type; | |
if (sepa == null) { | |
sepa = "/"; | |
} | |
var cache = PropertiesService.getUserProperties().getProperty(cachekey); | |
if (cache) { | |
return cache.substr(0, 4) + sepa + cache.substr(4, 2) + sepa + cache.substr(6, 2); | |
} | |
var params = { | |
"method": "post", | |
"payload": { | |
"lun_year": String(y), | |
"lun_month": String(m), | |
"lun_day": String(d), | |
"sol_year": String(y), | |
"sol_month": String(m), | |
"sol_day": String(d), | |
"yoon": String(type), | |
}, | |
}; | |
var url = "http://astro.kasi.re.kr/Life/Knowledge/solar2lunar/convert_daily_" + funcname + ".php"; | |
var response = UrlFetchApp.fetch(url, params); | |
var htmlstr = response.getContentText("CP949"); | |
var pos = htmlstr.indexOf("td width=500", pos); //-- 천문연구원 사이트 변경되면 수정 필요 | |
if (pos >= 0) { | |
pos += 13; | |
var check = htmlstr.substr(pos, 1); | |
if ("0" > check || check > "9") { | |
return "ERROR"; | |
} | |
var result = htmlstr.substr(pos, 4) + sepa + htmlstr.substr(pos + 6, 2) + sepa + htmlstr.substr(pos + 10, 2); | |
PropertiesService.getUserProperties().setProperty(cachekey, htmlstr.substr(pos, 4) + htmlstr.substr(pos + 6, 2) + htmlstr.substr(pos + 10, 2)); | |
return result; | |
} | |
return "ERROR"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment