date-and-time
Version:
The simplest, most intuitive date and time library
181 lines (173 loc) • 6.21 kB
TypeScript
/** @preserve Copyright (c) KNOWLEDGECODE - MIT License */
type CompiledObject = string[];
interface DateLike {
/**
* Returns the year of the date.
*/
getFullYear(): number;
/**
* Returns the month of the date (0-11).
*/
getMonth(): number;
/**
* Returns the day of the month (1-31).
*/
getDate(): number;
/**
* Returns the hours of the date (0-23).
*/
getHours(): number;
/**
* Returns the minutes of the date (0-59).
*/
getMinutes(): number;
/**
* Returns the seconds of the date (0-59).
*/
getSeconds(): number;
/**
* Returns the milliseconds of the date (0-999).
*/
getMilliseconds(): number;
/**
* Returns the day of the week (0-6, where 0 is Sunday).
*/
getDay(): number;
/**
* Returns the time value in milliseconds since the Unix epoch (January 1, 1970).
*/
getTime(): number;
/**
* Returns the timezone offset in minutes from UTC.
*/
getTimezoneOffset(): number;
}
interface LocaleOptions {
compiledObj: CompiledObject;
style: 'long' | 'short' | 'narrow';
case?: 'uppercase' | 'lowercase';
}
interface Locale {
getLocale: () => string;
getMonthList: (options: LocaleOptions) => string[];
getDayOfWeekList: (options: LocaleOptions) => string[];
getMeridiemList: (options: LocaleOptions) => string[];
}
interface Numeral {
encode: (str: string) => string;
decode: (str: string) => string;
}
interface TimeZone {
zone_name: string;
gmt_offset: number[];
}
interface FormatterPluginOptions {
/**
* The hour format to use for formatting.
* This is used when the hour is in 12-hour format.
* It can be 'h11' for 11-hour format or 'h12' for 12-hour format.
*/
hour12: 'h11' | 'h12';
/**
* The hour format to use for formatting.
* This is used when the hour is in 24-hour format.
* It can be 'h23' for 23-hour format or 'h24' for 24-hour format.
*/
hour24: 'h23' | 'h24';
/**
* The numeral system to use for formatting numbers.
* This is an object that provides methods to encode and decode numbers in the specified numeral system.
*/
numeral: Numeral;
/**
* The calendar system to use for formatting dates.
* This can be 'buddhist' for Buddhist calendar or 'gregory' for Gregorian calendar.
*/
calendar: 'buddhist' | 'gregory';
/**
* The time zone to use for formatting dates and times.
* This can be a specific time zone object or 'UTC' to use Coordinated Universal Time.
* If not specified, it defaults to undefined, which means the local time zone will be used.
*/
timeZone: TimeZone | 'UTC' | undefined;
/**
* The locale to use for formatting dates and times.
* This is an object that provides methods to get localized month names, day names, and meridiems.
*/
locale: Locale;
}
declare abstract class FormatterPlugin {
[key: string]: (d: DateLike, options: FormatterPluginOptions, compiledObj: CompiledObject) => string;
}
type ParserToken = 'Y' | 'M' | 'D' | 'H' | 'A' | 'h' | 'm' | 's' | 'S' | 'Z';
interface ParserPluginOptions {
/**
* The hour format to use for parsing.
* This is used when the hour is in 12-hour format.
* It can be 'h11' for 11-hour format or 'h12' for 12-hour format.
*/
hour12: 'h11' | 'h12';
/**
* The hour format to use for parsing.
* This is used when the hour is in 24-hour format.
* It can be 'h23' for 23-hour format or 'h24' for 24-hour format.
*/
hour24: 'h23' | 'h24';
/**
* The numeral system to use for parsing numbers.
* This is an object that provides methods to encode and decode numbers in the specified numeral system.
*/
numeral: Numeral;
/**
* The calendar system to use for parsing dates.
* This can be 'buddhist' for Buddhist calendar or 'gregory' for Gregorian calendar.
*/
calendar: 'buddhist' | 'gregory';
/**
* Whether to ignore case when matching strings.
* This is useful for matching month names, day names, and meridiems in a case-insensitive manner.
* If true, the parser will convert both the input string and the strings in the locale to lowercase before matching.
*/
ignoreCase: boolean;
/**
* The time zone to use for parsing dates and times.
* This can be a specific time zone object or 'UTC' to use Coordinated Universal Time.
* If not specified, it defaults to undefined, which means the local time zone will be used.
*/
timeZone: TimeZone | 'UTC' | undefined;
/**
* The locale to use for parsing dates and times.
* This is an object that provides methods to get localized month names, day names, and meridiems.
*/
locale: Locale;
}
interface ParseResult {
value: number;
length: number;
token?: ParserToken;
}
declare abstract class ParserPlugin {
[key: string]: (str: string, options: ParserPluginOptions, compiledObj: CompiledObject) => ParseResult;
}
/**
* Executes a regular expression against a string and returns parsed result.
* @param re - The regular expression to execute
* @param str - The string to execute the regex against
* @param [token] - Optional parser token to associate with the result
* @returns ParseResult containing the numeric value, length, and token
*/
declare const exec: (re: RegExp, str: string, token?: ParserToken) => {
value: number;
length: number;
token: ParserToken | undefined;
};
/**
* Finds the best matching string from an array based on length and string position.
* @param array - Array of strings to search through
* @param str - The string to match against
* @param [token] - Optional parser token to associate with the result
* @returns ParseResult with the index of the longest matching string at the start position
*/
declare const find: (array: string[], str: string, token?: ParserToken) => ParseResult;
export { FormatterPlugin, ParserPlugin, exec, find };
export type { CompiledObject, DateLike, FormatterPluginOptions, Locale, Numeral, ParseResult, ParserPluginOptions, TimeZone };