calendarFunctions.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. define([ "./Record", "requirejs-text/text!../cldr/supplemental/calendarData.json"],
  2. function (Record, calendarDataJson) {
  3. /**
  4. * Utility functions that are common across multiple
  5. * different calendars. Functions that are unique to a specific calendar should be contained
  6. * in the code for the given calendar, and not here.
  7. *
  8. * @private
  9. */
  10. var calendarData = JSON.parse(calendarDataJson).supplemental.calendarData;
  11. var calendarFunctions = {
  12. /**
  13. * Used to determine when a given era begins, based on supplemental calendar data from CLDR.
  14. *
  15. * @param {String} calendar The type of calendar
  16. * @param {Number} era The era number
  17. * @returns {Number} The year in which the given era begins
  18. * @private
  19. */
  20. eraOffset : function (calendar, era) {
  21. var eraStartDate = calendarData[calendar].eras[era.toString()]._start;
  22. var result = eraStartDate.charAt(0) === "-" ? Number(eraStartDate.split("-")[1]) * -1 :
  23. Number(eraStartDate.split("-")[0]);
  24. if (result <= 0) {
  25. result--; // Compensate for the fact that year 0 (Gregorian) doesn't exist.
  26. }
  27. return result;
  28. },
  29. /**
  30. * Used to find the era for a given date. Starts at the most recent era (highest era number) and works
  31. * backwards. Right now, findEra is only used in the Japanese and ROC calendar implementations, so
  32. * we don't have to worry about eras starting in a year numbered less than 0.
  33. *
  34. * @param {String} calendar The type of calendar
  35. * @param {Date} date The date for which we are trying to determine the era
  36. * @param {Number} maxEra The maximum era number in the given calendar
  37. * @returns {Number} The number of the era in which the given date resides
  38. * @private
  39. */
  40. findEra : function (calendar, date, maxEra) {
  41. var currentEra = maxEra;
  42. while (currentEra >= 0) {
  43. var compareDate = new Date();
  44. var eraStartDate = calendarData[calendar].eras[currentEra.toString()]._start;
  45. if (!eraStartDate) {
  46. return currentEra;
  47. }
  48. var pieces = eraStartDate.split("-");
  49. compareDate.setFullYear(pieces[0], pieces[1] - 1, pieces[2] - 1);
  50. if (date >= compareDate) {
  51. return currentEra;
  52. }
  53. currentEra--;
  54. }
  55. return currentEra; // Return -1 if date is before the start of era #0
  56. },
  57. /**
  58. * Used to set the hour, minute, second, and inDST fields, which usually don't vary across calendars.
  59. *
  60. * @param {Date} dt The date used as the basis for setting fields
  61. * @param {String} timeZone String representing the time zone (UTC or local)
  62. * @param {Record} result The object representing the resulting date/time
  63. * @private
  64. */
  65. setTimeFields : function (dt, timeZone, result) {
  66. result.set("hour", timeZone === "UTC" ? dt.getUTCHours() : dt.getHours());
  67. result.set("minute", timeZone === "UTC" ? dt.getUTCMinutes() : dt.getMinutes());
  68. result.set("second", timeZone === "UTC" ? dt.getUTCSeconds() : dt.getSeconds());
  69. var localMinutes = dt.getHours() * 60 + dt.getMinutes();
  70. var UTCMinutes = dt.getUTCHours() * 60 + dt.getUTCMinutes();
  71. result.set("inDST", timeZone === "UTC" ? false : localMinutes + dt.getTimezoneOffset() !== UTCMinutes);
  72. }
  73. };
  74. return calendarFunctions;
  75. });