Refactor DateUtils to ES6

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
pull/21833/head
Michael Telatynski 2018-01-10 12:00:11 +00:00
parent 9e2238e884
commit e45fcf10c7
No known key found for this signature in database
GPG Key ID: 3F879DA5AD802A5E
3 changed files with 41 additions and 44 deletions

View File

@ -15,7 +15,6 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
'use strict';
import { _t } from './languageHandler'; import { _t } from './languageHandler';
function getDaysArray() { function getDaysArray() {
@ -59,47 +58,45 @@ function twelveHourTime(date) {
return `${hours}:${minutes}${ampm}`; return `${hours}:${minutes}${ampm}`;
} }
module.exports = { export function formatDate(date, showTwelveHour=false) {
formatDate: function(date, showTwelveHour=false) { const now = new Date();
const now = new Date(); const days = getDaysArray();
const days = getDaysArray(); const months = getMonthsArray();
const months = getMonthsArray(); if (date.toDateString() === now.toDateString()) {
if (date.toDateString() === now.toDateString()) { return formatTime(date, showTwelveHour);
return this.formatTime(date, showTwelveHour); } else if (now.getTime() - date.getTime() < 6 * 24 * 60 * 60 * 1000) {
} else if (now.getTime() - date.getTime() < 6 * 24 * 60 * 60 * 1000) { // TODO: use standard date localize function provided in counterpart
// TODO: use standard date localize function provided in counterpart return _t('%(weekDayName)s %(time)s', {
return _t('%(weekDayName)s %(time)s', { weekDayName: days[date.getDay()],
weekDayName: days[date.getDay()], time: formatTime(date, showTwelveHour),
time: this.formatTime(date, showTwelveHour), });
}); } else if (now.getFullYear() === date.getFullYear()) {
} else if (now.getFullYear() === date.getFullYear()) { // TODO: use standard date localize function provided in counterpart
// TODO: use standard date localize function provided in counterpart return _t('%(weekDayName)s, %(monthName)s %(day)s %(time)s', {
return _t('%(weekDayName)s, %(monthName)s %(day)s %(time)s', {
weekDayName: days[date.getDay()],
monthName: months[date.getMonth()],
day: date.getDate(),
time: this.formatTime(date, showTwelveHour),
});
}
return this.formatFullDate(date, showTwelveHour);
},
formatFullDate: function(date, showTwelveHour=false) {
const days = getDaysArray();
const months = getMonthsArray();
return _t('%(weekDayName)s, %(monthName)s %(day)s %(fullYear)s %(time)s', {
weekDayName: days[date.getDay()], weekDayName: days[date.getDay()],
monthName: months[date.getMonth()], monthName: months[date.getMonth()],
day: date.getDate(), day: date.getDate(),
fullYear: date.getFullYear(), time: formatTime(date, showTwelveHour),
time: this.formatTime(date, showTwelveHour),
}); });
}, }
return formatFullDate(date, showTwelveHour);
}
formatTime: function(date, showTwelveHour=false) { export function formatFullDate(date, showTwelveHour=false) {
if (showTwelveHour) { const days = getDaysArray();
return twelveHourTime(date); const months = getMonthsArray();
} return _t('%(weekDayName)s, %(monthName)s %(day)s %(fullYear)s %(time)s', {
return pad(date.getHours()) + ':' + pad(date.getMinutes()); weekDayName: days[date.getDay()],
}, monthName: months[date.getMonth()],
}; day: date.getDate(),
fullYear: date.getFullYear(),
time: formatTime(date, showTwelveHour),
});
}
export function formatTime(date, showTwelveHour=false) {
if (showTwelveHour) {
return twelveHourTime(date);
}
return pad(date.getHours()) + ':' + pad(date.getMinutes());
}

View File

@ -26,7 +26,7 @@ const Velociraptor = require('../../../Velociraptor');
require('../../../VelocityBounce'); require('../../../VelocityBounce');
import { _t } from '../../../languageHandler'; import { _t } from '../../../languageHandler';
import DateUtils from '../../../DateUtils'; import {formatDate} from '../../../DateUtils';
let bounce = false; let bounce = false;
try { try {
@ -187,7 +187,7 @@ module.exports = React.createClass({
if (this.props.timestamp) { if (this.props.timestamp) {
title = _t( title = _t(
"Seen by %(userName)s at %(dateTime)s", "Seen by %(userName)s at %(dateTime)s",
{userName: this.props.member.userId, dateTime: DateUtils.formatDate(new Date(this.props.timestamp), this.props.showTwelveHour)}, {userName: this.props.member.userId, dateTime: formatDate(new Date(this.props.timestamp), this.props.showTwelveHour)},
); );
} }

View File

@ -20,7 +20,7 @@ import PropTypes from 'prop-types';
import sdk from '../../../index'; import sdk from '../../../index';
import { _t } from '../../../languageHandler'; import { _t } from '../../../languageHandler';
import MatrixClientPeg from '../../../MatrixClientPeg'; import MatrixClientPeg from '../../../MatrixClientPeg';
import DateUtils from '../../../DateUtils'; import {formatDate} from '../../../DateUtils';
export default class DevicesPanelEntry extends React.Component { export default class DevicesPanelEntry extends React.Component {
constructor(props, context) { constructor(props, context) {
@ -56,7 +56,7 @@ export default class DevicesPanelEntry extends React.Component {
let lastSeen = ""; let lastSeen = "";
if (device.last_seen_ts) { if (device.last_seen_ts) {
const lastSeenDate = DateUtils.formatDate(new Date(device.last_seen_ts)); const lastSeenDate = formatDate(new Date(device.last_seen_ts));
lastSeen = device.last_seen_ip + " @ " + lastSeen = device.last_seen_ip + " @ " +
lastSeenDate.toLocaleString(); lastSeenDate.toLocaleString();
} }