DateTime.js.svn-base 5.7 KB

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