Skip to content

Commit

Permalink
fix: timezone issue, set as always UTC
Browse files Browse the repository at this point in the history
  • Loading branch information
edniemeyer committed Jul 10, 2020
1 parent fcf1bf2 commit 95affb2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
15 changes: 14 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^8.0.1"
},
"dependencies": {
"moment-timezone": "^0.5.31"
}
}
15 changes: 4 additions & 11 deletions src/boleto-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,12 @@ exports.identificarReferencia = (codigo) => {
* @return {Date} dataBoleto
*/
exports.identificarData = (codigo, tipoCodigo) => {
var moment = require('moment-timezone');
codigo = codigo.replace(/[^0-9]/g, '');
const tipoBoleto = this.identificarTipoBoleto(codigo);

let fatorData = '';
let dataBoleto = new Date();

dataBoleto.setFullYear(1997);
dataBoleto.setMonth(9);
dataBoleto.setDate(7);
dataBoleto.setHours(23, 54, 59, 0);
let dataBoleto = moment.tz("1997-10-07 20:54:59.000Z", "UTC");

if (tipoCodigo === 'CODIGO_DE_BARRAS') {
if (tipoBoleto == 'BANCO' || tipoBoleto == 'CARTAO_DE_CREDITO') {
Expand All @@ -157,13 +153,10 @@ exports.identificarData = (codigo, tipoCodigo) => {
fatorData = '0';
}
}

dataBoleto.setDate(dataBoleto.getDate() + Number(fatorData));
dataBoleto.setTime(dataBoleto.getTime() + dataBoleto.getTimezoneOffset() - (3) * 60 * 60 * 1000);
dataBoleto.setMilliseconds(0);

dataBoleto.add(Number(fatorData), 'days');

return dataBoleto;
return dataBoleto.toDate();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('Boletos de 5 campos', function () {
expect(result).to.have.property('codigoBarras').to.equal('10499898100000214032006561000100040099726390');
expect(result).to.have.property('linhaDigitavel').to.equal('10492006506100010004200997263900989810000021403');
expect(result).to.have.property('vencimento');
assert.deepEqual(result.vencimento, new Date('2022-05-10T23:54:59.000Z'));
assert.deepEqual(result.vencimento, new Date('2022-05-10T20:54:59.000Z'));

});
});
Expand All @@ -46,7 +46,7 @@ describe('Boletos de 5 campos', function () {
expect(result).to.have.property('codigoBarras').to.equal('10499898100000214032006561000100040099726390');
expect(result).to.have.property('linhaDigitavel').to.equal('10492006506100010004200997263900989810000021403');
expect(result).to.have.property('vencimento');
assert.deepEqual(result.vencimento, new Date('2022-05-10T23:54:59.000Z'));
assert.deepEqual(result.vencimento, new Date('2022-05-10T20:54:59.000Z'));
});
});
});
Expand Down

0 comments on commit 95affb2

Please sign in to comment.