😀

mongodbでapacheのアクセスログをISODateに変換する関数

2022/11/28に公開約1,500字

apacheのリクエスト時間のデフォルトの日付フォーマットをISODateに渡せるように変換スクリプトを書いてみた

apacheDate2ISODate.js
    apacheDate2ISODate = function (apacheDate)
    {

        rawStr = apacheDate.replace(/^(\[)/, "").replace(/\]/, "")
        dateArr = rawStr.split(" ");

        dateSeparate  = dateArr[0].split('/')
        dateSeparate2 = dateSeparate[2].split(':')

        day   = dateSeparate[0]
        month = dateSeparate[1]
        year  = dateSeparate2[0]
        hours = dateSeparate2[1]
        minute = dateSeparate2[2]
        second = dateSeparate2[3]
        date   = dateSeparate2[1] + dateSeparate2[2] + dateSeparate2[3]

        DateString = month + " " + day + " " + year + " " + hours + ":" + minute + ":" + second
        convertDate = new Date(DateString)

        date     = ( convertDate.getDate()   < 10 )  ? '0' + convertDate.getDate()    : convertDate.getDate()
        month    = ( convertDate.getMonth()   < 10 ) ? '0' + convertDate.getMonth()   : convertDate.getMonth()
        hours    = ( convertDate.getHours()   < 10 ) ? '0' + convertDate.getHours()   : convertDate.getHours()
        minute   = ( convertDate.getMinutes() < 10 ) ? '0' + convertDate.getMinutes() : convertDate.getMinutes()
        second   = ( convertDate.getSeconds() < 10 ) ? '0' + convertDate.getSeconds() : convertDate.getSeconds()

        dateStr = convertDate.getFullYear() + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second

        return ISODate(dateStr)
    }

使い方

apacheDate2ISODate(apacheのリクエスト時間)

多分合ってると思う。。。

Discussion

ログインするとコメントできます