DateTime.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /***
  2. MochiKit.DateTime 1.4
  3. See <http://mochikit.com/> for documentation, downloads, license, etc.
  4. (c) 2005 Bob Ippolito. All rights Reserved.
  5. ***/
  6. if (typeof(dojo) != 'undefined') {
  7. dojo.provide('MochiKit.DateTime');
  8. }
  9. if (typeof(MochiKit) == 'undefined') {
  10. MochiKit = {};
  11. }
  12. if (typeof(MochiKit.DateTime) == 'undefined') {
  13. MochiKit.DateTime = {};
  14. }
  15. MochiKit.DateTime.NAME = "MochiKit.DateTime";
  16. MochiKit.DateTime.VERSION = "1.4";
  17. MochiKit.DateTime.__repr__ = function () {
  18. return "[" + this.NAME + " " + this.VERSION + "]";
  19. };
  20. MochiKit.DateTime.toString = function () {
  21. return this.__repr__();
  22. };
  23. /** @id MochiKit.DateTime.isoDate */
  24. MochiKit.DateTime.isoDate = function (str) {
  25. str = str + "";
  26. if (typeof(str) != "string" || str.length === 0) {
  27. return null;
  28. }
  29. var iso = str.split('-');
  30. if (iso.length === 0) {
  31. return null;
  32. }
  33. return new Date(iso[0], iso[1] - 1, iso[2]);
  34. };
  35. MochiKit.DateTime._isoRegexp = /(\d{4,})(?:-(\d{1,2})(?:-(\d{1,2})(?:[T ](\d{1,2}):(\d{1,2})(?::(\d{1,2})(?:\.(\d+))?)?(?:(Z)|([+-])(\d{1,2})(?::(\d{1,2}))?)?)?)?)?/;
  36. /** @id MochiKit.DateTime.isoTimestamp */
  37. MochiKit.DateTime.isoTimestamp = function (str) {
  38. str = str + "";
  39. if (typeof(str) != "string" || str.length === 0) {
  40. return null;
  41. }
  42. var res = str.match(MochiKit.DateTime._isoRegexp);
  43. if (typeof(res) == "undefined" || res === null) {
  44. return null;
  45. }
  46. var year, month, day, hour, min, sec, msec;
  47. year = parseInt(res[1], 10);
  48. if (typeof(res[2]) == "undefined" || res[2] === '') {
  49. return new Date(year);
  50. }
  51. month = parseInt(res[2], 10) - 1;
  52. day = parseInt(res[3], 10);
  53. if (typeof(res[4]) == "undefined" || res[4] === '') {
  54. return new Date(year, month, day);
  55. }
  56. hour = parseInt(res[4], 10);
  57. min = parseInt(res[5], 10);
  58. sec = (typeof(res[6]) != "undefined" && res[6] !== '') ? parseInt(res[6], 10) : 0;
  59. if (typeof(res[7]) != "undefined" && res[7] !== '') {
  60. msec = Math.round(1000.0 * parseFloat("0." + res[7]));
  61. } else {
  62. msec = 0;
  63. }
  64. if ((typeof(res[8]) == "undefined" || res[8] === '') && (typeof(res[9]) == "undefined" || res[9] === '')) {
  65. return new Date(year, month, day, hour, min, sec, msec);
  66. }
  67. var ofs;
  68. if (typeof(res[9]) != "undefined" && res[9] !== '') {
  69. ofs = parseInt(res[10], 10) * 3600000;
  70. if (typeof(res[11]) != "undefined" && res[11] !== '') {
  71. ofs += parseInt(res[11], 10) * 60000;
  72. }
  73. if (res[9] == "-") {
  74. ofs = -ofs;
  75. }
  76. } else {
  77. ofs = 0;
  78. }
  79. return new Date(Date.UTC(year, month, day, hour, min, sec, msec) - ofs);
  80. };
  81. /** @id MochiKit.DateTime.toISOTime */
  82. MochiKit.DateTime.toISOTime = function (date, realISO/* = false */) {
  83. if (typeof(date) == "undefined" || date === null) {
  84. return null;
  85. }
  86. var hh = date.getHours();
  87. var mm = date.getMinutes();
  88. var ss = date.getSeconds();
  89. var lst = [
  90. ((realISO && (hh < 10)) ? "0" + hh : hh),
  91. ((mm < 10) ? "0" + mm : mm),
  92. ((ss < 10) ? "0" + ss : ss)
  93. ];
  94. return lst.join(":");
  95. };
  96. /** @id MochiKit.DateTime.toISOTimeStamp */
  97. MochiKit.DateTime.toISOTimestamp = function (date, realISO/* = false*/) {
  98. if (typeof(date) == "undefined" || date === null) {
  99. return null;
  100. }
  101. var sep = realISO ? "T" : " ";
  102. var foot = realISO ? "Z" : "";
  103. if (realISO) {
  104. date = new Date(date.getTime() + (date.getTimezoneOffset() * 60000));
  105. }
  106. return MochiKit.DateTime.toISODate(date) + sep + MochiKit.DateTime.toISOTime(date, realISO) + foot;
  107. };
  108. /** @id MochiKit.DateTime.toISODate */
  109. MochiKit.DateTime.toISODate = function (date) {
  110. if (typeof(date) == "undefined" || date === null) {
  111. return null;
  112. }
  113. var _padTwo = MochiKit.DateTime._padTwo;
  114. return [
  115. date.getFullYear(),
  116. _padTwo(date.getMonth() + 1),
  117. _padTwo(date.getDate())
  118. ].join("-");
  119. };
  120. /** @id MochiKit.DateTime.americanDate */
  121. MochiKit.DateTime.americanDate = function (d) {
  122. d = d + "";
  123. if (typeof(d) != "string" || d.length === 0) {
  124. return null;
  125. }
  126. var a = d.split('/');
  127. return new Date(a[2], a[0] - 1, a[1]);
  128. };
  129. MochiKit.DateTime._padTwo = function (n) {
  130. return (n > 9) ? n : "0" + n;
  131. };
  132. /** @id MochiKit.DateTime.toPaddedAmericanDate */
  133. MochiKit.DateTime.toPaddedAmericanDate = function (d) {
  134. if (typeof(d) == "undefined" || d === null) {
  135. return null;
  136. }
  137. var _padTwo = MochiKit.DateTime._padTwo;
  138. return [
  139. _padTwo(d.getMonth() + 1),
  140. _padTwo(d.getDate()),
  141. d.getFullYear()
  142. ].join('/');
  143. };
  144. /** @id MochiKit.DateTime.toAmericanDate */
  145. MochiKit.DateTime.toAmericanDate = function (d) {
  146. if (typeof(d) == "undefined" || d === null) {
  147. return null;
  148. }
  149. return [d.getMonth() + 1, d.getDate(), d.getFullYear()].join('/');
  150. };
  151. MochiKit.DateTime.EXPORT = [
  152. "isoDate",
  153. "isoTimestamp",
  154. "toISOTime",
  155. "toISOTimestamp",
  156. "toISODate",
  157. "americanDate",
  158. "toPaddedAmericanDate",
  159. "toAmericanDate"
  160. ];
  161. MochiKit.DateTime.EXPORT_OK = [];
  162. MochiKit.DateTime.EXPORT_TAGS = {
  163. ":common": MochiKit.DateTime.EXPORT,
  164. ":all": MochiKit.DateTime.EXPORT
  165. };
  166. MochiKit.DateTime.__new__ = function () {
  167. // MochiKit.Base.nameFunctions(this);
  168. var base = this.NAME + ".";
  169. for (var k in this) {
  170. var o = this[k];
  171. if (typeof(o) == 'function' && typeof(o.NAME) == 'undefined') {
  172. try {
  173. o.NAME = base + k;
  174. } catch (e) {
  175. // pass
  176. }
  177. }
  178. }
  179. };
  180. MochiKit.DateTime.__new__();
  181. if (typeof(MochiKit.Base) != "undefined") {
  182. MochiKit.Base._exportSymbols(this, MochiKit.DateTime);
  183. } else {
  184. (function (globals, module) {
  185. if ((typeof(JSAN) == 'undefined' && typeof(dojo) == 'undefined')
  186. || (MochiKit.__export__ === false)) {
  187. var all = module.EXPORT_TAGS[":all"];
  188. for (var i = 0; i < all.length; i++) {
  189. globals[all[i]] = module[all[i]];
  190. }
  191. }
  192. })(this, MochiKit.DateTime);
  193. }