Class DateParser

java.lang.Object
com.oracle.truffle.js.parser.date.DateParser

public class DateParser extends Object
JavaScript date parser. This class first tries to parse a date string according to the extended ISO 8601 format specified in ES5 15.9.1.15. If that fails, it falls back to legacy mode in which it accepts a range of different formats.

This class is neither thread-safe nor reusable. Calling the parse() method more than once will yield undefined results.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Constant for index position of parsed day value.
    static final int
    Constant for index position of parsed hour value.
    static final int
    Constant for index position of parsed millisecond value.
    static final int
    Constant for index position of parsed minute value.
    static final int
    Constant for index position of parsed month value.
    static final int
    Constant for index position of parsed second value.
    static final int
    Constant for index position of parsed time zone offset value.
    static final int
    Constant for index position of parsed year value.
  • Constructor Summary

    Constructors
    Constructor
    Description
    DateParser(JSRealm realm, String string, boolean extraLenient)
    Construct a new DateParser instance for parsing the given string.
  • Method Summary

    Modifier and Type
    Method
    Description
    Get the parsed date and time fields as an array of Integers.
    boolean
    Try parsing the given string as date according to the extended ISO 8601 format specified in ES5 15.9.1.15.
    boolean
    Try parsing the date string according to the rules laid out in ES5 15.9.1.15.
    boolean
    Try parsing the date using a fuzzy algorithm that can handle a variety of formats.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • YEAR

      public static final int YEAR
      Constant for index position of parsed year value.
      See Also:
    • MONTH

      public static final int MONTH
      Constant for index position of parsed month value.
      See Also:
    • DAY

      public static final int DAY
      Constant for index position of parsed day value.
      See Also:
    • HOUR

      public static final int HOUR
      Constant for index position of parsed hour value.
      See Also:
    • MINUTE

      public static final int MINUTE
      Constant for index position of parsed minute value.
      See Also:
    • SECOND

      public static final int SECOND
      Constant for index position of parsed second value.
      See Also:
    • MILLISECOND

      public static final int MILLISECOND
      Constant for index position of parsed millisecond value.
      See Also:
    • TIMEZONE

      public static final int TIMEZONE
      Constant for index position of parsed time zone offset value.
      See Also:
  • Constructor Details

    • DateParser

      public DateParser(JSRealm realm, String string, boolean extraLenient)
      Construct a new DateParser instance for parsing the given string.
      Parameters:
      string - the string to be parsed
  • Method Details

    • parse

      public boolean parse()
      Try parsing the given string as date according to the extended ISO 8601 format specified in ES5 15.9.1.15. Fall back to legacy mode if that fails. This method returns true if the string could be parsed.
      Returns:
      true if the string could be parsed as date
    • parseEcmaDate

      public boolean parseEcmaDate()
      Try parsing the date string according to the rules laid out in ES5 15.9.1.15. The date string must conform to the following format:
        [('-'|'+')yy]yyyy[-MM[-dd]][Thh:mm[:ss[.sss]][Z|(+|-)hh:mm]] 

      If the string does not contain a time zone offset, the TIMEZONE field is set to 0 (GMT).

      Returns:
      true if string represents a valid ES5 date string.
    • parseLegacyDate

      public boolean parseLegacyDate()
      Try parsing the date using a fuzzy algorithm that can handle a variety of formats.

      Numbers separated by ':' are treated as time values, optionally followed by a millisecond value separated by '.'. Other number values are treated as date values. The exact sequence of day, month, and year values to apply is determined heuristically.

      English month names and selected time zone names as well as AM/PM markers are recognized and handled properly. Additionally, numeric time zone offsets such as (+|-)hh:mm or (+|-)hhmm are recognized. If the string does not contain a time zone offset the TIMEZONEfield is left undefined, meaning the local time zone should be applied.

      English weekday names are recognized but ignored. All text in parentheses is ignored as well. All other text causes parsing to fail.

      Returns:
      true if the string could be parsed
    • getDateFields

      public Integer[] getDateFields()
      Get the parsed date and time fields as an array of Integers.

      If parsing was successful, all fields are guaranteed to be set except for the TIMEZONE field which may be null, meaning that local time zone offset should be applied.

      Returns:
      the parsed date fields