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

[TimePicker] Update display of 12h clock (Fixes #1173) #1275

Merged
merged 1 commit into from
Aug 3, 2015
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
6 changes: 6 additions & 0 deletions docs/src/app/components/pages/components/time-picker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ let TimePickerPage = React.createClass({
type: 'one of: ampm, 24hr',
header: 'default: ampm',
desc: 'Tells the component to display the picker in ampm (12hr) format or 24hr format.'
},
{
name: 'pedantic',
type: 'boolean',
header: 'default: false',
desc: 'It\'s technically more correct to refer to "12 noon" and "12 midnight" rather than "12 a.m." and "12 p.m." and it avoids real confusion between different locales. By default (for compatibility reasons) TimePicker uses (12 a.m./12 p.m.) To use (noon/midnight) set pedantic={true}.'
}
]
},
Expand Down
23 changes: 17 additions & 6 deletions src/time-picker/time-picker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ let TimePicker = React.createClass({
propTypes: {
defaultTime: React.PropTypes.object,
format: React.PropTypes.oneOf(['ampm', '24hr']),
pedantic: React.PropTypes.bool,
onFocus: React.PropTypes.func,
onTouchTap: React.PropTypes.func,
onChange: React.PropTypes.func,
Expand All @@ -32,6 +33,7 @@ let TimePicker = React.createClass({
return {
defaultTime: emptyTime,
format: 'ampm',
pedantic: false,
};
},

Expand All @@ -44,23 +46,32 @@ let TimePicker = React.createClass({

formatTime(date) {
let hours = date.getHours();
let mins = date.getMinutes();
let aditional = "";
let mins = date.getMinutes().toString();

if (this.props.format === "ampm"){
let isAM = hours < 12;
hours = hours % 12;
aditional += isAM ? " am" : " pm";
hours = hours || 12;
let additional = isAM ? " am" : " pm";
hours = (hours || 12).toString();

if (mins.length < 2 ) mins = "0" + mins;

if (this.props.pedantic) {
// Treat midday/midnight specially http://www.nist.gov/pml/div688/times.cfm
if (hours === "12" && mins === "00") {
return additional === " pm" ? "12 noon" : "12 midnight";
}
}

return hours + (mins === "00" ? "" : ":" + mins) + additional;
}

hours = hours.toString();
mins = mins.toString();

if (hours.length < 2) hours = "0" + hours;
if (mins.length < 2) mins = "0" + mins;

return hours + ":" + mins + aditional;
return hours + ":" + mins;
},

render() {
Expand Down