Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion exercises/concept/appointment-time/.docs/hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions exercises/concept/appointment-time/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"SalahuddinAhammed",
"SleeplessByte"
],
"contributors": [
"BadIdeaException"
],
"files": {
"solution": [
"appointment-time.js"
Expand Down
5 changes: 4 additions & 1 deletion exercises/concept/appointment-time/.meta/exemplar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
44 changes: 28 additions & 16 deletions exercises/concept/appointment-time/appointment-time.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
});

Expand Down
Loading