// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

import { DateTimeFormatter } from "./_date_time_formatter.ts";

/**
 * Parses a date string using the specified format string.
 *
 * The following symbols from
 * {@link https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table | unicode LDML}
 * are supported:
 * - `yyyy` - numeric year
 * - `yy` - 2-digit year
 * - `M` - numeric month
 * - `MM` - 2-digit month
 * - `d` - numeric day
 * - `dd` - 2-digit day
 * - `H` - numeric hour (0-23 hours)
 * - `HH` - 2-digit hour (00-23 hours)
 * - `h` - numeric hour (1-12 hours)
 * - `hh` - 2-digit hour (01-12 hours)
 * - `m` - numeric minute
 * - `mm` - 2-digit minute
 * - `s` - numeric second
 * - `ss` - 2-digit second
 * - `S` - 1-digit fractional second
 * - `SS` - 2-digit fractional second
 * - `SSS` - 3-digit fractional second
 * - `a` - dayPeriod, either `AM` or `PM`
 * - `'foo'` - quoted literal
 * - `./-` - unquoted literal
 *
 * @param dateString The date string to parse.
 * @param formatString The date time string format.
 * @return The parsed date.
 *
 * @example Basic usage
 * ```ts
 * import { parse } from "@std/datetime/parse";
 * import { assertEquals } from "@std/assert";
 *
 * assertEquals(parse("01-03-2019 16:30", "MM-dd-yyyy HH:mm"), new Date(2019, 0, 3, 16, 30));
 *
 * assertEquals(parse("01-03-2019 16:33:23.123", "MM-dd-yyyy HH:mm:ss.SSS"), new Date(2019, 0, 3, 16, 33, 23, 123));
 * ```
 */
export function parse(dateString: string, formatString: string): Date {
  const formatter = new DateTimeFormatter(formatString);
  const parts = formatter.parseToParts(dateString);
  const sortParts = formatter.sortDateTimeFormatPart(parts);
  return formatter.partsToDate(sortParts);
}

// denoCacheMetadata={"headers":{"x-robots-tag":"noindex","alt-svc":"h3=\":443\"; ma=86400","content-length":"1795","cf-ray":"a223e6564f5d7db6-LAX","date":"Tue, 28 Jul 2026 12:31:14 GMT","cf-cache-status":"HIT","cache-control":"public, max-age=31536000, immutable","server":"cloudflare","age":"26958","content-security-policy":"default-src 'none'; script-src 'none'; style-src 'none'; img-src 'none'; font-src 'none'; connect-src 'none'; frame-src 'none'; object-src 'none'; frame-ancestors 'none'; sandbox; form-action 'none';","vary":"Origin","access-control-allow-origin":"*","access-control-expose-headers":"*","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","etag":"\"5ea131d9b8c25ecf2bc5920b68f89f44\"","accept-ranges":"bytes","cross-origin-resource-policy":"cross-origin","x-content-type-options":"nosniff","x-jsr-backend":"modules","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=WqNwgJpXCaxHpcIG%2B71A3E%2FC%2Fq3UEJ8zB6A14dpjsYYdMZcO1nHWxiMuDHehCsaIflxAozjgkQYioTHvqhGdTkmOOb%2FVOZExxus9cANUTDLSmUjYFzvUkwU%3D\"}]}","content-type":"text/typescript","set-cookie":"__cf_bm=rnwBBy4RkjeV4BL.lzJyDu2RcwKGoszJPQYkvAC2fgk-1785241874.9720683-1.0.1.1-.4_2hH9BS6pCRfYpULZtOp5P.Rq2CN7tGwb2H0VXQSDZqsimobOtzGp1M8f4uYOJTHUxnIy7C.2NY5d.i4hb9A46yCtTvT4Fr2nzg9XhtZjCDXsdvn9YxltnuLWuui_6; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:14 GMT"},"url":"https://jsr.io/@std/datetime/0.224.5/parse.ts","time":1785241874}