js-cfb/bits/37_dosdates.js

39 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-09-04 07:14:20 +00:00
/* -------------------------------------------------------------------------- */
/* DOS Date format:
high|YYYYYYYm.mmmddddd.HHHHHMMM.MMMSSSSS|low
add 1980 to stored year
stored second should be doubled
*/
/* write JS date to buf as a DOS date */
function write_dos_date(buf/*:CFBlob*/, date/*:Date|string*/) {
if(typeof date === "string") date = new Date(date);
var hms/*:number*/ = date.getHours();
hms = hms << 6 | date.getMinutes();
hms = hms << 5 | (date.getSeconds()>>>1);
buf.write_shift(2, hms);
var ymd/*:number*/ = (date.getFullYear() - 1980);
ymd = ymd << 4 | (date.getMonth()+1);
ymd = ymd << 5 | date.getDate();
buf.write_shift(2, ymd);
}
/* read four bytes from buf and interpret as a DOS date */
function parse_dos_date(buf/*:CFBlob*/)/*:Date*/ {
var hms = buf.read_shift(2) & 0xFFFF;
var ymd = buf.read_shift(2) & 0xFFFF;
var val = new Date();
var d = ymd & 0x1F; ymd >>>= 5;
var m = ymd & 0x0F; ymd >>>= 4;
val.setMilliseconds(0);
val.setFullYear(ymd + 1980);
val.setMonth(m-1);
val.setDate(d);
var S = hms & 0x1F; hms >>>= 5;
var M = hms & 0x3F; hms >>>= 6;
val.setHours(hms);
val.setMinutes(M);
val.setSeconds(S<<1);
return val;
}