From 95affb232a9e27f431c073380a3d83488cc6c235 Mon Sep 17 00:00:00 2001 From: eduardo Date: Fri, 10 Jul 2020 20:28:10 -0300 Subject: [PATCH] fix: timezone issue, set as always UTC --- package-lock.json | 15 ++++++++++++++- package.json | 3 +++ src/boleto-utils.js | 15 ++++----------- test/test.js | 4 ++-- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index 397f78e..48017ac 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@mrmgomes/boleto-utils", - "version": "1.1.0", + "version": "1.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -662,6 +662,19 @@ "yargs-unparser": "1.6.0" } }, + "moment": { + "version": "2.27.0", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.27.0.tgz", + "integrity": "sha512-al0MUK7cpIcglMv3YF13qSgdAIqxHTO7brRtaz3DlSULbqfazqkc5kEjNrLDOM7fsjshoFIihnU8snrP7zUvhQ==" + }, + "moment-timezone": { + "version": "0.5.31", + "resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.31.tgz", + "integrity": "sha512-+GgHNg8xRhMXfEbv81iDtrVeTcWt0kWmTEY1XQK14dICTXnWJnT0dxdlPspwqF3keKMVPXwayEsk1DI0AA/jdA==", + "requires": { + "moment": ">= 2.9.0" + } + }, "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", diff --git a/package.json b/package.json index fd6745e..b164890 100644 --- a/package.json +++ b/package.json @@ -32,5 +32,8 @@ "devDependencies": { "chai": "^4.2.0", "mocha": "^8.0.1" + }, + "dependencies": { + "moment-timezone": "^0.5.31" } } diff --git a/src/boleto-utils.js b/src/boleto-utils.js index f52d257..0a3bea6 100644 --- a/src/boleto-utils.js +++ b/src/boleto-utils.js @@ -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') { @@ -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(); } /** diff --git a/test/test.js b/test/test.js index 10227de..292eb92 100644 --- a/test/test.js +++ b/test/test.js @@ -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')); }); }); @@ -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')); }); }); });