stamp.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. define("dojo/date/stamp", ["../_base/kernel", "../_base/lang", "../_base/array"], function(dojo, lang, array) {
  2. // module:
  3. // dojo/date/stamp
  4. // summary:
  5. // TODOC
  6. lang.getObject("date.stamp", true, dojo);
  7. // Methods to convert dates to or from a wire (string) format using well-known conventions
  8. dojo.date.stamp.fromISOString = function(/*String*/formattedString, /*Number?*/defaultTime){
  9. // summary:
  10. // Returns a Date object given a string formatted according to a subset of the ISO-8601 standard.
  11. //
  12. // description:
  13. // Accepts a string formatted according to a profile of ISO8601 as defined by
  14. // [RFC3339](http://www.ietf.org/rfc/rfc3339.txt), except that partial input is allowed.
  15. // Can also process dates as specified [by the W3C](http://www.w3.org/TR/NOTE-datetime)
  16. // The following combinations are valid:
  17. //
  18. // * dates only
  19. // | * yyyy
  20. // | * yyyy-MM
  21. // | * yyyy-MM-dd
  22. // * times only, with an optional time zone appended
  23. // | * THH:mm
  24. // | * THH:mm:ss
  25. // | * THH:mm:ss.SSS
  26. // * and "datetimes" which could be any combination of the above
  27. //
  28. // timezones may be specified as Z (for UTC) or +/- followed by a time expression HH:mm
  29. // Assumes the local time zone if not specified. Does not validate. Improperly formatted
  30. // input may return null. Arguments which are out of bounds will be handled
  31. // by the Date constructor (e.g. January 32nd typically gets resolved to February 1st)
  32. // Only years between 100 and 9999 are supported.
  33. //
  34. // formattedString:
  35. // A string such as 2005-06-30T08:05:00-07:00 or 2005-06-30 or T08:05:00
  36. //
  37. // defaultTime:
  38. // Used for defaults for fields omitted in the formattedString.
  39. // Uses 1970-01-01T00:00:00.0Z by default.
  40. if(!dojo.date.stamp._isoRegExp){
  41. dojo.date.stamp._isoRegExp =
  42. //TODO: could be more restrictive and check for 00-59, etc.
  43. /^(?:(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(.\d+)?)?((?:[+-](\d{2}):(\d{2}))|Z)?)?$/;
  44. }
  45. var match = dojo.date.stamp._isoRegExp.exec(formattedString),
  46. result = null;
  47. if(match){
  48. match.shift();
  49. if(match[1]){match[1]--;} // Javascript Date months are 0-based
  50. if(match[6]){match[6] *= 1000;} // Javascript Date expects fractional seconds as milliseconds
  51. if(defaultTime){
  52. // mix in defaultTime. Relatively expensive, so use || operators for the fast path of defaultTime === 0
  53. defaultTime = new Date(defaultTime);
  54. array.forEach(array.map(["FullYear", "Month", "Date", "Hours", "Minutes", "Seconds", "Milliseconds"], function(prop){
  55. return defaultTime["get" + prop]();
  56. }), function(value, index){
  57. match[index] = match[index] || value;
  58. });
  59. }
  60. result = new Date(match[0]||1970, match[1]||0, match[2]||1, match[3]||0, match[4]||0, match[5]||0, match[6]||0); //TODO: UTC defaults
  61. if(match[0] < 100){
  62. result.setFullYear(match[0] || 1970);
  63. }
  64. var offset = 0,
  65. zoneSign = match[7] && match[7].charAt(0);
  66. if(zoneSign != 'Z'){
  67. offset = ((match[8] || 0) * 60) + (Number(match[9]) || 0);
  68. if(zoneSign != '-'){ offset *= -1; }
  69. }
  70. if(zoneSign){
  71. offset -= result.getTimezoneOffset();
  72. }
  73. if(offset){
  74. result.setTime(result.getTime() + offset * 60000);
  75. }
  76. }
  77. return result; // Date or null
  78. };
  79. /*=====
  80. dojo.date.stamp.__Options = function(){
  81. // selector: String
  82. // "date" or "time" for partial formatting of the Date object.
  83. // Both date and time will be formatted by default.
  84. // zulu: Boolean
  85. // if true, UTC/GMT is used for a timezone
  86. // milliseconds: Boolean
  87. // if true, output milliseconds
  88. this.selector = selector;
  89. this.zulu = zulu;
  90. this.milliseconds = milliseconds;
  91. }
  92. =====*/
  93. dojo.date.stamp.toISOString = function(/*Date*/dateObject, /*dojo.date.stamp.__Options?*/options){
  94. // summary:
  95. // Format a Date object as a string according a subset of the ISO-8601 standard
  96. //
  97. // description:
  98. // When options.selector is omitted, output follows [RFC3339](http://www.ietf.org/rfc/rfc3339.txt)
  99. // The local time zone is included as an offset from GMT, except when selector=='time' (time without a date)
  100. // Does not check bounds. Only years between 100 and 9999 are supported.
  101. //
  102. // dateObject:
  103. // A Date object
  104. var _ = function(n){ return (n < 10) ? "0" + n : n; };
  105. options = options || {};
  106. var formattedDate = [],
  107. getter = options.zulu ? "getUTC" : "get",
  108. date = "";
  109. if(options.selector != "time"){
  110. var year = dateObject[getter+"FullYear"]();
  111. date = ["0000".substr((year+"").length)+year, _(dateObject[getter+"Month"]()+1), _(dateObject[getter+"Date"]())].join('-');
  112. }
  113. formattedDate.push(date);
  114. if(options.selector != "date"){
  115. var time = [_(dateObject[getter+"Hours"]()), _(dateObject[getter+"Minutes"]()), _(dateObject[getter+"Seconds"]())].join(':');
  116. var millis = dateObject[getter+"Milliseconds"]();
  117. if(options.milliseconds){
  118. time += "."+ (millis < 100 ? "0" : "") + _(millis);
  119. }
  120. if(options.zulu){
  121. time += "Z";
  122. }else if(options.selector != "time"){
  123. var timezoneOffset = dateObject.getTimezoneOffset();
  124. var absOffset = Math.abs(timezoneOffset);
  125. time += (timezoneOffset > 0 ? "-" : "+") +
  126. _(Math.floor(absOffset/60)) + ":" + _(absOffset%60);
  127. }
  128. formattedDate.push(time);
  129. }
  130. return formattedDate.join('T'); // String
  131. };
  132. return dojo.date.stamp;
  133. });