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