Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeZone prop to IntlProvider #893

Merged
merged 9 commits into from
Sep 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions examples/explicit-timezone/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
node_modules

# testing
coverage

# production
build

# misc
.DS_Store
.env
npm-debug.log
13 changes: 13 additions & 0 deletions examples/explicit-timezone/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
React Intl Explicit Timezone Example
==============================

This app demonstrates the ability to explicitly pass a timeZone to IntlProvider so that times and dates are formatted accordingly

## Running Example

**In the project directory, run:**
```
$ npm install
$ npm start
```
**Open [http://localhost:3000](http://localhost:3000) to view it in the browser.**
22 changes: 22 additions & 0 deletions examples/explicit-timezone/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "react-intl-example-explicit-timezone",
"version": "1.0.0",
"description": "React Intl Explicit Timezone Example",
"private": true,
"main": "index.js",
"author": "Bryan Richards <brichardssa@gmail.com>",
"license": "BSD-3-Clause",
"devDependencies": {
"react-scripts": "0.6.0"
},
"dependencies": {
"prop-types": "^15.5.4",
"react": "^15.5.4",
"react-dom": "^15.5.4",
"react-intl": "^2.3.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build"
}
}
12 changes: 12 additions & 0 deletions examples/explicit-timezone/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>React Intl Explicit Timezone Example</title>
</head>
<body>
<div id="root"></div>
<script src="https://cdn.polyfill.io/v1/polyfill.min.js?features=Intl.~locale.en"></script>
</body>
</html>
25 changes: 25 additions & 0 deletions examples/explicit-timezone/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {FormattedDate, FormattedTime} from 'react-intl';

class App extends Component {
render() {
const {currentTime} = this.props;

return <p>
The date in Tokyo is: <FormattedDate value={currentTime} />
<br />
The time in Tokyo is: <FormattedTime value={currentTime} />
</p>;
}
}

App.propTypes = {
currentTime: PropTypes.object,
};

App.defaultProps = {
currentTime: new Date(),
};

export default App;
11 changes: 11 additions & 0 deletions examples/explicit-timezone/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';
import ReactDOM from 'react-dom';
import {IntlProvider} from 'react-intl';
import App from './App';

ReactDOM.render(
<IntlProvider locale="en" timeZone="Asia/Tokyo">
<App />
</IntlProvider>,
document.getElementById('root')
);
1 change: 1 addition & 0 deletions src/components/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const intlFormatPropNames = Object.keys(intlFormatPropTypes);
const defaultProps = {
formats: {},
messages: {},
timeZone: null,
textComponent: 'span',

defaultLocale: 'en',
Expand Down
26 changes: 12 additions & 14 deletions src/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,15 @@ function getNamedFormat(formats, type, name) {
}

export function formatDate(config, state, value, options = {}) {
const {locale, formats} = config;
const {locale, formats, timeZone} = config;
const {format} = options;

let date = new Date(value);
let defaults = format && getNamedFormat(formats, 'date', format);
let filteredOptions = filterProps(
options,
DATE_TIME_FORMAT_OPTIONS,
defaults
);
let defaults = {
...(timeZone && {timeZone}),
...(format && getNamedFormat(formats, 'date', format)),
};
let filteredOptions = filterProps(options, DATE_TIME_FORMAT_OPTIONS, defaults);

try {
return state.getDateTimeFormat(locale, filteredOptions).format(date);
Expand All @@ -75,16 +74,15 @@ export function formatDate(config, state, value, options = {}) {
}

export function formatTime(config, state, value, options = {}) {
const {locale, formats} = config;
const {locale, formats, timeZone} = config;
const {format} = options;

let date = new Date(value);
let defaults = format && getNamedFormat(formats, 'time', format);
let filteredOptions = filterProps(
options,
DATE_TIME_FORMAT_OPTIONS,
defaults
);
let defaults = {
...(timeZone && {timeZone}),
...(format && getNamedFormat(formats, 'time', format)),
};
let filteredOptions = filterProps(options, DATE_TIME_FORMAT_OPTIONS, defaults);

if (
!filteredOptions.hour &&
Expand Down
1 change: 1 addition & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const funcReq = func.isRequired;

export const intlConfigPropTypes = {
locale: string,
timeZone: string,
formats: object,
messages: object,
textComponent: any,
Expand Down
25 changes: 23 additions & 2 deletions test/unit/components/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('<IntlProvider>', () => {
);
});

it('renderes its `children`', () => {
it('renders its `children`', () => {
const el = (
<IntlProvider locale="en">
<Child />
Expand All @@ -153,13 +153,14 @@ describe('<IntlProvider>', () => {
const {intl} = renderer.getMountedInstance().getChildContext();

INTL_SHAPE_PROP_NAMES.forEach((propName) => {
expect(intl[propName]).toExist(`Missing context.intl prop: ${propName}`);
expect(intl[propName]).toNotBe(undefined, `Missing context.intl prop: ${propName}`);
});
});

it('provides `context.intl` with values from intl config props', () => {
const props = {
locale : 'fr-FR',
timeZone : 'UTC',
formats : {},
messages : {},
textComponent: 'span',
Expand All @@ -182,6 +183,23 @@ describe('<IntlProvider>', () => {
});
});

it('provides `context.intl` with timeZone from intl config props when it is specified', () => {
const props = {
timeZone: 'Europe/Paris',
};

const el = (
<IntlProvider {...props}>
<Child />
</IntlProvider>
);

renderer.render(el);
const {intl} = renderer.getMountedInstance().getChildContext();

expect(intl.timeZone).toBe('Europe/Paris');
});

it('provides `context.intl` with values from `defaultProps` for missing or undefined props', () => {
const props = {
locale: 'en-US',
Expand Down Expand Up @@ -236,6 +254,7 @@ describe('<IntlProvider>', () => {
it('inherits from an <IntlProvider> ancestor', () => {
const props = {
locale : 'en',
timeZone: 'UTC',
formats : {
date: {
'year-only': {
Expand Down Expand Up @@ -279,6 +298,7 @@ describe('<IntlProvider>', () => {
it('shadows inherited intl config props from an <IntlProvider> ancestor', () => {
const props = {
locale : 'en',
timeZone : 'Australia/Adelaide',
formats : {
date: {
'year-only': {
Expand All @@ -305,6 +325,7 @@ describe('<IntlProvider>', () => {
const el = (
<IntlProvider
locale="fr"
timeZone="Atlantic/Azores"
formats={{}}
messages={{}}
defaultLocale="en"
Expand Down
58 changes: 58 additions & 0 deletions test/unit/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,22 @@ describe('format API', () => {
expect(formatDate(timestamp)).toBe(df.format(timestamp));
});

it('uses the time zone specified by the provider', () => {
const timestamp = Date.now();
config.timeZone = 'Pacific/Wake';
formatDate = f.formatDate.bind(null, config, state);
const wakeDf = new Intl.DateTimeFormat(config.locale, {
timeZone: 'Pacific/Wake',
});
expect(formatDate(timestamp)).toBe(wakeDf.format(timestamp));
config.timeZone = 'Asia/Shanghai';
formatDate = f.formatDate.bind(null, config, state);
const shanghaiDf = new Intl.DateTimeFormat(config.locale, {
timeZone: 'Asia/Shanghai',
});
expect(formatDate(timestamp)).toBe(shanghaiDf.format(timestamp));
});

describe('options', () => {
it('accepts empty options', () => {
expect(formatDate(0, {})).toBe(df.format(0));
Expand Down Expand Up @@ -187,6 +203,16 @@ describe('format API', () => {
`[React Intl] No date format named: ${format}`
);
});

it('uses time zone specified in options over the one passed through by the provider', () => {
const timestamp = Date.now();
config.timeZone = 'Pacific/Wake';
formatDate = f.formatDate.bind(null, config, state);
const shanghaiDf = new Intl.DateTimeFormat(config.locale, {
timeZone: 'Asia/Shanghai',
});
expect(formatDate(timestamp, {timeZone: 'Asia/Shanghai'})).toBe(shanghaiDf.format(timestamp));
});
});
});

Expand Down Expand Up @@ -236,6 +262,26 @@ describe('format API', () => {
expect(formatTime(timestamp)).toBe(df.format(timestamp));
});

it('uses the time zone specified by the provider', () => {
const timestamp = Date.now();
config.timeZone = 'Africa/Johannesburg';
formatTime = f.formatTime.bind(null, config, state);
const johannesburgDf = new Intl.DateTimeFormat(config.locale, {
hour: 'numeric',
minute: 'numeric',
timeZone: 'Africa/Johannesburg',
});
expect(formatTime(timestamp)).toBe(johannesburgDf.format(timestamp));
config.timeZone = 'America/Chicago';
formatTime = f.formatTime.bind(null, config, state);
const chicagoDf = new Intl.DateTimeFormat(config.locale, {
hour: 'numeric',
minute: 'numeric',
timeZone: 'America/Chicago',
});
expect(formatTime(timestamp)).toBe(chicagoDf.format(timestamp));
});

describe('options', () => {
it('accepts empty options', () => {
expect(formatTime(0, {})).toBe(df.format(0));
Expand Down Expand Up @@ -319,6 +365,18 @@ describe('format API', () => {
df = new Intl.DateTimeFormat(locale, {hour});
expect(formatTime(date, {hour})).toBe(df.format(date));
});

it('uses time zone specified in options over the one passed through by the provider', () => {
const timestamp = Date.now();
config.timeZone = 'Africa/Johannesburg';
formatTime = f.formatTime.bind(null, config, state);
const chicagoDf = new Intl.DateTimeFormat(config.locale, {
hour: 'numeric',
minute: 'numeric',
timeZone: 'America/Chicago',
});
expect(formatTime(timestamp, {timeZone: 'America/Chicago'})).toBe(chicagoDf.format(timestamp));
});
});
});

Expand Down