/** * @license * Copyright The Closure Library Authors. * SPDX-License-Identifier: Apache-2.0 */ /** * @fileoverview Date/Time parsing library with locale support. */ /** * Namespace for locale date/time parsing functions */ /** * DateTimeParse is for parsing date in a locale-sensitive manner. It allows * user to use any customized patterns to parse date-time string under certain * locale. Things varies across locales like month name, weekname, field * order, etc. * * This module is the counter-part of DateTimeFormat. They use the same * date/time pattern specification, which is borrowed from ICU/JDK. * * This implementation could parse partial date/time. * * Time Format Syntax: To specify the time format use a time pattern string. * In this pattern, following letters are reserved as pattern letters, which * are defined as the following: * *
* Symbol Meaning Presentation Example * ------ ------- ------------ ------- * G era designator (Text) AD * y# year (Number) 1996 * M month in year (Text & Number) July & 07 * d day in month (Number) 10 * h hour in am/pm (1~12) (Number) 12 * H hour in day (0~23) (Number) 0 * m minute in hour (Number) 30 * s second in minute (Number) 55 * S fractional second (Number) 978 * E day of week (Text) Tuesday * D day in year (Number) 189 * a am/pm marker (Text) PM * b am/pm/noon/midnight (Text) Noon * B flexible day periods (Text) de l’après-midi' * k hour in day (1~24) (Number) 24 * K hour in am/pm (0~11) (Number) 0 * z time zone (Text) Pacific Standard Time * Z time zone (RFC 822) (Number) -0800 * v time zone (generic) (Text) Pacific Time * ' escape for text (Delimiter) 'Date=' * '' single quote (Literal) 'o''clock' ** * The count of pattern letters determine the format.
* (Text): 4 or more pattern letters--use full form, * less than 4--use short or abbreviated form if one exists. * In parsing, we will always try long format, then short.
* (Number): the minimum number of digits.
* (Text & Number): 3 or over, use text, otherwise use number.
* Any characters that not in the pattern will be treated as quoted text. For * instance, characters like ':', '.', ' ', '#' and '@' will appear in the * resulting time text even they are not embraced within single quotes. In our * current pattern usage, we didn't use up all letters. But those unused * letters are strongly discouraged to be used as quoted text without quote. * That's because we may use other letter for pattern in future.
* * Examples Using the US Locale: * * Format Pattern Result * -------------- ------- * "yyyy.MM.dd G 'at' HH:mm:ss vvvv" ->> 1996.07.10 AD at 15:08:56 Pacific Time * "EEE, MMM d, ''yy" ->> Wed, July 10, '96 * "h:mm a" ->> 12:08 PM * "hh 'o''clock' a, zzzz" ->> 12 o'clock PM, Pacific Daylight Time * "K:mm a, vvv" ->> 0:00 PM, PT * "yyyyy.MMMMM.dd GGG hh:mm aaa" ->> 01996.July.10 AD 12:08 PM * *
When parsing a date string using the abbreviated year pattern ("yy"), * DateTimeParse must interpret the abbreviated year relative to some * century. It does this by adjusting dates to be within 80 years before and 20 * years after the time the parse function is called. For example, using a * pattern of "MM/dd/yy" and a DateTimeParse instance created on Jan 1, 1997, * the string "01/11/12" would be interpreted as Jan 11, 2012 while the string * "05/04/64" would be interpreted as May 4, 1964. During parsing, only * strings consisting of exactly two digits, as defined by {@link * java.lang.Character#isDigit(char)}, will be parsed into the default * century. Any other numeric string, such as a one digit string, a three or * more digit string will be interpreted as its face value. * *
If the year pattern does not have exactly two 'y' characters, the year is * interpreted literally, regardless of the number of digits. So using the * pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D. * *
When numeric fields abut one another directly, with no intervening * delimiter characters, they constitute a run of abutting numeric fields. Such * runs are parsed specially. For example, the format "HHmmss" parses the input * text "123456" to 12:34:56, parses the input text "12345" to 1:23:45, and * fails to parse "1234". In other words, the leftmost field of the run is * flexible, while the others keep a fixed width. If the parse fails anywhere in * the run, then the leftmost field is shortened by one character, and the * entire run is parsed again. This is repeated until either the parse succeeds * or the leftmost field is one character in length. If the parse still fails at * that point, the parse of the run fails. * *
Now timezone parsing only support GMT:hhmm, GMT:+hhmm, GMT:-hhmm
*/
/**
* Construct a DateTimeParse based on current locale.
* instance rather than the global symbols.
* @final
*/
export class DateTimeParse {
/**
* Construct a DateTimeParse based on current locale.
* @param {string|number} pattern pattern specification or pattern type.
* @param {!Object=} opt_dateTimeSymbols Optional symbols to use for this
* instance rather than the global symbols.
*/
constructor(pattern: string | number, opt_dateTimeSymbols?: any | undefined);
/** @const @private {!Array}; */
private patternParts_;
/**
* Data structure with all the locale info needed for date formatting.
* (day/month names, most common patterns, rules for week-end, etc.)
* @const @private {!DateTimeSymbolsType}
*/
private dateTimeSymbols_;
/**
* Apply a pattern to this Parser. The pattern string will be parsed and saved
* in "compiled" form.
* Note: this method is somewhat similar to the pattern parsing method in
* datetimeformat. If you see something wrong here, you might want
* to check the other.
* @param {string} pattern It describes the format of date string that need to
* be parsed.
* @private
*/
private applyPattern_;
/**
* Apply a predefined pattern to this Parser.
* @param {number} formatType A constant used to identified the predefined
* pattern string stored in locale repository.
* @private
*/
private applyStandardPattern_;
/**
* Calculate character repeat count in pattern.
*
* @param {string} pattern It describes the format of date string that need to
* be parsed.
* @param {number} start The position of pattern character.
*
* @return {number} Repeat count.
* @private
*/
private getNextCharCount_;
/**
* Identify the start of an abutting numeric fields' run. Taking pattern
* "HHmmss" as an example. It will try to parse 2/2/2 characters of the input
* text, then if that fails, 1/2/2. We only adjust the width of the leftmost
* field; the others remain fixed. This allows "123456" => 12:34:56, but
* "12345" => 1:23:45. Likewise, for the pattern "yyyyMMdd" we try 4/2/2,
* 3/2/2, 2/2/2, and finally 1/2/2. The first field of connected numeric
* fields will be marked as abutStart, its width can be reduced to accommodate
* others.
*
* @private
*/
private markAbutStart_;
/**
* Check if the pattern part is a numeric field.
*
* @param {string} ch pattern character
* @param {number} count pattern character count
*
* @return {boolean} true if the pattern part is numeric field.
* @private
*/
private isNumericField_;
/**
* Assert this object's pattern supports predictive parsing.
* @private
*/
private assertPatternSupportsPredictive_;
/**
* Parse the given string and fill parsed values into date object.
*
* The existing values of any temporal fields of `date` not parsed from `text`
* are not directly changed, but may be affected by overflow. E.g. if a minutes
* value of 70 is parsed, the implementation of `date` may increment the value
* of its hours field by 1 while setting its minutes value to 10.
*
* @param {string} text The string being parsed.
* @param {?DateLike} date The Date object to hold the parsed date.
* @param {!Object=} options The options object.
* @return {number} How many characters parser advanced.
*/
parse(text: string, date: DateLike | null, options?: any | undefined): number;
/**
* Parse a run of abutting numeric pattern parts. Take the pattern "HHmmss" as
* an example. We will try to parse 2/2/2 characters of the input text, then if
* that fails, 1/2/2. We only adjust the width of the leftmost field; the others
* remain fixed. This allows "123456" => 12:34:56, but "12345" => 1:23:45.
* Likewise, for the pattern "yyyyMMdd" we try 4/2/2, 3/2/2, 2/2/2, and finally
* 1/2/2.
*
* @param {string} text the text to be parsed.
* @param {!Array