Skip to content

Commit

Permalink
Merge pull request #1861 from BorderTech/feature/monthname-fix
Browse files Browse the repository at this point in the history
Fix monthname and dayname timezone issues
  • Loading branch information
ricksbrown authored Jun 11, 2024
2 parents 5434c2c + 21f00e8 commit e69c0ba
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions wcomponents-theme/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"files": [ "src/test/**/*" ],
"env": {
"browser": true,
"node": true,
"jasmine": true
}
}
Expand Down
2 changes: 1 addition & 1 deletion wcomponents-theme/src/main/js/wc/date/dayName.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const cache = {};
* @return {string[]}
*/
function getDayNames(locale) {
const referenceDate = new Date(Date.UTC(2000, 1, 6)); // Sunday
const referenceDate = new Date(2024, 5, 9); // Sunday
const result = [];
for (let i = 0; i < 7; i++) {
result.push(referenceDate.toLocaleDateString(locale, { weekday: "long" }));
Expand Down
8 changes: 5 additions & 3 deletions wcomponents-theme/src/main/js/wc/date/monthName.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function initialise() {
let monthsAscii = new Array(result.months.length);
result.months.forEach((mnthname, idx) => {
const ascii = asciify(mnthname);
needsAsciiVersion ||= (ascii !== mnthname);
needsAsciiVersion = needsAsciiVersion || (ascii !== mnthname);
monthsAscii[idx] = ascii;
});

Expand All @@ -48,11 +48,13 @@ function initialise() {
* @return {string[]}
*/
function getMonthNames(locale, short) {
const referenceDate = new Date(Date.UTC(2000, 0, 1)); // January
const referenceDate = new Date(2000, 0, 15); // January
const result = [];
const type = short ? "short" : "long";
for (let i = 0; i < 12; i++) {
result.push(referenceDate.toLocaleDateString(locale, { month: type }));
let name = referenceDate.toLocaleDateString(locale, { month: type });
name = name.replace(/\.$/, "");
result.push(name);
referenceDate.setMonth(referenceDate.getMonth() + 1);
}
return result;
Expand Down
11 changes: 11 additions & 0 deletions wcomponents-theme/src/test/spec/wc.date.dayName.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ describe("wc/date/dayName", function() {
let mondayWeek,
defaultWeek;

const tz = process.env.TZ;

beforeAll(() => {
// PLEASE LEAVE THE TIMEZONE SWITCH IN PLACE even if you don't understand why
process.env.TZ = "America/Los_Angeles";
});

afterAll(() => {
console.log("Resetting timezone to", tz);
});

beforeEach(() => {
defaultWeek = dayName.get();
mondayWeek = dayName.get(true);
Expand Down
13 changes: 11 additions & 2 deletions wcomponents-theme/src/test/spec/wc.date.monthName.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,20 @@ describe("wc/date/monthName", function() {
const expectedFrAscii = ["janvier", "fevrier", "mars", "avril", "mai", "juin", "juillet",
"aout", "septembre", "octobre", "novembre", "decembre"];

const expectedFrAbbrAscii = ["janv.", "fevr.", "mars", "avr.", "mai", "juin", "juil.",
"aout", "sept.", "oct.", "nov.", "dec."];
const expectedFrAbbrAscii = ["janv", "fevr", "mars", "avr", "mai", "juin", "juil",
"aout", "sept", "oct", "nov", "dec"];

const lang = globalThis.document.documentElement.lang;
const tz = process.env.TZ;

beforeAll(() => {
// PLEASE LEAVE THE TIMEZONE SWITCH IN PLACE even if you don't understand why
process.env.TZ = "America/Los_Angeles";
});

afterAll(() => {
console.log("Resetting timezone to", tz);
});

beforeEach(() => {
globalThis.document.documentElement.lang = lang;
Expand Down

0 comments on commit e69c0ba

Please sign in to comment.