@@ -559,4 +559,60 @@ defmodule Localize.DateTime do
559559 end
560560
561561 defp resolve_locale_id ( locale ) , do: Localize.Locale . cldr_locale_id_from ( locale )
562+
563+ @ doc """
564+ Parses a localized date and time string.
565+
566+ Parsing lives in the companion [calendrical](https://hex.pm/packages/calendrical)
567+ package, which carries the calendar systems Localize formats for.
568+ `calendrical` depends on Localize, so Localize resolves it at runtime rather
569+ than depending on it in return — add `{:calendrical, "~> 1.0"}` to your
570+ dependencies to use this function.
571+
572+ ### Arguments
573+
574+ * `string` is a string in any shape the locale accepts, including the
575+ locale's CLDR short, medium, long and full patterns and ISO 8601.
576+
577+ * `options` is a keyword list of options.
578+
579+ ### Options
580+
581+ * `:locale` is a locale identifier. The default is the locale returned by
582+ `Localize.get_locale/0`.
583+
584+ * Remaining options are passed to `Calendrical.DateTime.parse/2`, which
585+ documents them.
586+
587+ ### Returns
588+
589+ * `{:ok, value}` where `value` is a `t:NaiveDateTime.t()` , or
590+
591+ * `{:error, exception}` if the string does not parse, or a
592+ `t:Localize.DependencyRequiredError.t/0` if `calendrical` is not among
593+ the application's dependencies.
594+
595+ ### Examples
596+
597+ Shown rather than run as doctests: `calendrical` is not a dependency of
598+ Localize itself, so the call does not resolve in this package's own tests.
599+
600+ Localize.DateTime.parse("22.03.2026, 14:30", locale: :de)
601+ #=> {:ok, ~N[2026-03-22 14:30:00]}
602+
603+ Localize.DateTime.parse("March 22, 2026, 2:30 PM", locale: :en)
604+ #=> {:ok, ~N[2026-03-22 14:30:00]}
605+
606+ """
607+ @ spec parse ( String . t ( ) , Keyword . t ( ) ) ::
608+ { :ok , NaiveDateTime . t ( ) | DateTime . t ( ) } | { :error , Exception . t ( ) }
609+ def parse ( string , options \\ [ ] ) when is_binary ( string ) do
610+ Localize.OptionalDependency . call (
611+ "Calendrical.DateTime" ,
612+ :parse ,
613+ [ string , options ] ,
614+ package: "calendrical" ,
615+ operation: "Localize.DateTime.parse/2"
616+ )
617+ end
562618end
0 commit comments