Skip to content

Commit

Permalink
Merge pull request #155 from JensSpanier/master
Browse files Browse the repository at this point in the history
Fix Date to W3C string converter
  • Loading branch information
alexandercerutti authored Aug 9, 2023
2 parents 6fd0a63 + c806153 commit 00d9494
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 30 deletions.
8 changes: 4 additions & 4 deletions specs/PKPass.spec.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -712,12 +712,12 @@ describe("PKPass", () => {

describe("expiration date", () => {
it("should set a pass expiration date", () => {
pkpass.setExpirationDate(new Date(2023, 3, 10));
pkpass.setExpirationDate(new Date("2023-04-09T17:00-07:00"));

const passjsonGenerated = getGeneratedPassJson(pkpass);

expect(passjsonGenerated.expirationDate).toBe(
"2023-04-10T00:00:00Z",
"2023-04-10T00:00:00.000Z",
);
});

Expand Down Expand Up @@ -808,11 +808,11 @@ describe("PKPass", () => {

describe("relevant date", () => {
it("should set pass relevant date", () => {
pkpass.setRelevantDate(new Date(2023, 3, 10, 14, 15));
pkpass.setRelevantDate(new Date("2023-04-11T00:15+10:00"));

const passjsonGenerated = getGeneratedPassJson(pkpass);

expect(passjsonGenerated.relevantDate).toBe("2023-04-10T14:15:00Z");
expect(passjsonGenerated.relevantDate).toBe("2023-04-10T14:15:00.000Z");
});

it("should reset relevant date", () => {
Expand Down
4 changes: 2 additions & 2 deletions specs/utils.spec.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ describe("Utils", () => {
});

it("should convert a Date object to a valid W3C date", () => {
expect(processDate(new Date(2020, 6, 1, 0, 0, 0, 0))).toBe(
"2020-07-01T00:00:00Z",
expect(processDate(new Date("2020-07-01T02:00+02:00"))).toBe(
"2020-07-01T00:00:00.000Z",
);
});
});
Expand Down
25 changes: 1 addition & 24 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,11 @@ function dateToW3CString(date: Date) {
return undefined;
}

const paddedMonth = padMeTwo(date.getMonth() + 1);
const paddedDay = padMeTwo(date.getDate());
const paddedHour = padMeTwo(date.getHours());
const paddedMinutes = padMeTwo(date.getMinutes());
const paddedSeconds = padMeTwo(date.getSeconds());

/**
* Date.prototype.getTimezoneOffset returns the timezone UTC offset in
* minutes of the local machine.
*
* That value should then be used to calculate the effective timezone as
* string, but still that would be related to the machine and not to the
* specified date.
*
* For this reason we are completing date with "Z" TimeZoneDesignator (TZD)
* to say it to use local timezone.
*
* In the future we might think to integrate another parameter to represent
* a custom timezone.
*
* @see https://www.w3.org/TR/NOTE-datetime
*/

return `${date.getFullYear()}-${paddedMonth}-${paddedDay}T${paddedHour}:${paddedMinutes}:${paddedSeconds}Z`;
}

function padMeTwo(original: string | number) {
return String(original).padStart(2, "0");
return date.toISOString();
}

/**
Expand Down

0 comments on commit 00d9494

Please sign in to comment.