diff --git a/.prettierignore b/.prettierignore index 1306859a0c..0187784ddc 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,7 +1,9 @@ /.github/labels.yml -/.github/workflows/sync-labels.yml -/.github/workflows/no-important-files-changed.yml + +# Generated exercises/**/README.md +pnpm-lock.yaml + !/README.md # Originates from https://github.com/exercism/org-wide-files @@ -19,4 +21,4 @@ config.json # Originates from https://github.com/exercism/problem-specifications exercises/practice/**/.docs/instructions.md -exercises/practice/**/.docs/introduction.md +exercises/practice/**/.docs/introduction.md \ No newline at end of file diff --git a/exercises/concept/appointment-time/.docs/hints.md b/exercises/concept/appointment-time/.docs/hints.md index 805e162bdd..f089f650fb 100644 --- a/exercises/concept/appointment-time/.docs/hints.md +++ b/exercises/concept/appointment-time/.docs/hints.md @@ -4,7 +4,8 @@ - You need to create a new date. The introduction elaborates on the different ways. - `Date.now()` gives you current time in milliseconds -- A day consist of 24 hour. An hour consist of 60 minutes. A minute consist of 60 seconds. A second consist of 1000 milliseconds. In order to get timestamp of `n` days later from current date, you can sum current timestamp and `n * 24 * 60 * 60 * 1000`. +- `Date` has several getter methods, listed in the introduction, to get date components. Can you use one of those methods? +- Likewise, `Date` has matching setter methods to set those components, rolling over into "higher" components if needed. ## 2. Convert a date into a timestamp diff --git a/exercises/concept/appointment-time/.meta/config.json b/exercises/concept/appointment-time/.meta/config.json index 4488a8e345..649f219e32 100644 --- a/exercises/concept/appointment-time/.meta/config.json +++ b/exercises/concept/appointment-time/.meta/config.json @@ -3,6 +3,9 @@ "SalahuddinAhammed", "SleeplessByte" ], + "contributors": [ + "BadIdeaException" + ], "files": { "solution": [ "appointment-time.js" diff --git a/exercises/concept/appointment-time/.meta/exemplar.js b/exercises/concept/appointment-time/.meta/exemplar.js index ae0b84fca7..ff47aad2fc 100644 --- a/exercises/concept/appointment-time/.meta/exemplar.js +++ b/exercises/concept/appointment-time/.meta/exemplar.js @@ -9,7 +9,10 @@ * @returns {Date} the appointment */ export function createAppointment(days, now = Date.now()) { - return new Date(now + days * 24 * 3600 * 1000); + const date = new Date(now); + date.setDate(date.getDate() + days); + + return date; } /** diff --git a/exercises/concept/appointment-time/appointment-time.spec.js b/exercises/concept/appointment-time/appointment-time.spec.js index 689e2075c5..d0872cc2b3 100644 --- a/exercises/concept/appointment-time/appointment-time.spec.js +++ b/exercises/concept/appointment-time/appointment-time.spec.js @@ -10,33 +10,45 @@ import { } from './appointment-time'; describe('createAppointment', () => { - test('creates appointment 4 days in the future', () => { - const currentTime = Date.now(); - const expectedTime = currentTime + 345600 * 1000; + test('uses the passed in current time', () => { + const currentTime = Date.UTC(2000, 6, 16, 12, 0, 0, 0); + const result = createAppointment(0, currentTime); - expect(createAppointment(4, currentTime)).toEqual(new Date(expectedTime)); + expect(result).toEqual(new Date(currentTime)); }); - test('creates appointment 124 in the future', () => { + test('uses the actual current time when it is not passed in', () => { const currentTime = Date.now(); - const expectedTime = currentTime + 10713600 * 1000; + const result = createAppointment(0); - expect(createAppointment(124, currentTime)).toEqual(new Date(expectedTime)); + expect(result).toEqual(new Date(currentTime)); }); - test('uses the passed in current time', () => { - const currentTime = Date.UTC(2000, 6, 16, 12, 0, 0, 0); - const result = createAppointment(0, currentTime); + test('creates appointment without DST change', () => { + const offset = 4; // days - expect(result.getFullYear()).toEqual(2000); + const currentTime = Date.UTC(2000, 6, 1, 12, 0, 0, 0); + const expectedTime = currentTime + offset * 24 * 60 * 60 * 1000; + + expect(createAppointment(offset, currentTime)).toEqual( + new Date(expectedTime), + ); }); - test('uses the actual current time when it is not passed in', () => { - const result = createAppointment(0); + test('creates appointment with potential DST change', () => { + const offset = 180; // days + + const currentTime = Date.UTC(2000, 6, 1, 12, 0, 0, 0); + let expectedTime = currentTime + offset * 24 * 60 * 60 * 1000; + // Manually adjust for DST timezone offset: + expectedTime += + (new Date(expectedTime).getTimezoneOffset() - + new Date(currentTime).getTimezoneOffset()) * + 60 * + 1000; - expect(Math.abs(Date.now() - result.getTime())).toBeLessThanOrEqual( - // Maximum number of time zones difference - 27 * 60 * 60 * 1000, + expect(createAppointment(offset, currentTime)).toEqual( + new Date(expectedTime), ); });