date.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var validator = require('utils/validator');
  19. var App = require('app');
  20. module.exports = {
  21. /**
  22. * List of monthes short names
  23. * @type {string[]}
  24. */
  25. dateMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  26. /**
  27. * List of days short names
  28. * @type {string[]}
  29. */
  30. dateDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
  31. /**
  32. * Add leading zero
  33. *
  34. * @param {string} time
  35. * @returns {string}
  36. * @method dateFormatZeroFirst
  37. */
  38. dateFormatZeroFirst: function (time) {
  39. if (time < 10) return '0' + time;
  40. return "" + time;
  41. },
  42. /**
  43. * Convert timestamp to date-string
  44. * default format - 'DAY_OF_THE_WEEK, MONTH DAY, YEAR HOURS:MINUTES'
  45. *
  46. * @param {number} timestamp
  47. * @param {bool} format
  48. * @return {*} date
  49. * @method dateFormat
  50. */
  51. dateFormat: function (timestamp, format) {
  52. if (!validator.isValidInt(timestamp)) {
  53. return timestamp;
  54. }
  55. format = format || 'ddd, MMM DD, YYYY HH:mm';
  56. return moment((new Date(timestamp))).format(format);
  57. },
  58. /**
  59. * Convert timestamp to date-string 'DAY_OF_THE_WEEK MONTH DAY YEAR'
  60. *
  61. * @param {string} timestamp
  62. * @return {string}
  63. * @method dateFormatShort
  64. */
  65. dateFormatShort: function (timestamp) {
  66. if (!validator.isValidInt(timestamp)) {
  67. return timestamp;
  68. }
  69. var format = 'ddd MMM DD YYYY';
  70. var date = moment((new Date(timestamp))).format(format);
  71. var today = moment((new Date())).format(format);
  72. if (date === today) {
  73. return 'Today ' + (new Date(timestamp)).toLocaleTimeString();
  74. }
  75. return date;
  76. },
  77. /**
  78. * Convert starTimestamp to 'DAY_OF_THE_WEEK, MONTH DAY, YEAR HOURS:MINUTES', except for the case: year equals 1969
  79. *
  80. * @param {string} startTimestamp
  81. * @return {string} startTimeSummary
  82. * @method startTime
  83. */
  84. startTime: function (startTimestamp) {
  85. if (!validator.isValidInt(startTimestamp)) {
  86. return '';
  87. }
  88. var startDate = new Date(startTimestamp);
  89. var months = this.dateMonths;
  90. var days = this.dateDays;
  91. // generate start time
  92. if (startDate.getFullYear() == 1969 || startTimestamp < 1) {
  93. return 'Not started';
  94. }
  95. var startTimeSummary = '';
  96. if (new Date(startTimestamp).setHours(0, 0, 0, 0) == new Date().setHours(0, 0, 0, 0)) { //today
  97. startTimeSummary = 'Today ' + this.dateFormatZeroFirst(startDate.getHours()) + ':' + this.dateFormatZeroFirst(startDate.getMinutes());
  98. } else {
  99. startTimeSummary = days[startDate.getDay()] + ' ' + months[startDate.getMonth()] + ' ' +
  100. this.dateFormatZeroFirst(startDate.getDate()) + ' ' + startDate.getFullYear() + ' '
  101. + this.dateFormatZeroFirst(startDate.getHours()) + ':' + this.dateFormatZeroFirst(startDate.getMinutes());
  102. }
  103. return startTimeSummary;
  104. },
  105. /**
  106. * Provides the duration between the given start and end timestamp. If start time
  107. * not valid, duration will be ''. If end time is not valid, duration will
  108. * be till now, showing 'Lasted for xxx secs'.
  109. *
  110. * @param {string} startTimestamp
  111. * @param {string} endTimestamp
  112. * @return {string} durationSummary
  113. * @method durationSummary
  114. */
  115. durationSummary: function (startTimestamp, endTimestamp) {
  116. // generate duration
  117. var durationSummary = '';
  118. var startDate = new Date(startTimestamp);
  119. var endDate = new Date(endTimestamp);
  120. var self = this;
  121. if (startDate.getFullYear() == 1969 || startTimestamp < 1) {
  122. // not started
  123. return Em.I18n.t('common.na');
  124. }
  125. if (endDate.getFullYear() != 1969 && endTimestamp > 0) {
  126. return '' + this.timingFormat(endTimestamp - startTimestamp, 1); //lasted for xx secs
  127. } else {
  128. // still running, duration till now
  129. var time = (App.dateTime() - startTimestamp) < 0 ? 0 : (App.dateTime() - startTimestamp);
  130. durationSummary = '' + this.timingFormat(time, 1);
  131. }
  132. return durationSummary;
  133. },
  134. /**
  135. * Convert time in mseconds to
  136. * 30 ms = 30 ms
  137. * 300 ms = 300 ms
  138. * 999 ms = 999 ms
  139. * 1000 ms = 1.00 secs
  140. * 3000 ms = 3.00 secs
  141. * 35000 ms = 35.00 secs
  142. * 350000 ms = 350.00 secs
  143. * 999999 ms = 999.99 secs
  144. * 1000000 ms = 16.66 mins
  145. * 3500000 secs = 58.33 mins
  146. *
  147. * @param {number} time
  148. * @param {bool} [zeroValid] for the case to show 0 when time is 0, not null
  149. * @return {string|null} formatted date
  150. * @method timingFormat
  151. */
  152. timingFormat: function (time, /* optional */ zeroValid) {
  153. var intTime = parseInt(time);
  154. if (zeroValid && intTime == 0) {
  155. return 0 + ' secs';
  156. }
  157. if (!intTime) {
  158. return null;
  159. }
  160. var timeStr = intTime.toString();
  161. var lengthOfNumber = timeStr.length;
  162. var oneMinMs = 60000;
  163. var oneHourMs = 3600000;
  164. var oneDayMs = 86400000;
  165. if (lengthOfNumber < 4) {
  166. return time + ' ms';
  167. } else if (lengthOfNumber < 7) {
  168. time = (time / 1000).toFixed(2);
  169. return time + ' secs';
  170. } else if (time < oneHourMs) {
  171. time = (time / oneMinMs).toFixed(2);
  172. return time + ' mins';
  173. } else if (time < oneDayMs) {
  174. time = (time / oneHourMs).toFixed(2);
  175. return time + ' hours';
  176. } else {
  177. time = (time / oneDayMs).toFixed(2);
  178. return time + ' days';
  179. }
  180. },
  181. /**
  182. * Provides the duration between the given start and end time. If start time
  183. * is not given, duration will be 0. If end time is not given, duration will
  184. * be till now.
  185. *
  186. * @param {Number} startTime Start time from epoch
  187. * @param {Number} endTime End time from epoch
  188. * @return {Number} duration
  189. * @method duration
  190. */
  191. duration: function (startTime, endTime) {
  192. var duration = 0;
  193. if (startTime && startTime > 0) {
  194. if (!endTime || endTime < 1) {
  195. endTime = App.dateTime();
  196. }
  197. duration = endTime - startTime;
  198. }
  199. return duration;
  200. }
  201. };