PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/files/yui/3.16.0/datatype-date-format/datatype-date-format.js

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