posix.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.date.posix"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.date.posix"] = true;
  8. dojo.provide("dojox.date.posix");
  9. dojo.require("dojo.date");
  10. dojo.require("dojo.date.locale");
  11. dojo.require("dojo.string");
  12. dojox.date.posix.strftime = function(/*Date*/dateObject, /*String*/format, /*String?*/locale){
  13. //
  14. // summary:
  15. // Formats the date object using the specifications of the POSIX strftime function
  16. //
  17. // description:
  18. // see http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html
  19. // zero pad
  20. var padChar = null;
  21. var _ = function(s, n){
  22. return dojo.string.pad(s, n || 2, padChar || "0");
  23. };
  24. var bundle = dojo.date.locale._getGregorianBundle(locale);
  25. var $ = function(property){
  26. switch(property){
  27. case "a": // abbreviated weekday name according to the current locale
  28. return dojo.date.locale.getNames('days', 'abbr', 'format', locale)[dateObject.getDay()];
  29. case "A": // full weekday name according to the current locale
  30. return dojo.date.locale.getNames('days', 'wide', 'format', locale)[dateObject.getDay()];
  31. case "b":
  32. case "h": // abbreviated month name according to the current locale
  33. return dojo.date.locale.getNames('months', 'abbr', 'format', locale)[dateObject.getMonth()];
  34. case "B": // full month name according to the current locale
  35. return dojo.date.locale.getNames('months', 'wide', 'format', locale)[dateObject.getMonth()];
  36. case "c": // preferred date and time representation for the current
  37. // locale
  38. return dojo.date.locale.format(dateObject, {formatLength: 'full', locale: locale});
  39. case "C": // century number (the year divided by 100 and truncated
  40. // to an integer, range 00 to 99)
  41. return _(Math.floor(dateObject.getFullYear()/100));
  42. case "d": // day of the month as a decimal number (range 01 to 31)
  43. return _(dateObject.getDate());
  44. case "D": // same as %m/%d/%y
  45. return $("m") + "/" + $("d") + "/" + $("y");
  46. case "e": // day of the month as a decimal number, a single digit is
  47. // preceded by a space (range ' 1' to '31')
  48. if(padChar == null){ padChar = " "; }
  49. return _(dateObject.getDate());
  50. case "f": // month as a decimal number, a single digit is
  51. // preceded by a space (range ' 1' to '12')
  52. if(padChar == null){ padChar = " "; }
  53. return _(dateObject.getMonth()+1);
  54. case "g": // like %G, but without the century.
  55. break;
  56. case "G": // The 4-digit year corresponding to the ISO week number
  57. // (see %V). This has the same format and value as %Y,
  58. // except that if the ISO week number belongs to the
  59. // previous or next year, that year is used instead.
  60. dojo.unimplemented("unimplemented modifier 'G'");
  61. break;
  62. case "F": // same as %Y-%m-%d
  63. return $("Y") + "-" + $("m") + "-" + $("d");
  64. case "H": // hour as a decimal number using a 24-hour clock (range
  65. // 00 to 23)
  66. return _(dateObject.getHours());
  67. case "I": // hour as a decimal number using a 12-hour clock (range
  68. // 01 to 12)
  69. return _(dateObject.getHours() % 12 || 12);
  70. case "j": // day of the year as a decimal number (range 001 to 366)
  71. return _(dojo.date.locale._getDayOfYear(dateObject), 3);
  72. case "k": // Hour as a decimal number using a 24-hour clock (range
  73. // 0 to 23 (space-padded))
  74. if(padChar == null){ padChar = " "; }
  75. return _(dateObject.getHours());
  76. case "l": // Hour as a decimal number using a 12-hour clock (range
  77. // 1 to 12 (space-padded))
  78. if(padChar == null){ padChar = " "; }
  79. return _(dateObject.getHours() % 12 || 12);
  80. case "m": // month as a decimal number (range 01 to 12)
  81. return _(dateObject.getMonth() + 1);
  82. case "M": // minute as a decimal number
  83. return _(dateObject.getMinutes());
  84. case "n":
  85. return "\n";
  86. case "p": // either `am' or `pm' according to the given time value,
  87. // or the corresponding strings for the current locale
  88. return bundle['dayPeriods-format-wide-' + (dateObject.getHours() < 12 ? "am" : "pm")];
  89. case "r": // time in a.m. and p.m. notation
  90. return $("I") + ":" + $("M") + ":" + $("S") + " " + $("p");
  91. case "R": // time in 24 hour notation
  92. return $("H") + ":" + $("M");
  93. case "S": // second as a decimal number
  94. return _(dateObject.getSeconds());
  95. case "t":
  96. return "\t";
  97. case "T": // current time, equal to %H:%M:%S
  98. return $("H") + ":" + $("M") + ":" + $("S");
  99. case "u": // weekday as a decimal number [1,7], with 1 representing
  100. // Monday
  101. return String(dateObject.getDay() || 7);
  102. case "U": // week number of the current year as a decimal number,
  103. // starting with the first Sunday as the first day of the
  104. // first week
  105. return _(dojo.date.locale._getWeekOfYear(dateObject));
  106. case "V": // week number of the year (Monday as the first day of the
  107. // week) as a decimal number [01,53]. If the week containing
  108. // 1 January has four or more days in the new year, then it
  109. // is considered week 1. Otherwise, it is the last week of
  110. // the previous year, and the next week is week 1.
  111. return _(dojox.date.posix.getIsoWeekOfYear(dateObject));
  112. case "W": // week number of the current year as a decimal number,
  113. // starting with the first Monday as the first day of the
  114. // first week
  115. return _(dojo.date.locale._getWeekOfYear(dateObject, 1));
  116. case "w": // day of the week as a decimal, Sunday being 0
  117. return String(dateObject.getDay());
  118. case "x": // preferred date representation for the current locale
  119. // without the time
  120. return dojo.date.locale.format(dateObject, {selector:'date', formatLength: 'full', locale:locale});
  121. case "X": // preferred time representation for the current locale
  122. // without the date
  123. return dojo.date.locale.format(dateObject, {selector:'time', formatLength: 'full', locale:locale});
  124. case "y": // year as a decimal number without a century (range 00 to
  125. // 99)
  126. return _(dateObject.getFullYear()%100);
  127. case "Y": // year as a decimal number including the century
  128. return String(dateObject.getFullYear());
  129. case "z": // time zone or name or abbreviation
  130. var timezoneOffset = dateObject.getTimezoneOffset();
  131. return (timezoneOffset > 0 ? "-" : "+") +
  132. _(Math.floor(Math.abs(timezoneOffset)/60)) + ":" +
  133. _(Math.abs(timezoneOffset)%60);
  134. case "Z": // time zone or name or abbreviation
  135. return dojo.date.getTimezoneName(dateObject);
  136. case "%":
  137. return "%";
  138. }
  139. };
  140. // parse the formatting string and construct the resulting string
  141. var string = "";
  142. var i = 0;
  143. var index = 0;
  144. var switchCase = null;
  145. while ((index = format.indexOf("%", i)) != -1){
  146. string += format.substring(i, index++);
  147. // inspect modifier flag
  148. switch (format.charAt(index++)) {
  149. case "_": // Pad a numeric result string with spaces.
  150. padChar = " "; break;
  151. case "-": // Do not pad a numeric result string.
  152. padChar = ""; break;
  153. case "0": // Pad a numeric result string with zeros.
  154. padChar = "0"; break;
  155. case "^": // Convert characters in result string to uppercase.
  156. switchCase = "upper"; break;
  157. case "*": // Convert characters in result string to lowercase
  158. switchCase = "lower"; break;
  159. case "#": // Swap the case of the result string.
  160. switchCase = "swap"; break;
  161. default: // no modifier flag so decrement the index
  162. padChar = null; index--; break;
  163. }
  164. // toggle case if a flag is set
  165. var property = $(format.charAt(index++));
  166. switch (switchCase){
  167. case "upper":
  168. property = property.toUpperCase();
  169. break;
  170. case "lower":
  171. property = property.toLowerCase();
  172. break;
  173. case "swap": // Upper to lower, and versey-vicea
  174. var compareString = property.toLowerCase();
  175. var swapString = '';
  176. var ch = '';
  177. for (var j = 0; j < property.length; j++){
  178. ch = property.charAt(j);
  179. swapString += (ch == compareString.charAt(j)) ?
  180. ch.toUpperCase() : ch.toLowerCase();
  181. }
  182. property = swapString;
  183. break;
  184. default:
  185. break;
  186. }
  187. switchCase = null;
  188. string += property;
  189. i = index;
  190. }
  191. string += format.substring(i);
  192. return string; // String
  193. };
  194. dojox.date.posix.getStartOfWeek = function(/*Date*/dateObject, /*Number*/firstDay){
  195. // summary: Return a date object representing the first day of the given
  196. // date's week.
  197. if(isNaN(firstDay)){
  198. firstDay = dojo.cldr.supplemental.getFirstDayOfWeek ? dojo.cldr.supplemental.getFirstDayOfWeek() : 0;
  199. }
  200. var offset = firstDay;
  201. if(dateObject.getDay() >= firstDay){
  202. offset -= dateObject.getDay();
  203. }else{
  204. offset -= (7 - dateObject.getDay());
  205. }
  206. var date = new Date(dateObject);
  207. date.setHours(0, 0, 0, 0);
  208. return dojo.date.add(date, "day", offset); // Date
  209. }
  210. dojox.date.posix.setIsoWeekOfYear = function(/*Date*/dateObject, /*Number*/week){
  211. // summary: Set the ISO8601 week number of the given date.
  212. // The week containing January 4th is the first week of the year.
  213. // week:
  214. // can be positive or negative: -1 is the year's last week.
  215. if(!week){ return dateObject; }
  216. var currentWeek = dojox.date.posix.getIsoWeekOfYear(dateObject);
  217. var offset = week - currentWeek;
  218. if(week < 0){
  219. var weeks = dojox.date.posix.getIsoWeeksInYear(dateObject);
  220. offset = (weeks + week + 1) - currentWeek;
  221. }
  222. return dojo.date.add(dateObject, "week", offset); // Date
  223. }
  224. dojox.date.posix.getIsoWeekOfYear = function(/*Date*/dateObject){
  225. // summary: Get the ISO8601 week number of the given date.
  226. // The week containing January 4th is the first week of the year.
  227. // See http://en.wikipedia.org/wiki/ISO_week_date
  228. var weekStart = dojox.date.posix.getStartOfWeek(dateObject, 1);
  229. var yearStart = new Date(dateObject.getFullYear(), 0, 4); // January 4th
  230. yearStart = dojox.date.posix.getStartOfWeek(yearStart, 1);
  231. var diff = weekStart.getTime() - yearStart.getTime();
  232. if(diff < 0){ return dojox.date.posix.getIsoWeeksInYear(weekStart); } // Integer
  233. return Math.ceil(diff / 604800000) + 1; // Integer
  234. }
  235. dojox.date.posix.getIsoWeeksInYear = function(/*Date*/dateObject) {
  236. // summary: Determine the number of ISO8601 weeks in the year of the given
  237. // date. Most years have 52 but some have 53.
  238. // See http://www.phys.uu.nl/~vgent/calendar/isocalendar_text3.htm
  239. function p(y) {
  240. return y + Math.floor(y/4) - Math.floor(y/100) + Math.floor(y/400);
  241. }
  242. var y = dateObject.getFullYear();
  243. return ( p(y) % 7 == 4 || p(y-1) % 7 == 3 ) ? 53 : 52; // Integer
  244. }
  245. }