Skip to content

Commit ba46576

Browse files
authored
Update date time functions (#518)
Co-authored-by: 5saviahv <[email protected]>
1 parent 7a1b68e commit ba46576

File tree

3 files changed

+61
-28
lines changed

3 files changed

+61
-28
lines changed

headers/entryHeader.js

+26-27
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,12 @@ module.exports = function () {
2929
extraLen: 0
3030
};
3131

32-
function setTime(val) {
33-
val = new Date(val);
34-
_time =
35-
(((val.getFullYear() - 1980) & 0x7f) << 25) | // b09-16 years from 1980
36-
((val.getMonth() + 1) << 21) | // b05-08 month
37-
(val.getDate() << 16) | // b00-04 hour
38-
// 2 bytes time
39-
(val.getHours() << 11) | // b11-15 hour
40-
(val.getMinutes() << 5) | // b05-10 minute
41-
(val.getSeconds() >> 1); // b00-04 seconds divided by 2
42-
}
43-
44-
setTime(+new Date());
32+
// casting
33+
const uint32 = (val) => Math.max(0, val) >>> 0;
34+
const uint16 = (val) => Math.max(0, val) & 0xffff;
35+
const uint8 = (val) => Math.max(0, val) & 0xff;
36+
37+
_time = Utils.fromDate2DOS(new Date());
4538

4639
return {
4740
get made() {
@@ -102,33 +95,41 @@ module.exports = function () {
10295
},
10396

10497
get time() {
105-
return new Date(((_time >> 25) & 0x7f) + 1980, ((_time >> 21) & 0x0f) - 1, (_time >> 16) & 0x1f, (_time >> 11) & 0x1f, (_time >> 5) & 0x3f, (_time & 0x1f) << 1);
98+
return Utils.fromDOS2Date(this.timeval);
10699
},
107100
set time(val) {
108-
setTime(val);
101+
this.timeval = Utils.fromDate2DOS(val);
102+
},
103+
104+
get timeval() {
105+
return _time;
109106
},
107+
set timeval(val) {
108+
_time = uint32(val);
109+
},
110+
110111
get timeHighByte() {
111-
return (_time >>> 8) & 0xff;
112+
return uint8(_time >>> 8);
112113
},
113114
get crc() {
114115
return _crc;
115116
},
116117
set crc(val) {
117-
_crc = Math.max(0, val) >>> 0;
118+
_crc = uint32(val);
118119
},
119120

120121
get compressedSize() {
121122
return _compressedSize;
122123
},
123124
set compressedSize(val) {
124-
_compressedSize = Math.max(0, val) >>> 0;
125+
_compressedSize = uint32(val);
125126
},
126127

127128
get size() {
128129
return _size;
129130
},
130131
set size(val) {
131-
_size = Math.max(0, val) >>> 0;
132+
_size = uint32(val);
132133
},
133134

134135
get fileNameLength() {
@@ -163,37 +164,37 @@ module.exports = function () {
163164
return _diskStart;
164165
},
165166
set diskNumStart(val) {
166-
_diskStart = Math.max(0, val) >>> 0;
167+
_diskStart = uint32(val);
167168
},
168169

169170
get inAttr() {
170171
return _inattr;
171172
},
172173
set inAttr(val) {
173-
_inattr = Math.max(0, val) >>> 0;
174+
_inattr = uint32(val);
174175
},
175176

176177
get attr() {
177178
return _attr;
178179
},
179180
set attr(val) {
180-
_attr = Math.max(0, val) >>> 0;
181+
_attr = uint32(val);
181182
},
182183

183184
// get Unix file permissions
184185
get fileAttr() {
185-
return _attr ? (((_attr >>> 0) | 0) >> 16) & 0xfff : 0;
186+
return uint16(_attr >> 16) & 0xfff;
186187
},
187188

188189
get offset() {
189190
return _offset;
190191
},
191192
set offset(val) {
192-
_offset = Math.max(0, val) >>> 0;
193+
_offset = uint32(val);
193194
},
194195

195196
get encrypted() {
196-
return (_flags & 1) === 1;
197+
return (_flags & Constants.FLG_ENC) === Constants.FLG_ENC;
197198
},
198199

199200
get centralHeaderSize() {
@@ -338,8 +339,6 @@ module.exports = function () {
338339
data.writeUInt32LE(_attr, Constants.CENATX);
339340
// LOC header offset
340341
data.writeUInt32LE(_offset, Constants.CENOFF);
341-
// fill all with
342-
data.fill(0x00, Constants.CENHDR);
343342
return data;
344343
},
345344

test/header.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe("headers", () => {
8080
});
8181
});
8282

83-
describe("entry-header", () => {
83+
describe("central-header", () => {
8484
const centralHeader = require("../headers/entryHeader");
8585
const datestamp = [1981, 3, 1, 12, 10, 10];
8686
const readBuf = Buffer.from("504b0102140014000008080045618102efbeadde0001000000020000000000000000000000000000000000000000", "hex");
@@ -180,6 +180,26 @@ describe("headers", () => {
180180
expect(head.centralHeaderSize).to.equal(446);
181181
});
182182

183+
it("centralHeader date if date is specified", () => {
184+
const head = new centralHeader();
185+
const times = [1978, 3, 1, 12, 10, 10];
186+
187+
head.time = new Date(...times);
188+
expect(head.timeval).to.equal(0);
189+
190+
times[0] = 1979;
191+
head.time = new Date(...times);
192+
expect(head.timeval).to.equal(0);
193+
194+
times[0] = 1980;
195+
head.time = new Date(...times);
196+
expect(head.timeval).to.equal(0x00816145);
197+
198+
times[0] = 1981;
199+
head.time = new Date(...times);
200+
expect(head.timeval).to.equal(0x02816145);
201+
});
202+
183203
describe("local-header", () => {
184204
const localHeader = Buffer.from("504b030414000008080045618102efbeadde000100000002000000000000", "hex");
185205

util/utils.js

+14
Original file line numberDiff line numberDiff line change
@@ -318,5 +318,19 @@ Utils.readBigUInt64LE = function (/*Buffer*/ buffer, /*int*/ index) {
318318
return parseInt(`0x${slice.toString("hex")}`);
319319
};
320320

321+
Utils.fromDOS2Date = function (val) {
322+
return new Date(((val >> 25) & 0x7f) + 1980, Math.max(((val >> 21) & 0x0f) - 1, 0), Math.max((val >> 16) & 0x1f, 1), (val >> 11) & 0x1f, (val >> 5) & 0x3f, (val & 0x1f) << 1);
323+
};
324+
325+
Utils.fromDate2DOS = function (val) {
326+
let date = 0;
327+
let time = 0;
328+
if (val.getFullYear() > 1979) {
329+
date = (((val.getFullYear() - 1980) & 0x7f) << 9) | ((val.getMonth() + 1) << 5) | val.getDate();
330+
time = (val.getHours() << 11) | (val.getMinutes() << 5) | (val.getSeconds() >> 1);
331+
}
332+
return (date << 16) | time;
333+
};
334+
321335
Utils.isWin = isWin; // Do we have windows system
322336
Utils.crcTable = crcTable;

0 commit comments

Comments
 (0)