Skip to content

Commit

Permalink
Modifies all dates in the calendar to have their time set to noon
Browse files Browse the repository at this point in the history
  • Loading branch information
Maja Wichrowska committed Oct 12, 2016
1 parent 73b3ff6 commit 5a24720
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/utils/getCalendarMonthWeeks.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export default function getCalendarMonthWeeks(month, enableOutsideDays) {
// set utc offset to get correct dates in future (when timezone changes)
const baseDate = month.clone().utcOffset(month.utcOffset());
const firstOfMonth = baseDate.clone().startOf('month');
const lastOfMonth = baseDate.clone().endOf('month');
const baseDate = month.clone();
const firstOfMonth = baseDate.clone().startOf('month').hour(12);
const lastOfMonth = baseDate.clone().endOf('month').hour(12);

const currentDay = firstOfMonth.clone();
let currentWeek = [];
Expand Down
2 changes: 1 addition & 1 deletion src/utils/toMomentObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export default function toMomentObject(dateString, customFormat) {
[DISPLAY_FORMAT, ISO_FORMAT];

const date = moment(dateString, dateFormats, true);
return date.isValid() ? date : null;
return date.isValid() ? date.hour(12) : null;
}
35 changes: 35 additions & 0 deletions test/utils/getCalendarMonthWeeks_spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import moment from 'moment';
import { expect } from 'chai';

import isSameDay from '../../src/utils/isSameDay';
import getCalendarMonthWeeks from '../../src/utils/getCalendarMonthWeeks';

const today = moment();
Expand All @@ -27,6 +28,40 @@ describe('getCalendarMonthWeeks', () => {
expect(isIncluded).to.equal(true);
});

it('all days have a time of 12PM', () => {
weeks.forEach((week) => {
week.forEach((day) => {
if (day) {
expect(day.hours()).to.equal(12);
}
});
});
});

describe.only('Daylight Savings Time issues', () => {
it('last of February does not equal first of March', () => {
const february = getCalendarMonthWeeks(today.clone().month(1));
const lastWeekOfFebruary = february[february.length - 1].filter(day => !!day);
const lastOfFebruary = lastWeekOfFebruary[lastWeekOfFebruary.length - 1];

const march = getCalendarMonthWeeks(today.clone().month(2));
const firstOfMarch = march[0].filter(day => !!day)[0];

expect(isSameDay(lastOfFebruary, firstOfMarch)).to.equal(false);
});

it('last of March does not equal first of April', () => {
const march = getCalendarMonthWeeks(today.clone().month(2));
const lastWeekOfMarch = march[march.length - 1].filter(day => !!day);
const lastOfMarch = lastWeekOfMarch[lastWeekOfMarch.length - 1];

const april = getCalendarMonthWeeks(today.clone().month(3));
const firstOfApril = april[0].filter(day => !!day)[0];

expect(isSameDay(lastOfMarch, firstOfApril)).to.equal(false);
});
});

describe('enableOutsideDays arg is false', () => {
it('first non-null element is first of the month', () => {
const firstOfMonth = today.clone().startOf('month');
Expand Down
15 changes: 15 additions & 0 deletions test/utils/toMomentObject_spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import moment from 'moment';
import { expect } from 'chai';

import isSameDay from '../../src/utils/isSameDay';
import toMomentObject from '../../src/utils/toMomentObject';

describe('toMomentObject', () => {
Expand All @@ -20,6 +21,10 @@ describe('toMomentObject', () => {
expect(toMomentObject()).to.equal(null);
});

it('output has time of 12PM', () => {
expect(toMomentObject('1991-07-13').hour()).to.equal(12);
});

it('parses custom format', () => {
const date = toMomentObject('1991---13/07', 'YYYY---DD/MM');

Expand All @@ -29,6 +34,16 @@ describe('toMomentObject', () => {
expect(date.year()).to.equal(1991);
});

describe('Daylight Savings Time issues', () => {
it('last of February does not equal first of March', () => {
expect(isSameDay(toMomentObject('2017-02-28'), toMomentObject('2017-03-01'))).to.equal(false);
});

it('last of March does not equal first of April', () => {
expect(isSameDay(toMomentObject('2017-03-31'), toMomentObject('2017-04-01'))).to.equal(false);
});
});

describe('Catalan locale', () => {
before(() => {
moment.locale('ca');
Expand Down

0 comments on commit 5a24720

Please sign in to comment.