Skip to content

Commit

Permalink
Merge pull request #552 from georgesmith46/fix-timezone
Browse files Browse the repository at this point in the history
Fix date parsing for southern hemisphere timezones
  • Loading branch information
georgesmith46 committed May 1, 2023
2 parents 58dfc38 + fbdb11c commit f8e7486
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
10 changes: 2 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"scripts": {
"build": "rollup -c",
"test": "jest --testPathIgnorePatterns=\"/node_modules/\", \"/test/\"",
"test:timezone": "TZ=Europe/Berlin npm run test && TZ=America/Los_Angeles npm run test",
"test:timezone": "TZ=America/Santiago npm run test && TZ=Europe/Berlin npm run test && TZ=America/Los_Angeles npm run test",
"test:smoke": "jest test/smoke.test.js",
"release": "npx semantic-release",
"release-dry-run": "npx semantic-release-github-pr --debug"
Expand Down Expand Up @@ -37,11 +37,5 @@
"semantic-release-github-pr": "^6.0.1",
"typescript": "^5.0.4"
},
"keywords": [
"iata",
"boarding pass",
"barcode",
"encode",
"decode"
]
"keywords": ["iata", "boarding pass", "barcode", "encode", "decode"]
}
12 changes: 5 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ export const numberToHex = (n: number) =>
n.toString(16).padStart(2, "0").toUpperCase();

export const dateToDayOfYear = (date: Date, addYearPrefix = false) => {
const start = new Date(Date.UTC(date.getFullYear(), 0, 0));
const diff =
date.getTime() -
start.getTime() +
(start.getTimezoneOffset() - date.getTimezoneOffset()) * 60 * 1000;
const oneDay = 1000 * 60 * 60 * 24;
const dayOfYear = Math.floor(diff / oneDay);
const dayOfYear =
(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()) -
Date.UTC(date.getUTCFullYear(), 0, 0)) /
oneDay;
let yearPrefix = "";
if (addYearPrefix) {
yearPrefix = date.getFullYear().toString().slice(-1);
yearPrefix = date.getUTCFullYear().toString().slice(-1);
}
return `${yearPrefix}${dayOfYear.toString()}`;
};
Expand Down

0 comments on commit f8e7486

Please sign in to comment.