info.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import DateTime from "./datetime.js";
  2. import Settings from "./settings.js";
  3. import Locale from "./impl/locale.js";
  4. import IANAZone from "./zones/IANAZone.js";
  5. import { normalizeZone } from "./impl/zoneUtil.js";
  6. import { hasLocaleWeekInfo, hasRelative } from "./impl/util.js";
  7. /**
  8. * The Info class contains static methods for retrieving general time and date related data. For example, it has methods for finding out if a time zone has a DST, for listing the months in any supported locale, and for discovering which of Luxon features are available in the current environment.
  9. */
  10. export default class Info {
  11. /**
  12. * Return whether the specified zone contains a DST.
  13. * @param {string|Zone} [zone='local'] - Zone to check. Defaults to the environment's local zone.
  14. * @return {boolean}
  15. */
  16. static hasDST(zone = Settings.defaultZone) {
  17. const proto = DateTime.now().setZone(zone).set({ month: 12 });
  18. return !zone.isUniversal && proto.offset !== proto.set({ month: 6 }).offset;
  19. }
  20. /**
  21. * Return whether the specified zone is a valid IANA specifier.
  22. * @param {string} zone - Zone to check
  23. * @return {boolean}
  24. */
  25. static isValidIANAZone(zone) {
  26. return IANAZone.isValidZone(zone);
  27. }
  28. /**
  29. * Converts the input into a {@link Zone} instance.
  30. *
  31. * * If `input` is already a Zone instance, it is returned unchanged.
  32. * * If `input` is a string containing a valid time zone name, a Zone instance
  33. * with that name is returned.
  34. * * If `input` is a string that doesn't refer to a known time zone, a Zone
  35. * instance with {@link Zone#isValid} == false is returned.
  36. * * If `input is a number, a Zone instance with the specified fixed offset
  37. * in minutes is returned.
  38. * * If `input` is `null` or `undefined`, the default zone is returned.
  39. * @param {string|Zone|number} [input] - the value to be converted
  40. * @return {Zone}
  41. */
  42. static normalizeZone(input) {
  43. return normalizeZone(input, Settings.defaultZone);
  44. }
  45. /**
  46. * Get the weekday on which the week starts according to the given locale.
  47. * @param {Object} opts - options
  48. * @param {string} [opts.locale] - the locale code
  49. * @param {string} [opts.locObj=null] - an existing locale object to use
  50. * @returns {number} the start of the week, 1 for Monday through 7 for Sunday
  51. */
  52. static getStartOfWeek({ locale = null, locObj = null } = {}) {
  53. return (locObj || Locale.create(locale)).getStartOfWeek();
  54. }
  55. /**
  56. * Get the minimum number of days necessary in a week before it is considered part of the next year according
  57. * to the given locale.
  58. * @param {Object} opts - options
  59. * @param {string} [opts.locale] - the locale code
  60. * @param {string} [opts.locObj=null] - an existing locale object to use
  61. * @returns {number}
  62. */
  63. static getMinimumDaysInFirstWeek({ locale = null, locObj = null } = {}) {
  64. return (locObj || Locale.create(locale)).getMinDaysInFirstWeek();
  65. }
  66. /**
  67. * Get the weekdays, which are considered the weekend according to the given locale
  68. * @param {Object} opts - options
  69. * @param {string} [opts.locale] - the locale code
  70. * @param {string} [opts.locObj=null] - an existing locale object to use
  71. * @returns {number[]} an array of weekdays, 1 for Monday through 7 for Sunday
  72. */
  73. static getWeekendWeekdays({ locale = null, locObj = null } = {}) {
  74. // copy the array, because we cache it internally
  75. return (locObj || Locale.create(locale)).getWeekendDays().slice();
  76. }
  77. /**
  78. * Return an array of standalone month names.
  79. * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat
  80. * @param {string} [length='long'] - the length of the month representation, such as "numeric", "2-digit", "narrow", "short", "long"
  81. * @param {Object} opts - options
  82. * @param {string} [opts.locale] - the locale code
  83. * @param {string} [opts.numberingSystem=null] - the numbering system
  84. * @param {string} [opts.locObj=null] - an existing locale object to use
  85. * @param {string} [opts.outputCalendar='gregory'] - the calendar
  86. * @example Info.months()[0] //=> 'January'
  87. * @example Info.months('short')[0] //=> 'Jan'
  88. * @example Info.months('numeric')[0] //=> '1'
  89. * @example Info.months('short', { locale: 'fr-CA' } )[0] //=> 'janv.'
  90. * @example Info.months('numeric', { locale: 'ar' })[0] //=> '١'
  91. * @example Info.months('long', { outputCalendar: 'islamic' })[0] //=> 'Rabiʻ I'
  92. * @return {Array}
  93. */
  94. static months(
  95. length = "long",
  96. { locale = null, numberingSystem = null, locObj = null, outputCalendar = "gregory" } = {}
  97. ) {
  98. return (locObj || Locale.create(locale, numberingSystem, outputCalendar)).months(length);
  99. }
  100. /**
  101. * Return an array of format month names.
  102. * Format months differ from standalone months in that they're meant to appear next to the day of the month. In some languages, that
  103. * changes the string.
  104. * See {@link Info#months}
  105. * @param {string} [length='long'] - the length of the month representation, such as "numeric", "2-digit", "narrow", "short", "long"
  106. * @param {Object} opts - options
  107. * @param {string} [opts.locale] - the locale code
  108. * @param {string} [opts.numberingSystem=null] - the numbering system
  109. * @param {string} [opts.locObj=null] - an existing locale object to use
  110. * @param {string} [opts.outputCalendar='gregory'] - the calendar
  111. * @return {Array}
  112. */
  113. static monthsFormat(
  114. length = "long",
  115. { locale = null, numberingSystem = null, locObj = null, outputCalendar = "gregory" } = {}
  116. ) {
  117. return (locObj || Locale.create(locale, numberingSystem, outputCalendar)).months(length, true);
  118. }
  119. /**
  120. * Return an array of standalone week names.
  121. * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat
  122. * @param {string} [length='long'] - the length of the weekday representation, such as "narrow", "short", "long".
  123. * @param {Object} opts - options
  124. * @param {string} [opts.locale] - the locale code
  125. * @param {string} [opts.numberingSystem=null] - the numbering system
  126. * @param {string} [opts.locObj=null] - an existing locale object to use
  127. * @example Info.weekdays()[0] //=> 'Monday'
  128. * @example Info.weekdays('short')[0] //=> 'Mon'
  129. * @example Info.weekdays('short', { locale: 'fr-CA' })[0] //=> 'lun.'
  130. * @example Info.weekdays('short', { locale: 'ar' })[0] //=> 'الاثنين'
  131. * @return {Array}
  132. */
  133. static weekdays(length = "long", { locale = null, numberingSystem = null, locObj = null } = {}) {
  134. return (locObj || Locale.create(locale, numberingSystem, null)).weekdays(length);
  135. }
  136. /**
  137. * Return an array of format week names.
  138. * Format weekdays differ from standalone weekdays in that they're meant to appear next to more date information. In some languages, that
  139. * changes the string.
  140. * See {@link Info#weekdays}
  141. * @param {string} [length='long'] - the length of the month representation, such as "narrow", "short", "long".
  142. * @param {Object} opts - options
  143. * @param {string} [opts.locale=null] - the locale code
  144. * @param {string} [opts.numberingSystem=null] - the numbering system
  145. * @param {string} [opts.locObj=null] - an existing locale object to use
  146. * @return {Array}
  147. */
  148. static weekdaysFormat(
  149. length = "long",
  150. { locale = null, numberingSystem = null, locObj = null } = {}
  151. ) {
  152. return (locObj || Locale.create(locale, numberingSystem, null)).weekdays(length, true);
  153. }
  154. /**
  155. * Return an array of meridiems.
  156. * @param {Object} opts - options
  157. * @param {string} [opts.locale] - the locale code
  158. * @example Info.meridiems() //=> [ 'AM', 'PM' ]
  159. * @example Info.meridiems({ locale: 'my' }) //=> [ 'နံနက်', 'ညနေ' ]
  160. * @return {Array}
  161. */
  162. static meridiems({ locale = null } = {}) {
  163. return Locale.create(locale).meridiems();
  164. }
  165. /**
  166. * Return an array of eras, such as ['BC', 'AD']. The locale can be specified, but the calendar system is always Gregorian.
  167. * @param {string} [length='short'] - the length of the era representation, such as "short" or "long".
  168. * @param {Object} opts - options
  169. * @param {string} [opts.locale] - the locale code
  170. * @example Info.eras() //=> [ 'BC', 'AD' ]
  171. * @example Info.eras('long') //=> [ 'Before Christ', 'Anno Domini' ]
  172. * @example Info.eras('long', { locale: 'fr' }) //=> [ 'avant Jésus-Christ', 'après Jésus-Christ' ]
  173. * @return {Array}
  174. */
  175. static eras(length = "short", { locale = null } = {}) {
  176. return Locale.create(locale, null, "gregory").eras(length);
  177. }
  178. /**
  179. * Return the set of available features in this environment.
  180. * Some features of Luxon are not available in all environments. For example, on older browsers, relative time formatting support is not available. Use this function to figure out if that's the case.
  181. * Keys:
  182. * * `relative`: whether this environment supports relative time formatting
  183. * * `localeWeek`: whether this environment supports different weekdays for the start of the week based on the locale
  184. * @example Info.features() //=> { relative: false, localeWeek: true }
  185. * @return {Object}
  186. */
  187. static features() {
  188. return { relative: hasRelative(), localeWeek: hasLocaleWeekInfo() };
  189. }
  190. }