Last active
December 20, 2015 19:49
-
-
Save mas3/6186231 to your computer and use it in GitHub Desktop.
エポック秒→日時変換(「スクリプト実行」用スクリプト)
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
/* | |
* エポック秒→日時変換 | |
* | |
* 「スクリプト実行」用スクリプト | |
* 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