206 lines
9.0 KiB
JavaScript
206 lines
9.0 KiB
JavaScript
|
import DateTime from "./datetime.js";
|
|||
|
import Settings from "./settings.js";
|
|||
|
import Locale from "./impl/locale.js";
|
|||
|
import IANAZone from "./zones/IANAZone.js";
|
|||
|
import { normalizeZone } from "./impl/zoneUtil.js";
|
|||
|
|
|||
|
import { hasLocaleWeekInfo, hasRelative } from "./impl/util.js";
|
|||
|
|
|||
|
/**
|
|||
|
* 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.
|
|||
|
*/
|
|||
|
export default class Info {
|
|||
|
/**
|
|||
|
* Return whether the specified zone contains a DST.
|
|||
|
* @param {string|Zone} [zone='local'] - Zone to check. Defaults to the environment's local zone.
|
|||
|
* @return {boolean}
|
|||
|
*/
|
|||
|
static hasDST(zone = Settings.defaultZone) {
|
|||
|
const proto = DateTime.now().setZone(zone).set({ month: 12 });
|
|||
|
|
|||
|
return !zone.isUniversal && proto.offset !== proto.set({ month: 6 }).offset;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* Return whether the specified zone is a valid IANA specifier.
|
|||
|
* @param {string} zone - Zone to check
|
|||
|
* @return {boolean}
|
|||
|
*/
|
|||
|
static isValidIANAZone(zone) {
|
|||
|
return IANAZone.isValidZone(zone);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* Converts the input into a {@link Zone} instance.
|
|||
|
*
|
|||
|
* * If `input` is already a Zone instance, it is returned unchanged.
|
|||
|
* * If `input` is a string containing a valid time zone name, a Zone instance
|
|||
|
* with that name is returned.
|
|||
|
* * If `input` is a string that doesn't refer to a known time zone, a Zone
|
|||
|
* instance with {@link Zone#isValid} == false is returned.
|
|||
|
* * If `input is a number, a Zone instance with the specified fixed offset
|
|||
|
* in minutes is returned.
|
|||
|
* * If `input` is `null` or `undefined`, the default zone is returned.
|
|||
|
* @param {string|Zone|number} [input] - the value to be converted
|
|||
|
* @return {Zone}
|
|||
|
*/
|
|||
|
static normalizeZone(input) {
|
|||
|
return normalizeZone(input, Settings.defaultZone);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* Get the weekday on which the week starts according to the given locale.
|
|||
|
* @param {Object} opts - options
|
|||
|
* @param {string} [opts.locale] - the locale code
|
|||
|
* @param {string} [opts.locObj=null] - an existing locale object to use
|
|||
|
* @returns {number} the start of the week, 1 for Monday through 7 for Sunday
|
|||
|
*/
|
|||
|
static getStartOfWeek({ locale = null, locObj = null } = {}) {
|
|||
|
return (locObj || Locale.create(locale)).getStartOfWeek();
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* Get the minimum number of days necessary in a week before it is considered part of the next year according
|
|||
|
* to the given locale.
|
|||
|
* @param {Object} opts - options
|
|||
|
* @param {string} [opts.locale] - the locale code
|
|||
|
* @param {string} [opts.locObj=null] - an existing locale object to use
|
|||
|
* @returns {number}
|
|||
|
*/
|
|||
|
static getMinimumDaysInFirstWeek({ locale = null, locObj = null } = {}) {
|
|||
|
return (locObj || Locale.create(locale)).getMinDaysInFirstWeek();
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* Get the weekdays, which are considered the weekend according to the given locale
|
|||
|
* @param {Object} opts - options
|
|||
|
* @param {string} [opts.locale] - the locale code
|
|||
|
* @param {string} [opts.locObj=null] - an existing locale object to use
|
|||
|
* @returns {number[]} an array of weekdays, 1 for Monday through 7 for Sunday
|
|||
|
*/
|
|||
|
static getWeekendWeekdays({ locale = null, locObj = null } = {}) {
|
|||
|
// copy the array, because we cache it internally
|
|||
|
return (locObj || Locale.create(locale)).getWeekendDays().slice();
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* Return an array of standalone month names.
|
|||
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat
|
|||
|
* @param {string} [length='long'] - the length of the month representation, such as "numeric", "2-digit", "narrow", "short", "long"
|
|||
|
* @param {Object} opts - options
|
|||
|
* @param {string} [opts.locale] - the locale code
|
|||
|
* @param {string} [opts.numberingSystem=null] - the numbering system
|
|||
|
* @param {string} [opts.locObj=null] - an existing locale object to use
|
|||
|
* @param {string} [opts.outputCalendar='gregory'] - the calendar
|
|||
|
* @example Info.months()[0] //=> 'January'
|
|||
|
* @example Info.months('short')[0] //=> 'Jan'
|
|||
|
* @example Info.months('numeric')[0] //=> '1'
|
|||
|
* @example Info.months('short', { locale: 'fr-CA' } )[0] //=> 'janv.'
|
|||
|
* @example Info.months('numeric', { locale: 'ar' })[0] //=> '١'
|
|||
|
* @example Info.months('long', { outputCalendar: 'islamic' })[0] //=> 'Rabiʻ I'
|
|||
|
* @return {Array}
|
|||
|
*/
|
|||
|
static months(
|
|||
|
length = "long",
|
|||
|
{ locale = null, numberingSystem = null, locObj = null, outputCalendar = "gregory" } = {}
|
|||
|
) {
|
|||
|
return (locObj || Locale.create(locale, numberingSystem, outputCalendar)).months(length);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* Return an array of format month names.
|
|||
|
* Format months differ from standalone months in that they're meant to appear next to the day of the month. In some languages, that
|
|||
|
* changes the string.
|
|||
|
* See {@link Info#months}
|
|||
|
* @param {string} [length='long'] - the length of the month representation, such as "numeric", "2-digit", "narrow", "short", "long"
|
|||
|
* @param {Object} opts - options
|
|||
|
* @param {string} [opts.locale] - the locale code
|
|||
|
* @param {string} [opts.numberingSystem=null] - the numbering system
|
|||
|
* @param {string} [opts.locObj=null] - an existing locale object to use
|
|||
|
* @param {string} [opts.outputCalendar='gregory'] - the calendar
|
|||
|
* @return {Array}
|
|||
|
*/
|
|||
|
static monthsFormat(
|
|||
|
length = "long",
|
|||
|
{ locale = null, numberingSystem = null, locObj = null, outputCalendar = "gregory" } = {}
|
|||
|
) {
|
|||
|
return (locObj || Locale.create(locale, numberingSystem, outputCalendar)).months(length, true);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* Return an array of standalone week names.
|
|||
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat
|
|||
|
* @param {string} [length='long'] - the length of the weekday representation, such as "narrow", "short", "long".
|
|||
|
* @param {Object} opts - options
|
|||
|
* @param {string} [opts.locale] - the locale code
|
|||
|
* @param {string} [opts.numberingSystem=null] - the numbering system
|
|||
|
* @param {string} [opts.locObj=null] - an existing locale object to use
|
|||
|
* @example Info.weekdays()[0] //=> 'Monday'
|
|||
|
* @example Info.weekdays('short')[0] //=> 'Mon'
|
|||
|
* @example Info.weekdays('short', { locale: 'fr-CA' })[0] //=> 'lun.'
|
|||
|
* @example Info.weekdays('short', { locale: 'ar' })[0] //=> 'الاثنين'
|
|||
|
* @return {Array}
|
|||
|
*/
|
|||
|
static weekdays(length = "long", { locale = null, numberingSystem = null, locObj = null } = {}) {
|
|||
|
return (locObj || Locale.create(locale, numberingSystem, null)).weekdays(length);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* Return an array of format week names.
|
|||
|
* Format weekdays differ from standalone weekdays in that they're meant to appear next to more date information. In some languages, that
|
|||
|
* changes the string.
|
|||
|
* See {@link Info#weekdays}
|
|||
|
* @param {string} [length='long'] - the length of the month representation, such as "narrow", "short", "long".
|
|||
|
* @param {Object} opts - options
|
|||
|
* @param {string} [opts.locale=null] - the locale code
|
|||
|
* @param {string} [opts.numberingSystem=null] - the numbering system
|
|||
|
* @param {string} [opts.locObj=null] - an existing locale object to use
|
|||
|
* @return {Array}
|
|||
|
*/
|
|||
|
static weekdaysFormat(
|
|||
|
length = "long",
|
|||
|
{ locale = null, numberingSystem = null, locObj = null } = {}
|
|||
|
) {
|
|||
|
return (locObj || Locale.create(locale, numberingSystem, null)).weekdays(length, true);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* Return an array of meridiems.
|
|||
|
* @param {Object} opts - options
|
|||
|
* @param {string} [opts.locale] - the locale code
|
|||
|
* @example Info.meridiems() //=> [ 'AM', 'PM' ]
|
|||
|
* @example Info.meridiems({ locale: 'my' }) //=> [ 'နံနက်', 'ညနေ' ]
|
|||
|
* @return {Array}
|
|||
|
*/
|
|||
|
static meridiems({ locale = null } = {}) {
|
|||
|
return Locale.create(locale).meridiems();
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* Return an array of eras, such as ['BC', 'AD']. The locale can be specified, but the calendar system is always Gregorian.
|
|||
|
* @param {string} [length='short'] - the length of the era representation, such as "short" or "long".
|
|||
|
* @param {Object} opts - options
|
|||
|
* @param {string} [opts.locale] - the locale code
|
|||
|
* @example Info.eras() //=> [ 'BC', 'AD' ]
|
|||
|
* @example Info.eras('long') //=> [ 'Before Christ', 'Anno Domini' ]
|
|||
|
* @example Info.eras('long', { locale: 'fr' }) //=> [ 'avant Jésus-Christ', 'après Jésus-Christ' ]
|
|||
|
* @return {Array}
|
|||
|
*/
|
|||
|
static eras(length = "short", { locale = null } = {}) {
|
|||
|
return Locale.create(locale, null, "gregory").eras(length);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* Return the set of available features in this environment.
|
|||
|
* 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.
|
|||
|
* Keys:
|
|||
|
* * `relative`: whether this environment supports relative time formatting
|
|||
|
* * `localeWeek`: whether this environment supports different weekdays for the start of the week based on the locale
|
|||
|
* @example Info.features() //=> { relative: false, localeWeek: true }
|
|||
|
* @return {Object}
|
|||
|
*/
|
|||
|
static features() {
|
|||
|
return { relative: hasRelative(), localeWeek: hasLocaleWeekInfo() };
|
|||
|
}
|
|||
|
}
|