Instantly share code, notes, and snippets.

Embed
What would you like to do?
エポック秒→日時変換(「スクリプト実行」用スクリプト)
/*
* エポック秒→日時変換
*
* 「スクリプト実行」用スクリプト
* http://books.ivory.ne.jp/execscript/
*/
var lines = data.split("\n"),
line,
i,
datetime,
year, month, day, hour, min, sec,
outdata = new Array(),
lpad = function (val, paddedLength) {
var str = val + "",
length = str.length,
i;
if (length >= paddedLength) {
return str;
}
for (i = length; i < paddedLength; i++) {
str = '0' + str;
}
return str;
};
for (i = 0; i < lines.length; i++) {
line = lines[i];
if (line === "") {
continue;
}
if (! /^[0-9]+$/.test(line)) {
outdata.push("format error. [" + line + "]");
continue;
}
datetime = new Date(line * 1000);
year = datetime.getFullYear();
month = lpad(datetime.getMonth() + 1, 2);
day = lpad(datetime.getDate(), 2);
hour = lpad(datetime.getHours(), 2);
min = lpad(datetime.getMinutes(), 2);
sec = lpad(datetime.getSeconds(), 2);
outdata.push(
year + "-" + month + "-" + day + " "
+ hour + ":" + min + ':' + sec
);
}
return outdata.join("\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment