2015-11-30 21:11:04 +03:00
|
|
|
/*
|
2016-01-07 07:06:39 +03:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2017-05-25 20:26:42 +03:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2015-11-30 21:11:04 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
2017-05-25 13:39:08 +03:00
|
|
|
import { _t } from './languageHandler';
|
2015-11-30 21:11:04 +03:00
|
|
|
|
2017-05-23 17:16:31 +03:00
|
|
|
function getDaysArray() {
|
2017-05-25 20:26:42 +03:00
|
|
|
return [
|
|
|
|
_t('Sun'),
|
|
|
|
_t('Mon'),
|
|
|
|
_t('Tue'),
|
|
|
|
_t('Wed'),
|
|
|
|
_t('Thu'),
|
|
|
|
_t('Fri'),
|
|
|
|
_t('Sat'),
|
|
|
|
];
|
2017-05-23 17:16:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function getMonthsArray() {
|
2017-05-25 20:26:42 +03:00
|
|
|
return [
|
|
|
|
_t('Jan'),
|
|
|
|
_t('Feb'),
|
|
|
|
_t('Mar'),
|
|
|
|
_t('Apr'),
|
|
|
|
_t('May'),
|
|
|
|
_t('Jun'),
|
|
|
|
_t('Jul'),
|
|
|
|
_t('Aug'),
|
|
|
|
_t('Sep'),
|
|
|
|
_t('Oct'),
|
|
|
|
_t('Nov'),
|
|
|
|
_t('Dec'),
|
|
|
|
];
|
2017-05-23 17:16:31 +03:00
|
|
|
}
|
2015-11-30 21:11:04 +03:00
|
|
|
|
2017-05-15 02:25:12 +03:00
|
|
|
function pad(n) {
|
|
|
|
return (n < 10 ? '0' : '') + n;
|
|
|
|
}
|
|
|
|
|
2017-05-20 00:24:02 +03:00
|
|
|
function twelveHourTime(date) {
|
2017-05-19 23:31:24 +03:00
|
|
|
let hours = date.getHours() % 12;
|
2017-05-20 00:26:24 +03:00
|
|
|
const minutes = pad(date.getMinutes());
|
2017-07-03 18:25:03 +03:00
|
|
|
const ampm = date.getHours() >= 12 ? _t('PM') : _t('AM');
|
2017-06-30 18:29:40 +03:00
|
|
|
hours = hours ? hours : 12; // convert 0 -> 12
|
2017-05-30 18:31:16 +03:00
|
|
|
return `${hours}:${minutes}${ampm}`;
|
2017-05-19 00:00:44 +03:00
|
|
|
}
|
|
|
|
|
2015-11-30 21:11:04 +03:00
|
|
|
module.exports = {
|
2017-06-22 17:53:58 +03:00
|
|
|
formatDate: function(date, showTwelveHour=false) {
|
2017-07-01 16:42:12 +03:00
|
|
|
const now = new Date();
|
2017-05-26 19:55:48 +03:00
|
|
|
const days = getDaysArray();
|
|
|
|
const months = getMonthsArray();
|
2015-11-30 21:11:04 +03:00
|
|
|
if (date.toDateString() === now.toDateString()) {
|
2017-09-13 15:20:24 +03:00
|
|
|
return this.formatTime(date, showTwelveHour);
|
2017-07-01 16:42:12 +03:00
|
|
|
} else if (now.getTime() - date.getTime() < 6 * 24 * 60 * 60 * 1000) {
|
2017-05-23 17:16:31 +03:00
|
|
|
// TODO: use standard date localize function provided in counterpart
|
2017-07-01 16:42:12 +03:00
|
|
|
return _t('%(weekDayName)s %(time)s', {
|
|
|
|
weekDayName: days[date.getDay()],
|
|
|
|
time: this.formatTime(date, showTwelveHour),
|
|
|
|
});
|
|
|
|
} else if (now.getFullYear() === date.getFullYear()) {
|
2017-05-23 17:16:31 +03:00
|
|
|
// TODO: use standard date localize function provided in counterpart
|
2017-05-26 19:30:02 +03:00
|
|
|
return _t('%(weekDayName)s, %(monthName)s %(day)s %(time)s', {
|
|
|
|
weekDayName: days[date.getDay()],
|
|
|
|
monthName: months[date.getMonth()],
|
|
|
|
day: date.getDate(),
|
2017-09-13 15:20:24 +03:00
|
|
|
time: this.formatTime(date, showTwelveHour),
|
2017-05-26 19:30:02 +03:00
|
|
|
});
|
2015-11-30 21:11:04 +03:00
|
|
|
}
|
2017-06-22 17:53:58 +03:00
|
|
|
return this.formatFullDate(date, showTwelveHour);
|
2017-05-15 02:25:12 +03:00
|
|
|
},
|
|
|
|
|
2017-05-30 22:11:57 +03:00
|
|
|
formatFullDate: function(date, showTwelveHour=false) {
|
2017-05-23 17:16:31 +03:00
|
|
|
const days = getDaysArray();
|
|
|
|
const months = getMonthsArray();
|
2017-05-26 19:30:02 +03:00
|
|
|
return _t('%(weekDayName)s, %(monthName)s %(day)s %(fullYear)s %(time)s', {
|
|
|
|
weekDayName: days[date.getDay()],
|
|
|
|
monthName: months[date.getMonth()],
|
|
|
|
day: date.getDate(),
|
|
|
|
fullYear: date.getFullYear(),
|
2017-09-13 15:20:24 +03:00
|
|
|
time: this.formatTime(date, showTwelveHour),
|
2017-05-26 19:30:02 +03:00
|
|
|
});
|
2016-08-16 17:01:01 +03:00
|
|
|
},
|
|
|
|
|
2017-05-20 00:24:02 +03:00
|
|
|
formatTime: function(date, showTwelveHour=false) {
|
|
|
|
if (showTwelveHour) {
|
2017-09-13 15:20:24 +03:00
|
|
|
return twelveHourTime(date);
|
2017-05-19 00:00:44 +03:00
|
|
|
}
|
2017-05-15 02:25:12 +03:00
|
|
|
return pad(date.getHours()) + ':' + pad(date.getMinutes());
|
2017-05-19 00:00:44 +03:00
|
|
|
},
|
2017-01-20 17:22:27 +03:00
|
|
|
};
|