Skip to content

Commit

Permalink
Now the date passed to DateTimeFormat is always in UTC
Browse files Browse the repository at this point in the history
  • Loading branch information
JustAndrei committed Jun 4, 2017
1 parent 2fdc0bb commit b5a9b7c
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const DatePickerExampleInternational = () => (
day: 'numeric',
month: 'long',
year: 'numeric',
timeZone: 'UTC',
}).format}
/>
</div>
Expand Down
4 changes: 3 additions & 1 deletion src/DatePicker/CalendarToolbar.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {dateUTC} from './dateUtils';
import IconButton from '../IconButton';
import NavigationChevronLeft from '../svg-icons/navigation/chevron-left';
import NavigationChevronRight from '../svg-icons/navigation/chevron-right';
Expand Down Expand Up @@ -70,7 +71,8 @@ class CalendarToolbar extends Component {
const dateTimeFormatted = new DateTimeFormat(locale, {
month: 'long',
year: 'numeric',
}).format(displayDate);
timeZone: 'UTC',
}).format(dateUTC(displayDate));

return (
<div style={styles.root}>
Expand Down
3 changes: 2 additions & 1 deletion src/DatePicker/CalendarYear.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class CalendarYear extends Component {

const yearFormated = new DateTimeFormat(locale, {
year: 'numeric',
}).format(utils.setYear(selectedDate, year));
timeZone: 'UTC',
}).format(utils.dateUTC(utils.setYear(selectedDate, year)));

const yearButton = (
<YearButton
Expand Down
7 changes: 5 additions & 2 deletions src/DatePicker/DateDisplay.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {dateUTC} from './dateUtils';
import transitions from '../styles/transitions';
import SlideInTransitionGroup from '../internal/SlideIn';

Expand Down Expand Up @@ -141,13 +142,15 @@ class DateDisplay extends Component {

const year = new DateTimeFormat(locale, {
year: 'numeric',
}).format(selectedDate);
timeZone: 'UTC',
}).format(dateUTC(selectedDate));

const dateTime = new DateTimeFormat(locale, {
month: 'short',
weekday: 'short',
day: '2-digit',
}).format(selectedDate);
timeZone: 'UTC',
}).format(dateUTC(selectedDate));

return (
<div {...other} style={prepareStyles(styles.root, style)}>
Expand Down
7 changes: 4 additions & 3 deletions src/DatePicker/DatePicker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {dateTimeFormat, formatIso, isEqualDate} from './dateUtils';
import {dateTimeFormat, formatIso, isEqualDate, dateUTC} from './dateUtils';
import DatePickerDialog from './DatePickerDialog';
import TextField from '../TextField';

Expand Down Expand Up @@ -262,7 +262,8 @@ class DatePicker extends Component {
day: 'numeric',
month: 'numeric',
year: 'numeric',
}).format(date);
timeZone: 'UTC',
}).format(dateUTC(date));
} else {
return formatIso(date);
}
Expand Down Expand Up @@ -308,7 +309,7 @@ class DatePicker extends Component {
onTouchTap={this.handleTouchTap}
ref="input"
style={textFieldStyle}
value={this.state.date ? formatDate(this.state.date) : ''}
value={this.state.date ? formatDate(dateUTC(this.state.date)) : ''}
/>
<DatePickerDialog
DateTimeFormat={DateTimeFormat}
Expand Down
5 changes: 3 additions & 2 deletions src/DatePicker/DayButton.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import Transition from '../styles/transitions';
import {isEqualDate} from './dateUtils';
import {isEqualDate, dateUTC} from './dateUtils';
import EnhancedButton from '../internal/EnhancedButton';

function getStyles(props, context, state) {
Expand Down Expand Up @@ -129,7 +129,8 @@ class DayButton extends Component {
<span style={prepareStyles(styles.label)}>
{new DateTimeFormat(locale, {
day: 'numeric',
}).format(date)}
timeZone: 'UTC',
}).format(dateUTC(date))}
</span>
</EnhancedButton>
) : (
Expand Down
9 changes: 7 additions & 2 deletions src/DatePicker/dateUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ export function getWeekArray(d, firstDayOfWeek) {
}

export function localizedWeekday(DateTimeFormat, locale, day, firstDayOfWeek) {
const weekdayFormatter = new DateTimeFormat(locale, {weekday: 'narrow'});
const weekdayFormatter = new DateTimeFormat(locale, {weekday: 'narrow', timeZone: 'UTC'});
const firstDayDate = getFirstDayOfWeek();

return weekdayFormatter.format(addDays(firstDayDate, day + firstDayOfWeek));
return weekdayFormatter.format(dateUTC(addDays(firstDayDate, day + firstDayOfWeek)));
}

// Convert date to ISO 8601 (YYYY-MM-DD) date string, accounting for current timezone
Expand Down Expand Up @@ -168,6 +168,10 @@ export function yearDiff(d1, d2) {
return ~~(monthDiff(d1, d2) / 12);
}

export function dateUTC(date) {
return new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
}

export const defaultUtils = {
getYear,
setYear,
Expand All @@ -177,4 +181,5 @@ export const defaultUtils = {
getFirstDayOfMonth,
getWeekArray,
monthDiff,
dateUTC,
};

0 comments on commit b5a9b7c

Please sign in to comment.