From 0519b5bd3de2f98cda3a0e5b48bb8a8f1b890fc8 Mon Sep 17 00:00:00 2001 From: RWERNER Date: Fri, 1 Mar 2024 17:59:16 +0100 Subject: [PATCH] date field of GS1 barcodes should consider the timezone offset --- spec/BarcodeParserSpec.js | 13 +++++++++++-- src/BarcodeParser.js | 4 ++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/spec/BarcodeParserSpec.js b/spec/BarcodeParserSpec.js index 496a65f..cd3294f 100644 --- a/spec/BarcodeParserSpec.js +++ b/spec/BarcodeParserSpec.js @@ -24,7 +24,7 @@ describe("A parsed GS1 barcode", () => { expect(result.parsedCodeItems).toEqual(jasmine.arrayContaining([jasmine.objectContaining({ ai: "17", dataTitle: "USE BY OR EXPIRY", - data: new Date(2015, 0, 29, 0, 0, 0, 0) + data: new Date('2015-01-29') })])); }); @@ -93,7 +93,16 @@ describe("A parsed GS1 barcode", () => { expect(result.parsedCodeItems).toEqual(jasmine.arrayContaining([jasmine.objectContaining({ ai: "17", dataTitle: "USE BY OR EXPIRY", - data: new Date(2019, 4, 31, 0, 0, 0, 0) + data: new Date('2019-05-31') })])); }); +}); + +describe("Date fields of a parsed GS1 barcode", () => { + it("should consider the timezone offset", () => { + const barcode = "15230300"; + const result = parseBarcode(barcode); + + expect(result.parsedCodeItems[0].data.toISOString()).toEqual('2023-03-31T00:00:00.000Z'); + }); }); \ No newline at end of file diff --git a/src/BarcodeParser.js b/src/BarcodeParser.js index c1ae9c1..2a7f42a 100644 --- a/src/BarcodeParser.js +++ b/src/BarcodeParser.js @@ -71,7 +71,7 @@ const parseBarcode = (function () { break; case "D": this.data = new Date(); - this.data.setHours(0, 0, 0, 0); + this.data.setUTCHours(0, 0, 0, 0); break; default: this.data = ""; @@ -218,7 +218,7 @@ const parseBarcode = (function () { monthAsNumber--; } - elementToReturn.data.setFullYear(yearAsNumber, monthAsNumber, dayAsNumber); + elementToReturn.data.setUTCFullYear(yearAsNumber, monthAsNumber, dayAsNumber); codestringToReturn = codestring.slice(offSet + 6, codestringLength); elementToReturn.raw = codestring.slice(offSet, offSet+6); }