123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- /**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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.
- */
- var validator = require('utils/validator');
- var App = require('app');
- module.exports = {
- /**
- * List of monthes short names
- * @type {string[]}
- */
- dateMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
- /**
- * List of days short names
- * @type {string[]}
- */
- dateDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
- /**
- * Add leading zero
- *
- * @param {string} time
- * @returns {string}
- * @method dateFormatZeroFirst
- */
- dateFormatZeroFirst: function (time) {
- if (time < 10) return '0' + time;
- return "" + time;
- },
- /**
- * Convert timestamp to date-string
- * default format - 'DAY_OF_THE_WEEK, MONTH DAY, YEAR HOURS:MINUTES'
- *
- * @param {number} timestamp
- * @param {bool} format
- * @return {*} date
- * @method dateFormat
- */
- dateFormat: function (timestamp, format) {
- if (!validator.isValidInt(timestamp)) {
- return timestamp;
- }
- format = format || 'ddd, MMM DD, YYYY HH:mm';
- return moment((new Date(timestamp))).format(format);
- },
- /**
- * Convert timestamp to date-string 'DAY_OF_THE_WEEK MONTH DAY YEAR'
- *
- * @param {string} timestamp
- * @return {string}
- * @method dateFormatShort
- */
- dateFormatShort: function (timestamp) {
- if (!validator.isValidInt(timestamp)) {
- return timestamp;
- }
- var format = 'ddd MMM DD YYYY';
- var date = moment((new Date(timestamp))).format(format);
- var today = moment((new Date())).format(format);
- if (date === today) {
- return 'Today ' + (new Date(timestamp)).toLocaleTimeString();
- }
- return date;
- },
- /**
- * Convert starTimestamp to 'DAY_OF_THE_WEEK, MONTH DAY, YEAR HOURS:MINUTES', except for the case: year equals 1969
- *
- * @param {string} startTimestamp
- * @return {string} startTimeSummary
- * @method startTime
- */
- startTime: function (startTimestamp) {
- if (!validator.isValidInt(startTimestamp)) {
- return '';
- }
- var startDate = new Date(startTimestamp);
- var months = this.dateMonths;
- var days = this.dateDays;
- // generate start time
- if (startDate.getFullYear() == 1969 || startTimestamp < 1) {
- return 'Not started';
- }
- var startTimeSummary = '';
- if (new Date(startTimestamp).setHours(0, 0, 0, 0) == new Date().setHours(0, 0, 0, 0)) { //today
- startTimeSummary = 'Today ' + this.dateFormatZeroFirst(startDate.getHours()) + ':' + this.dateFormatZeroFirst(startDate.getMinutes());
- } else {
- startTimeSummary = days[startDate.getDay()] + ' ' + months[startDate.getMonth()] + ' ' +
- this.dateFormatZeroFirst(startDate.getDate()) + ' ' + startDate.getFullYear() + ' '
- + this.dateFormatZeroFirst(startDate.getHours()) + ':' + this.dateFormatZeroFirst(startDate.getMinutes());
- }
- return startTimeSummary;
- },
- /**
- * Provides the duration between the given start and end timestamp. If start time
- * not valid, duration will be ''. If end time is not valid, duration will
- * be till now, showing 'Lasted for xxx secs'.
- *
- * @param {string} startTimestamp
- * @param {string} endTimestamp
- * @return {string} durationSummary
- * @method durationSummary
- */
- durationSummary: function (startTimestamp, endTimestamp) {
- // generate duration
- var durationSummary = '';
- var startDate = new Date(startTimestamp);
- var endDate = new Date(endTimestamp);
- var self = this;
- if (startDate.getFullYear() == 1969 || startTimestamp < 1) {
- // not started
- return Em.I18n.t('common.na');
- }
- if (endDate.getFullYear() != 1969 && endTimestamp > 0) {
- return '' + this.timingFormat(endTimestamp - startTimestamp, 1); //lasted for xx secs
- } else {
- // still running, duration till now
- var time = (App.dateTime() - startTimestamp) < 0 ? 0 : (App.dateTime() - startTimestamp);
- durationSummary = '' + this.timingFormat(time, 1);
- }
- return durationSummary;
- },
- /**
- * Convert time in mseconds to
- * 30 ms = 30 ms
- * 300 ms = 300 ms
- * 999 ms = 999 ms
- * 1000 ms = 1.00 secs
- * 3000 ms = 3.00 secs
- * 35000 ms = 35.00 secs
- * 350000 ms = 350.00 secs
- * 999999 ms = 999.99 secs
- * 1000000 ms = 16.66 mins
- * 3500000 secs = 58.33 mins
- *
- * @param {number} time
- * @param {bool} [zeroValid] for the case to show 0 when time is 0, not null
- * @return {string|null} formatted date
- * @method timingFormat
- */
- timingFormat: function (time, /* optional */ zeroValid) {
- var intTime = parseInt(time);
- if (zeroValid && intTime == 0) {
- return 0 + ' secs';
- }
- if (!intTime) {
- return null;
- }
- var timeStr = intTime.toString();
- var lengthOfNumber = timeStr.length;
- var oneMinMs = 60000;
- var oneHourMs = 3600000;
- var oneDayMs = 86400000;
- if (lengthOfNumber < 4) {
- return time + ' ms';
- } else if (lengthOfNumber < 7) {
- time = (time / 1000).toFixed(2);
- return time + ' secs';
- } else if (time < oneHourMs) {
- time = (time / oneMinMs).toFixed(2);
- return time + ' mins';
- } else if (time < oneDayMs) {
- time = (time / oneHourMs).toFixed(2);
- return time + ' hours';
- } else {
- time = (time / oneDayMs).toFixed(2);
- return time + ' days';
- }
- },
- /**
- * Provides the duration between the given start and end time. If start time
- * is not given, duration will be 0. If end time is not given, duration will
- * be till now.
- *
- * @param {Number} startTime Start time from epoch
- * @param {Number} endTime End time from epoch
- * @return {Number} duration
- * @method duration
- */
- duration: function (startTime, endTime) {
- var duration = 0;
- if (startTime && startTime > 0) {
- if (!endTime || endTime < 1) {
- endTime = App.dateTime();
- }
- duration = endTime - startTime;
- }
- return duration;
- }
- };
|