date.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. dateMonths:['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  22. dateDays:['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
  23. dateFormatZeroFirst:function (time) {
  24. if (time < 10) return '0' + time;
  25. return time;
  26. },
  27. /**
  28. * Convert timestamp to date-string 'DAY_OF_THE_WEEK, MONTH DAY, YEAR HOURS:MINUTES'
  29. * @param timestamp
  30. * @return string date
  31. */
  32. dateFormat:function (timestamp, showSeconds) {
  33. if (!validator.isValidInt(timestamp)) return timestamp;
  34. var date = new Date(timestamp);
  35. var months = this.dateMonths;
  36. var days = this.dateDays;
  37. var formattedDate = days[date.getDay()] + ', ' + months[date.getMonth()] + ' ' + this.dateFormatZeroFirst(date.getDate()) + ', ' + date.getFullYear() + ' ' + this.dateFormatZeroFirst(date.getHours()) + ':' + this.dateFormatZeroFirst(date.getMinutes());
  38. if (showSeconds) {
  39. formattedDate += ':' + this.dateFormatZeroFirst(date.getSeconds());
  40. };
  41. return formattedDate;
  42. },
  43. /**
  44. * Convert timestamp to date-string 'DAY_OF_THE_WEEK MONTH DAY YEAR'
  45. * @param timestamp
  46. * @return {*}
  47. */
  48. dateFormatShort: function(timestamp) {
  49. if (!validator.isValidInt(timestamp)) return timestamp;
  50. var date = new Date(timestamp);
  51. var today = new Date();
  52. if (date.toDateString() === today.toDateString()) {
  53. return 'Today ' + date.toLocaleTimeString();
  54. }
  55. return date.toDateString();
  56. },
  57. /**
  58. * Convert starTimestamp to 'DAY_OF_THE_WEEK, MONTH DAY, YEAR HOURS:MINUTES', except for the case: year equals 1969
  59. * @param startTimestamp
  60. * @return string startTimeSummary
  61. */
  62. startTime: function (startTimestamp) {
  63. if (!validator.isValidInt(startTimestamp)) return '';
  64. var startDate = new Date(startTimestamp);
  65. var months = this.dateMonths;
  66. var days = this.dateDays;
  67. // generate start time
  68. if (startDate.getFullYear() == 1969 || startTimestamp < 1) {
  69. return 'Not started';
  70. }
  71. var startTimeSummary = '';
  72. if (new Date(startTimestamp).setHours(0, 0, 0, 0) == new Date().setHours(0, 0, 0, 0) ) { //today
  73. startTimeSummary = 'Today ' + this.dateFormatZeroFirst(startDate.getHours()) + ':' + this.dateFormatZeroFirst(startDate.getMinutes());
  74. } else {
  75. startTimeSummary = days[startDate.getDay()] + ' ' + months[startDate.getMonth()] + ' ' + this.dateFormatZeroFirst(startDate.getDate()) + ' ' + startDate.getFullYear() + ' '
  76. + this.dateFormatZeroFirst(startDate.getHours()) + ':' + this.dateFormatZeroFirst(startDate.getMinutes());
  77. }
  78. return startTimeSummary;
  79. },
  80. /**
  81. * Provides the duration between the given start and end timestamp. If start time
  82. * not valid, duration will be ''. If end time is not valid, duration will
  83. * be till now, showing 'Lasted for xxx secs'.
  84. * @param startTimestamp
  85. * @param endTimestamp
  86. * @return string durationSummary
  87. */
  88. durationSummary: function (startTimestamp, endTimestamp) {
  89. // generate duration
  90. var durationSummary = '';
  91. var startDate = new Date(startTimestamp);
  92. var endDate = new Date(endTimestamp);
  93. var self = this;
  94. if (startDate.getFullYear() == 1969 || startTimestamp < 1) {
  95. // not started
  96. return Em.I18n.t('common.na');
  97. }
  98. if (endDate.getFullYear() != 1969 && endTimestamp > 0) {
  99. return '' + this.timingFormat(endTimestamp - startTimestamp, 1); //lasted for xx secs
  100. } else {
  101. // still running, duration till now
  102. var time = (App.dateTime() - startTimestamp) < 0? 0 : (App.dateTime() - startTimestamp) ;
  103. durationSummary = '' + this.timingFormat( time , 1);
  104. }
  105. return durationSummary;
  106. },
  107. /**
  108. * Convert time in mseconds to
  109. * 30 ms = 30 ms
  110. * 300 ms = 300 ms
  111. * 999 ms = 999 ms
  112. * 1000 ms = 1.00 secs
  113. * 3000 ms = 3.00 secs
  114. * 35000 ms = 35.00 secs
  115. * 350000 ms = 350.00 secs
  116. * 999999 ms = 999.99 secs
  117. * 1000000 ms = 16.66 mins
  118. * 3500000 secs = 58.33 mins
  119. * @param time
  120. * @param zeroValid for the case to show 0 when time is 0, not null
  121. * @return string formatted date
  122. */
  123. timingFormat:function (time, /* optional */ zeroValid) {
  124. var intTime = parseInt(time);
  125. if (zeroValid && intTime == 0) return 0 + ' secs';
  126. if (!intTime) return null;
  127. var timeStr = intTime.toString();
  128. var lengthOfNumber = timeStr.length;
  129. var oneMinMs = 60000;
  130. var oneHourMs = 3600000;
  131. var oneDayMs = 86400000;
  132. if (lengthOfNumber < 4) {
  133. return time + ' ms';
  134. } else if (lengthOfNumber < 7) {
  135. time = (time / 1000).toFixed(2);
  136. return time + ' secs';
  137. } else if (time < oneHourMs) {
  138. time = (time / oneMinMs).toFixed(2);
  139. return time + ' mins';
  140. } else if (time < oneDayMs) {
  141. time = (time / oneHourMs).toFixed(2);
  142. return time + ' hours';
  143. } else {
  144. time = (time / oneDayMs).toFixed(2);
  145. return time + ' days';
  146. }
  147. },
  148. /**
  149. * Provides the duration between the given start and end time. If start time
  150. * is not given, duration will be 0. If end time is not given, duration will
  151. * be till now.
  152. *
  153. * @param {Number} startTime Start time from epoch
  154. * @param {Number} endTime End time from epoch
  155. * @return {Number} duration
  156. */
  157. duration : function(startTime, endTime) {
  158. var duration = 0;
  159. if (startTime && startTime > 0) {
  160. if (!endTime || endTime < 1) {
  161. endTime = App.dateTime();
  162. }
  163. duration = endTime - startTime;
  164. }
  165. return duration;
  166. }
  167. };