@@ -21,6 +21,7 @@ defmodule Cldr.DateTime do
21
21
22
22
alias Cldr.DateTime.Format
23
23
alias Cldr.LanguageTag
24
+ alias Cldr.Locale
24
25
25
26
@ format_types [ :short , :medium , :long , :full ]
26
27
@ default_format_type :medium
@@ -101,9 +102,12 @@ defmodule Cldr.DateTime do
101
102
102
103
## Options
103
104
104
- * `:format` is one of `:short`, `:medium`, `:long`, `:full` or a format string or
105
- any of the keys in the map returned by `Cldr.DateTime.Format.date_time_formats/3`.
106
- The default is `:medium`. See [here](README.md#date-time-and-datetime-localization-formats)
105
+ * `:format` is one of `:short`, `:medium`, `:long`, `:full`, or a format ID
106
+ or a format string. The default is `:medium` for full datetimes (that is,
107
+ dates having `:year`, `:month`, `:day`, `:hour`, `:minutes`, `:second` and
108
+ `:calendar` fields). The default for partial datetimes is to derive a candidate
109
+ format ID from the date and find the best match from the formats returned by
110
+ `Cldr.DateTime.available_formats/3`. See [here](README.md#date-time-and-datetime-localization-formats)
107
111
for more information about specifying formats.
108
112
109
113
* `:date_format` is any one of `:short`, `:medium`, `:long`, `:full`. If defined,
@@ -249,9 +253,12 @@ defmodule Cldr.DateTime do
249
253
250
254
## Options
251
255
252
- * `:format` is one of `:short`, `:medium`, `:long`, `:full` or a format string or
253
- any of the keys returned by `Cldr.DateTime.Format.date_time_formats/3`.
254
- The default is `:medium`. See [here](README.md#date-time-and-datetime-localization-formats)
256
+ * `:format` is one of `:short`, `:medium`, `:long`, `:full`, or a format ID
257
+ or a format string. The default is `:medium` for full datetimes (that is,
258
+ dates having `:year`, `:month`, `:day`, `:hour`, `:minutes`, `:second` and
259
+ `:calendar` fields). The default for partial datetimes is to derive a candidate
260
+ format ID from the date and find the best match from the formats returned by
261
+ `Cldr.DateTime.available_formats/3`. See [here](README.md#date-time-and-datetime-localization-formats)
255
262
for more information about specifying formats.
256
263
257
264
* `:style` is either `:at` or `:default`. When set to `:at` the datetime may
@@ -321,6 +328,146 @@ defmodule Cldr.DateTime do
321
328
end
322
329
end
323
330
331
+ @ doc """
332
+ Returns a map of the standard datetime formats for a given locale and calendar.
333
+
334
+ ## Arguments
335
+
336
+ * `locale` is any locale returned by `Cldr.known_locale_names/0`
337
+ or a `t:Cldr.LanguageTag.t/0`. The default is `Cldr.get_locale/0`.
338
+
339
+ * `calendar` is any calendar returned by `Cldr.DateTime.Format.calendars_for/1`
340
+ The default is `:gregorian`.
341
+
342
+ * `backend` is any module that includes `use Cldr` and therefore
343
+ is a `Cldr` backend module. The default is `Cldr.default_backend/0`.
344
+
345
+ ## Examples:
346
+
347
+ iex> Cldr.DateTime.Format.date_time_formats(:en)
348
+ {:ok, %Cldr.DateTime.Formats{
349
+ full: "{1}, {0}",
350
+ long: "{1}, {0}",
351
+ medium: "{1}, {0}",
352
+ short: "{1}, {0}"
353
+ }}
354
+
355
+ iex> Cldr.DateTime.Format.date_time_formats(:en, :buddhist, MyApp.Cldr)
356
+ {:ok, %Cldr.DateTime.Formats{
357
+ full: "{1}, {0}",
358
+ long: "{1}, {0}",
359
+ medium: "{1}, {0}",
360
+ short: "{1}, {0}"
361
+ }}
362
+
363
+ """
364
+ @ spec formats (
365
+ Locale . locale_reference ( ) ,
366
+ Cldr.Calendar . calendar ( ) ,
367
+ Cldr . backend ( )
368
+ ) ::
369
+ { :ok , map ( ) } | { :error , { atom , String . t ( ) } }
370
+
371
+ def formats (
372
+ locale \\ Cldr . get_locale ( ) ,
373
+ calendar \\ Cldr.Calendar . default_cldr_calendar ( ) ,
374
+ backend \\ Cldr.Date . default_backend ( )
375
+ ) do
376
+ Cldr.DateTime.Format . date_time_formats ( locale , calendar , backend )
377
+ end
378
+
379
+ @ doc """
380
+ Returns a map of the available datetime formats for a
381
+ given locale and calendar.
382
+
383
+ ## Arguments
384
+
385
+ * `locale` is any locale returned by `Cldr.known_locale_names/0`
386
+ or a `t:Cldr.LanguageTag.t/0`. The default is `Cldr.get_locale/0`.
387
+
388
+ * `calendar` is any calendar returned by `Cldr.DateTime.Format.calendars_for/1`
389
+ The default is `:gregorian`.
390
+
391
+ * `backend` is any module that includes `use Cldr` and therefore
392
+ is a `Cldr` backend module. The default is `Cldr.default_backend/0`.
393
+
394
+ ## Examples:
395
+
396
+ iex> Cldr.DateTime.available_formats(:en)
397
+ {:ok,
398
+ %{
399
+ yw: %{
400
+ other: "'week' w 'of' Y",
401
+ one: "'week' w 'of' Y",
402
+ pluralize: :week_of_year
403
+ },
404
+ GyMMMEd: "E, MMM d, y G",
405
+ Hms: "HH:mm:ss",
406
+ MMMMW: %{
407
+ other: "'week' W 'of' MMMM",
408
+ one: "'week' W 'of' MMMM",
409
+ pluralize: :week_of_month
410
+ },
411
+ E: "ccc",
412
+ MMMd: "MMM d",
413
+ yMEd: "E, M/d/y",
414
+ yQQQ: "QQQ y",
415
+ Ehm: %{unicode: "E h:mm a", ascii: "E h:mm a"},
416
+ M: "L",
417
+ hm: %{unicode: "h:mm a", ascii: "h:mm a"},
418
+ yM: "M/y",
419
+ GyMMMd: "MMM d, y G",
420
+ GyMd: "M/d/y G",
421
+ Gy: "y G",
422
+ Hm: "HH:mm",
423
+ EBhms: "E h:mm:ss B",
424
+ d: "d",
425
+ hms: %{unicode: "h:mm:ss a", ascii: "h:mm:ss a"},
426
+ Ed: "d E",
427
+ Ehms: %{unicode: "E h:mm:ss a", ascii: "E h:mm:ss a"},
428
+ EHms: "E HH:mm:ss",
429
+ Bh: "h B",
430
+ h: %{unicode: "h a", ascii: "h a"},
431
+ Bhms: "h:mm:ss B",
432
+ Hmv: "HH:mm v",
433
+ hmv: %{unicode: "h:mm a v", ascii: "h:mm a v"},
434
+ yMd: "M/d/y",
435
+ ms: "mm:ss",
436
+ MMM: "LLL",
437
+ y: "y",
438
+ Bhm: "h:mm B",
439
+ yMMM: "MMM y",
440
+ yQQQQ: "QQQQ y",
441
+ yMMMEd: "E, MMM d, y",
442
+ yMMMM: "MMMM y",
443
+ EBhm: "E h:mm B",
444
+ Hmsv: "HH:mm:ss v",
445
+ yMMMd: "MMM d, y",
446
+ MEd: "E, M/d",
447
+ EHm: "E HH:mm",
448
+ GyMMM: "MMM y G",
449
+ hmsv: %{unicode: "h:mm:ss a v", ascii: "h:mm:ss a v"},
450
+ H: "HH",
451
+ Md: "M/d",
452
+ MMMEd: "E, MMM d",
453
+ MMMMd: "MMMM d"
454
+ }}
455
+
456
+ """
457
+ @ spec available_formats (
458
+ Locale . locale_reference ( ) ,
459
+ Cldr.Calendar . calendar ( ) ,
460
+ Cldr . backend ( )
461
+ ) :: { :ok , map ( ) } | { :error , { atom , String . t ( ) } }
462
+
463
+ def available_formats (
464
+ locale \\ Cldr . get_locale ( ) ,
465
+ calendar \\ Cldr.Calendar . default_cldr_calendar ( ) ,
466
+ backend \\ Cldr.Date . default_backend ( )
467
+ ) do
468
+ Format . date_time_available_formats ( locale , calendar , backend )
469
+ end
470
+
324
471
defp normalize_options ( datetime , backend , [ ] ) do
325
472
{ locale , _backend } = Cldr . locale_and_backend_from ( nil , backend )
326
473
number_system = Cldr.Number.System . number_system_from_locale ( locale , backend )
0 commit comments