date.js 6.0 KB

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