posix.js 11 KB

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