MSDOS(FAT)時刻⇔JavaScript(Date) SJIS判定 for zip(pkzip) TypeScript

引退させてもらえないzipファイル用の小ネタです。
MSDOS時刻(32bit整数)とJavaScriptのDateとの相互変換
Windows(日本)で作成したzipファイルはシフトJISなので、UTF-8かどうか判定してstringに変換
2107年まで現役?

function dateFromMSDOS(d : number){
    let year =  ((d>>>25) & 0x7f) + 1980;
    let month = ((d>>>21) & 0x0f);
    let day =   ((d>>>16) & 0x1f);
    let hour =  ((d>>>11) & 0x1f);
    let min =   ((d>>> 5) & 0x3f);
    let sec =   ((d>>> 0) & 0x1f)*2;
    return new Date(year, month-1, day, hour, min, sec);
}

function msdosFromDate(date : Date) : number{
    return ((((date.getFullYear() -1980)) & 0x7f )*33554432) //(1<<25)
    + (((date.getMonth()+1) & 0xf ) *2097152) //(1<<21)
    + (( date.getDate() & 0x001f )  *65536) //(1<<16)
    + (( date.getHours() & 0x1f )   *2048) //(1<<11)
    + (( date.getMinutes() & 0x3f ) *32) //(1<<5)
    + (( date.getSeconds()>>>1 ) & 0x1f);
}

// for zip(pkzip)
export class TextDecoderSJISorUTF8{
    static sjisDec = new TextDecoder("sjis");//Windows(日本)
    static utf8Dec = new TextDecoder("utf-8", { fatal:true } );
    decode(bin : Uint8Array){
        try{
            return TextDecoderSJISorUTF8.utf8Dec.decode(bin);
        }catch{
            return TextDecoderSJISorUTF8.sjisDec.decode(bin);
        }
    }
}

この記事が役に立ったという方は、サポートお願いします。今後の製作の励みになります。