PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/yui/3.13.0/datatype-date-format/datatype-date-format.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 385 lines | 235 code | 27 blank | 123 comment | 19 complexity | 1c08d26ae50d7e87430af0c07267299f MD5 | raw file
  1. YUI.add('datatype-date-format', function (Y, NAME) {
  2. /**
  3. * The `datatype` module is an alias for three utilities, Y.Date,
  4. * Y.Number and Y.XML, that provide type-conversion and string-formatting
  5. * convenience methods for various JavaScript object types.
  6. *
  7. * @module datatype
  8. * @main datatype
  9. */
  10. /**
  11. * The Date Utility provides type-conversion and string-formatting
  12. * convenience methods for Dates.
  13. *
  14. * @module datatype-date
  15. * @main datatype-date
  16. */
  17. /**
  18. * Date module.
  19. *
  20. * @module datatype-date
  21. */
  22. /**
  23. * Format date module implements strftime formatters for javascript based on the
  24. * Open Group specification defined at
  25. * http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html
  26. * This implementation does not include modified conversion specifiers (i.e., Ex and Ox)
  27. *
  28. * @module datatype-date
  29. * @submodule datatype-date-format
  30. */
  31. /**
  32. * Date provides a set of utility functions to operate against Date objects.
  33. *
  34. * @class Date
  35. * @static
  36. */
  37. /**
  38. * Pad a number with leading spaces, zeroes or something else
  39. * @method xPad
  40. * @param x {Number} The number to be padded
  41. * @param pad {String} The character to pad the number with
  42. * @param r {Number} (optional) The base of the pad, eg, 10 implies to two digits, 100 implies to 3 digits.
  43. * @private
  44. */
  45. var xPad=function (x, pad, r)
  46. {
  47. if(typeof r === "undefined")
  48. {
  49. r=10;
  50. }
  51. pad = pad + "";
  52. for( ; parseInt(x, 10)<r && r>1; r/=10) {
  53. x = pad + x;
  54. }
  55. return x.toString();
  56. };
  57. var Dt = {
  58. formats: {
  59. a: function (d, l) { return l.a[d.getDay()]; },
  60. A: function (d, l) { return l.A[d.getDay()]; },
  61. b: function (d, l) { return l.b[d.getMonth()]; },
  62. B: function (d, l) { return l.B[d.getMonth()]; },
  63. C: function (d) { return xPad(parseInt(d.getFullYear()/100, 10), 0); },
  64. d: ["getDate", "0"],
  65. e: ["getDate", " "],
  66. g: function (d) { return xPad(parseInt(Dt.formats.G(d)%100, 10), 0); },
  67. G: function (d) {
  68. var y = d.getFullYear();
  69. var V = parseInt(Dt.formats.V(d), 10);
  70. var W = parseInt(Dt.formats.W(d), 10);
  71. if(W > V) {
  72. y++;
  73. } else if(W===0 && V>=52) {
  74. y--;
  75. }
  76. return y;
  77. },
  78. H: ["getHours", "0"],
  79. I: function (d) { var I=d.getHours()%12; return xPad(I===0?12:I, 0); },
  80. j: function (d) {
  81. var gmd_1 = new Date("" + d.getFullYear() + "/1/1 GMT");
  82. var gmdate = new Date("" + d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate() + " GMT");
  83. var ms = gmdate - gmd_1;
  84. var doy = parseInt(ms/60000/60/24, 10)+1;
  85. return xPad(doy, 0, 100);
  86. },
  87. k: ["getHours", " "],
  88. l: function (d) { var I=d.getHours()%12; return xPad(I===0?12:I, " "); },
  89. m: function (d) { return xPad(d.getMonth()+1, 0); },
  90. M: ["getMinutes", "0"],
  91. p: function (d, l) { return l.p[d.getHours() >= 12 ? 1 : 0 ]; },
  92. P: function (d, l) { return l.P[d.getHours() >= 12 ? 1 : 0 ]; },
  93. s: function (d, l) { return parseInt(d.getTime()/1000, 10); },
  94. S: ["getSeconds", "0"],
  95. u: function (d) { var dow = d.getDay(); return dow===0?7:dow; },
  96. U: function (d) {
  97. var doy = parseInt(Dt.formats.j(d), 10);
  98. var rdow = 6-d.getDay();
  99. var woy = parseInt((doy+rdow)/7, 10);
  100. return xPad(woy, 0);
  101. },
  102. V: function (d) {
  103. var woy = parseInt(Dt.formats.W(d), 10);
  104. var dow1_1 = (new Date("" + d.getFullYear() + "/1/1")).getDay();
  105. // First week is 01 and not 00 as in the case of %U and %W,
  106. // so we add 1 to the final result except if day 1 of the year
  107. // is a Monday (then %W returns 01).
  108. // We also need to subtract 1 if the day 1 of the year is
  109. // Friday-Sunday, so the resulting equation becomes:
  110. var idow = woy + (dow1_1 > 4 || dow1_1 <= 1 ? 0 : 1);
  111. if(idow === 53 && (new Date("" + d.getFullYear() + "/12/31")).getDay() < 4)
  112. {
  113. idow = 1;
  114. }
  115. else if(idow === 0)
  116. {
  117. idow = Dt.formats.V(new Date("" + (d.getFullYear()-1) + "/12/31"));
  118. }
  119. return xPad(idow, 0);
  120. },
  121. w: "getDay",
  122. W: function (d) {
  123. var doy = parseInt(Dt.formats.j(d), 10);
  124. var rdow = 7-Dt.formats.u(d);
  125. var woy = parseInt((doy+rdow)/7, 10);
  126. return xPad(woy, 0, 10);
  127. },
  128. y: function (d) { return xPad(d.getFullYear()%100, 0); },
  129. Y: "getFullYear",
  130. z: function (d) {
  131. var o = d.getTimezoneOffset();
  132. var H = xPad(parseInt(Math.abs(o/60), 10), 0);
  133. var M = xPad(Math.abs(o%60), 0);
  134. return (o>0?"-":"+") + H + M;
  135. },
  136. Z: function (d) {
  137. var tz = d.toString().replace(/^.*:\d\d( GMT[+-]\d+)? \(?([A-Za-z ]+)\)?\d*$/, "$2").replace(/[a-z ]/g, "");
  138. if(tz.length > 4) {
  139. tz = Dt.formats.z(d);
  140. }
  141. return tz;
  142. },
  143. "%": function (d) { return "%"; }
  144. },
  145. aggregates: {
  146. c: "locale",
  147. D: "%m/%d/%y",
  148. F: "%Y-%m-%d",
  149. h: "%b",
  150. n: "\n",
  151. r: "%I:%M:%S %p",
  152. R: "%H:%M",
  153. t: "\t",
  154. T: "%H:%M:%S",
  155. x: "locale",
  156. X: "locale"
  157. //"+": "%a %b %e %T %Z %Y"
  158. },
  159. /**
  160. * Takes a native JavaScript Date and formats it as a string for display to user.
  161. *
  162. * @for Date
  163. * @method format
  164. * @param oDate {Date} Date.
  165. * @param oConfig {Object} (Optional) Object literal of configuration values:
  166. * <dl>
  167. * <dt>format {HTML} (Optional)</dt>
  168. * <dd>
  169. * <p>
  170. * Any strftime string is supported, such as "%I:%M:%S %p". strftime has several format specifiers defined by the Open group at
  171. * <a href="http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html">http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html</a>
  172. * PHP added a few of its own, defined at <a href="http://www.php.net/strftime">http://www.php.net/strftime</a>
  173. * </p>
  174. * <p>
  175. * This javascript implementation supports all the PHP specifiers and a few more. The full list is below.
  176. * </p>
  177. * <p>
  178. * If not specified, it defaults to the ISO 8601 standard date format: %Y-%m-%d.
  179. * </p>
  180. * <dl>
  181. * <dt>%a</dt> <dd>abbreviated weekday name according to the current locale</dd>
  182. * <dt>%A</dt> <dd>full weekday name according to the current locale</dd>
  183. * <dt>%b</dt> <dd>abbreviated month name according to the current locale</dd>
  184. * <dt>%B</dt> <dd>full month name according to the current locale</dd>
  185. * <dt>%c</dt> <dd>preferred date and time representation for the current locale</dd>
  186. * <dt>%C</dt> <dd>century number (the year divided by 100 and truncated to an integer, range 00 to 99)</dd>
  187. * <dt>%d</dt> <dd>day of the month as a decimal number (range 01 to 31)</dd>
  188. * <dt>%D</dt> <dd>same as %m/%d/%y</dd>
  189. * <dt>%e</dt> <dd>day of the month as a decimal number, a single digit is preceded by a space (range " 1" to "31")</dd>
  190. * <dt>%F</dt> <dd>same as %Y-%m-%d (ISO 8601 date format)</dd>
  191. * <dt>%g</dt> <dd>like %G, but without the century</dd>
  192. * <dt>%G</dt> <dd>The 4-digit year corresponding to the ISO week number</dd>
  193. * <dt>%h</dt> <dd>same as %b</dd>
  194. * <dt>%H</dt> <dd>hour as a decimal number using a 24-hour clock (range 00 to 23)</dd>
  195. * <dt>%I</dt> <dd>hour as a decimal number using a 12-hour clock (range 01 to 12)</dd>
  196. * <dt>%j</dt> <dd>day of the year as a decimal number (range 001 to 366)</dd>
  197. * <dt>%k</dt> <dd>hour as a decimal number using a 24-hour clock (range 0 to 23); single digits are preceded by a blank. (See also %H.)</dd>
  198. * <dt>%l</dt> <dd>hour as a decimal number using a 12-hour clock (range 1 to 12); single digits are preceded by a blank. (See also %I.) </dd>
  199. * <dt>%m</dt> <dd>month as a decimal number (range 01 to 12)</dd>
  200. * <dt>%M</dt> <dd>minute as a decimal number</dd>
  201. * <dt>%n</dt> <dd>newline character</dd>
  202. * <dt>%p</dt> <dd>either "AM" or "PM" according to the given time value, or the corresponding strings for the current locale</dd>
  203. * <dt>%P</dt> <dd>like %p, but lower case</dd>
  204. * <dt>%r</dt> <dd>time in a.m. and p.m. notation equal to %I:%M:%S %p</dd>
  205. * <dt>%R</dt> <dd>time in 24 hour notation equal to %H:%M</dd>
  206. * <dt>%s</dt> <dd>number of seconds since the Epoch, ie, since 1970-01-01 00:00:00 UTC</dd>
  207. * <dt>%S</dt> <dd>second as a decimal number</dd>
  208. * <dt>%t</dt> <dd>tab character</dd>
  209. * <dt>%T</dt> <dd>current time, equal to %H:%M:%S</dd>
  210. * <dt>%u</dt> <dd>weekday as a decimal number [1,7], with 1 representing Monday</dd>
  211. * <dt>%U</dt> <dd>week number of the current year as a decimal number, starting with the
  212. * first Sunday as the first day of the first week</dd>
  213. * <dt>%V</dt> <dd>The ISO 8601:1988 week number of the current year as a decimal number,
  214. * range 01 to 53, where week 1 is the first week that has at least 4 days
  215. * in the current year, and with Monday as the first day of the week.</dd>
  216. * <dt>%w</dt> <dd>day of the week as a decimal, Sunday being 0</dd>
  217. * <dt>%W</dt> <dd>week number of the current year as a decimal number, starting with the
  218. * first Monday as the first day of the first week</dd>
  219. * <dt>%x</dt> <dd>preferred date representation for the current locale without the time</dd>
  220. * <dt>%X</dt> <dd>preferred time representation for the current locale without the date</dd>
  221. * <dt>%y</dt> <dd>year as a decimal number without a century (range 00 to 99)</dd>
  222. * <dt>%Y</dt> <dd>year as a decimal number including the century</dd>
  223. * <dt>%z</dt> <dd>numerical time zone representation</dd>
  224. * <dt>%Z</dt> <dd>time zone name or abbreviation</dd>
  225. * <dt>%%</dt> <dd>a literal "%" character</dd>
  226. * </dl>
  227. * </dd>
  228. * </dl>
  229. * @return {HTML} Formatted date for display.
  230. */
  231. format : function (oDate, oConfig) {
  232. oConfig = oConfig || {};
  233. if(!Y.Lang.isDate(oDate)) {
  234. return Y.Lang.isValue(oDate) ? oDate : "";
  235. }
  236. var format, resources, compatMode, sLocale, LOCALE;
  237. format = oConfig.format || "%Y-%m-%d";
  238. resources = Y.Intl.get('datatype-date-format');
  239. var replace_aggs = function (m0, m1) {
  240. if (compatMode && m1 === "r") {
  241. return resources[m1];
  242. }
  243. var f = Dt.aggregates[m1];
  244. return (f === "locale" ? resources[m1] : f);
  245. };
  246. var replace_formats = function (m0, m1) {
  247. var f = Dt.formats[m1];
  248. switch(Y.Lang.type(f)) {
  249. case "string": // string => built in date function
  250. return oDate[f]();
  251. case "function": // function => our own function
  252. return f.call(oDate, oDate, resources);
  253. case "array": // built in function with padding
  254. if(Y.Lang.type(f[0]) === "string") {
  255. return xPad(oDate[f[0]](), f[1]);
  256. } // no break; (fall through to default:)
  257. default:
  258. return m1;
  259. }
  260. };
  261. // First replace aggregates (run in a loop because an agg may be made up of other aggs)
  262. while(format.match(/%[cDFhnrRtTxX]/)) {
  263. format = format.replace(/%([cDFhnrRtTxX])/g, replace_aggs);
  264. }
  265. // Now replace formats (do not run in a loop otherwise %%a will be replace with the value of %a)
  266. var str = format.replace(/%([aAbBCdegGHIjklmMpPsSuUVwWyYzZ%])/g, replace_formats);
  267. replace_aggs = replace_formats = undefined;
  268. return str;
  269. }
  270. };
  271. Y.mix(Y.namespace("Date"), Dt);
  272. Y.namespace("DataType");
  273. Y.DataType.Date = Y.Date;
  274. }, '@VERSION@', {
  275. "lang": [
  276. "ar",
  277. "ar-JO",
  278. "ca",
  279. "ca-ES",
  280. "da",
  281. "da-DK",
  282. "de",
  283. "de-AT",
  284. "de-DE",
  285. "el",
  286. "el-GR",
  287. "en",
  288. "en-AU",
  289. "en-CA",
  290. "en-GB",
  291. "en-IE",
  292. "en-IN",
  293. "en-JO",
  294. "en-MY",
  295. "en-NZ",
  296. "en-PH",
  297. "en-SG",
  298. "en-US",
  299. "es",
  300. "es-AR",
  301. "es-BO",
  302. "es-CL",
  303. "es-CO",
  304. "es-EC",
  305. "es-ES",
  306. "es-MX",
  307. "es-PE",
  308. "es-PY",
  309. "es-US",
  310. "es-UY",
  311. "es-VE",
  312. "fi",
  313. "fi-FI",
  314. "fr",
  315. "fr-BE",
  316. "fr-CA",
  317. "fr-FR",
  318. "hi",
  319. "hi-IN",
  320. "hu",
  321. "id",
  322. "id-ID",
  323. "it",
  324. "it-IT",
  325. "ja",
  326. "ja-JP",
  327. "ko",
  328. "ko-KR",
  329. "ms",
  330. "ms-MY",
  331. "nb",
  332. "nb-NO",
  333. "nl",
  334. "nl-BE",
  335. "nl-NL",
  336. "pl",
  337. "pl-PL",
  338. "pt",
  339. "pt-BR",
  340. "ro",
  341. "ro-RO",
  342. "ru",
  343. "ru-RU",
  344. "sv",
  345. "sv-SE",
  346. "th",
  347. "th-TH",
  348. "tr",
  349. "tr-TR",
  350. "vi",
  351. "vi-VN",
  352. "zh-Hans",
  353. "zh-Hans-CN",
  354. "zh-Hant",
  355. "zh-Hant-HK",
  356. "zh-Hant-TW"
  357. ]
  358. });