Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
まだ昼休みだからセーフだから!(はてなハイクでエクスポートしたCSVに日本時間とUTCの時刻の列を追加するHTMLファイルです。ローカルに保存して開いて指示に従うとZIPが降ってきてCSVをゲットできます。わーおすごいわね!)
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf=8">
</head>
<body>
<h1>はてなCSVのunixtimeをどうにかする</h1>
<p>CSVをくわす</p>
<input type="file" id="files" name="files[]" />
<div id="haiku"></div>
</body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.5/jszip.min.js"></script>
<script>
function datestr(s) {
return ('0' + s).slice(-2);
}
function gen_newcsv(t) {
tl = t.split("\n");
var ret = "Date,Date(UTC)," + tl[0] + "\n";
var l;
for (var i = 1; i < tl.length; i++) {
l = tl[i].split(",");
/* forとか使えそうだけど面倒だからいいや */
var d_utc = new Date(l[0]*1000);
var d_jst = new Date(l[0]*1000);
console.log(d_utc);
d_jst.setTime(d_jst.getTime() + 1000 * 60 * 60 * 9);
var created_at_utc = d_utc.getFullYear() + '/' + datestr(d_utc.getMonth()+1) + '/' + datestr(d_utc.getDate()) + ' ' +
datestr(d_utc.getHours()) + ':' + datestr(d_utc.getMinutes()) + ':' + datestr(d_utc.getSeconds());
var created_at_jst = d_jst.getFullYear() + '/' + datestr(d_jst.getMonth()+1) + '/' + datestr(d_jst.getDate()) + ' ' +
datestr(d_jst.getHours()) + ':' + datestr(d_jst.getMinutes()) + ':' + datestr(d_jst.getSeconds());
ret += '"' + created_at_jst + '","' + created_at_utc + '",' + tl[i] + "\n";
}
return ret;
}
/* https://www.html5rocks.com/ja/tutorials/file/dndfiles/ */
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
// Loop through the FileList and render image files as thumbnails.
for (var i = 0, f; f = files[i]; i++) {
// Only process csv files.
if (f.type != 'text/csv') {
continue;
}
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
var newcsv = gen_newcsv(e.target.result);
var zip = new JSZip();
zip.file("haiku_csv_with_date.csv", newcsv);
zip.generateAsync({type:"blob"})
.then(function (blob) {
if (window.navigator.msSaveBlob) {
window.navigator.msSaveBlob(blob, USER + '.zip');
window.navigator.msSaveOrOpenBlob(blob, USER + '.zip');
} else {
var lnk = document.createElement('a');
lnk.textContent = 'ダウンロード';
lnk.id = "haikudl";
lnk.href = window.URL.createObjectURL(blob);
document.getElementById('haiku').appendChild(lnk);
}
});
};
})(f);
// Read in the image file as a data URL.
reader.readAsText(f);
}
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment