/tags/release/7020-RC1/aipo/war/src/main/webapp/javascript/dojo/date/stamp.xd.js

http://aipo.googlecode.com/ · JavaScript · 131 lines · 72 code · 12 blank · 47 comment · 24 complexity · 327d9b228b87f97ab2320154d3fab506 MD5 · raw file

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