TimeSpan-1.2.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*!
  2. * JavaScript TimeSpan Library
  3. *
  4. * Copyright (c) 2010 Michael Stum, http://www.Stum.de/
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. /*global window: false */
  26. "use strict";
  27. (function () {
  28. // Constructor function, all parameters are optional
  29. var TimeSpan = window.TimeSpan = function (milliseconds, seconds, minutes, hours, days) {
  30. var version = "1.2",
  31. // Millisecond-constants
  32. msecPerSecond = 1000,
  33. msecPerMinute = 60000,
  34. msecPerHour = 3600000,
  35. msecPerDay = 86400000,
  36. // Internally we store the TimeSpan as Milliseconds
  37. msecs = 0,
  38. // Helper functions
  39. isNumeric = function (input) {
  40. return !isNaN(parseFloat(input)) && isFinite(input);
  41. };
  42. // Constructor Logic
  43. if (isNumeric(days)) {
  44. msecs += (days * msecPerDay);
  45. }
  46. if (isNumeric(hours)) {
  47. msecs += (hours * msecPerHour);
  48. }
  49. if (isNumeric(minutes)) {
  50. msecs += (minutes * msecPerMinute);
  51. }
  52. if (isNumeric(seconds)) {
  53. msecs += (seconds * msecPerSecond);
  54. }
  55. if (isNumeric(milliseconds)) {
  56. msecs += milliseconds;
  57. }
  58. // Addition Functions
  59. this.addMilliseconds = function (milliseconds) {
  60. if (!isNumeric(milliseconds)) {
  61. return;
  62. }
  63. msecs += milliseconds;
  64. };
  65. this.addSeconds = function (seconds) {
  66. if (!isNumeric(seconds)) {
  67. return;
  68. }
  69. msecs += (seconds * msecPerSecond);
  70. };
  71. this.addMinutes = function (minutes) {
  72. if (!isNumeric(minutes)) {
  73. return;
  74. }
  75. msecs += (minutes * msecPerMinute);
  76. };
  77. this.addHours = function (hours) {
  78. if (!isNumeric(hours)) {
  79. return;
  80. }
  81. msecs += (hours * msecPerHour);
  82. };
  83. this.addDays = function (days) {
  84. if (!isNumeric(days)) {
  85. return;
  86. }
  87. msecs += (days * msecPerDay);
  88. };
  89. // Subtraction Functions
  90. this.subtractMilliseconds = function (milliseconds) {
  91. if (!isNumeric(milliseconds)) {
  92. return;
  93. }
  94. msecs -= milliseconds;
  95. };
  96. this.subtractSeconds = function (seconds) {
  97. if (!isNumeric(seconds)) {
  98. return;
  99. }
  100. msecs -= (seconds * msecPerSecond);
  101. };
  102. this.subtractMinutes = function (minutes) {
  103. if (!isNumeric(minutes)) {
  104. return;
  105. }
  106. msecs -= (minutes * msecPerMinute);
  107. };
  108. this.subtractHours = function (hours) {
  109. if (!isNumeric(hours)) {
  110. return;
  111. }
  112. msecs -= (hours * msecPerHour);
  113. };
  114. this.subtractDays = function (days) {
  115. if (!isNumeric(days)) {
  116. return;
  117. }
  118. msecs -= (days * msecPerDay);
  119. };
  120. // Functions to interact with other TimeSpans
  121. this.isTimeSpan = true;
  122. this.add = function (otherTimeSpan) {
  123. if (!otherTimeSpan.isTimeSpan) {
  124. return;
  125. }
  126. msecs += otherTimeSpan.totalMilliseconds();
  127. };
  128. this.subtract = function (otherTimeSpan) {
  129. if (!otherTimeSpan.isTimeSpan) {
  130. return;
  131. }
  132. msecs -= otherTimeSpan.totalMilliseconds();
  133. };
  134. this.equals = function (otherTimeSpan) {
  135. if (!otherTimeSpan.isTimeSpan) {
  136. return;
  137. }
  138. return msecs === otherTimeSpan.totalMilliseconds();
  139. };
  140. // Getters
  141. this.totalMilliseconds = function (roundDown) {
  142. var result = msecs;
  143. if (roundDown === true) {
  144. result = Math.floor(result);
  145. }
  146. return result;
  147. };
  148. this.totalSeconds = function (roundDown) {
  149. var result = msecs / msecPerSecond;
  150. if (roundDown === true) {
  151. result = Math.floor(result);
  152. }
  153. return result;
  154. };
  155. this.totalMinutes = function (roundDown) {
  156. var result = msecs / msecPerMinute;
  157. if (roundDown === true) {
  158. result = Math.floor(result);
  159. }
  160. return result;
  161. };
  162. this.totalHours = function (roundDown) {
  163. var result = msecs / msecPerHour;
  164. if (roundDown === true) {
  165. result = Math.floor(result);
  166. }
  167. return result;
  168. };
  169. this.totalDays = function (roundDown) {
  170. var result = msecs / msecPerDay;
  171. if (roundDown === true) {
  172. result = Math.floor(result);
  173. }
  174. return result;
  175. };
  176. // Return a Fraction of the TimeSpan
  177. this.milliseconds = function () {
  178. return msecs % 1000;
  179. };
  180. this.seconds = function () {
  181. return Math.floor(msecs / msecPerSecond) % 60;
  182. };
  183. this.minutes = function () {
  184. return Math.floor(msecs / msecPerMinute) % 60;
  185. };
  186. this.hours = function () {
  187. return Math.floor(msecs / msecPerHour) % 24;
  188. };
  189. this.days = function () {
  190. return Math.floor(msecs / msecPerDay);
  191. };
  192. // Misc. Functions
  193. this.getVersion = function () {
  194. return version;
  195. };
  196. };
  197. // "Static Constructors"
  198. TimeSpan.FromSeconds = function (seconds) {
  199. return new TimeSpan(0, seconds, 0, 0, 0);
  200. };
  201. TimeSpan.FromMinutes = function (minutes) {
  202. return new TimeSpan(0, 0, minutes, 0, 0);
  203. };
  204. TimeSpan.FromHours = function (hours) {
  205. return new TimeSpan(0, 0, 0, hours, 0);
  206. };
  207. TimeSpan.FromDays = function (days) {
  208. return new TimeSpan(0, 0, 0, 0, days);
  209. };
  210. TimeSpan.FromDates = function (firstDate, secondDate, forcePositive) {
  211. var differenceMsecs = secondDate.valueOf() - firstDate.valueOf();
  212. if(forcePositive === true) {
  213. differenceMsecs = Math.abs(differenceMsecs);
  214. }
  215. return new TimeSpan(differenceMsecs, 0, 0, 0, 0);
  216. };
  217. }());