Skip to content

Commit 1b22e73

Browse files
committedJul 9, 2016
extract date data
1 parent df3be25 commit 1b22e73

File tree

3 files changed

+79
-7
lines changed

3 files changed

+79
-7
lines changed
 

‎lib/cldr/date.ex

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
defmodule CLDRex.Date do
2+
@moduledoc """
3+
4+
"""
5+
alias CLDRex.Main
6+
alias CLDRex.Formatters.CLDRFormatter
7+
import CLDRex.Utils
8+
9+
def localize(date, locale, options \\ %{}) do
10+
locale = normalize_locale(locale)
11+
fallback = fallback(locale)
12+
length = get_in(options, [:length]) || :full
13+
cal = get_in(options, [:calendar]) || :gregorian
14+
15+
f = get_in(Main.cldr_main_data,
16+
[locale, :calendar, cal, :date_formats, length])
17+
18+
if !f || Enum.empty?(f) do
19+
f = get_in(Main.cldr_main_data,
20+
[fallback, :calendar, cal, :date_formats, length])
21+
end
22+
23+
Timex.format(date, f, CLDRFormatter)
24+
end
25+
26+
def short(date, locale, calendar \\ :gregorian),
27+
do: localize(date, locale, %{length: :short, calendar: calendar})
28+
29+
def medium(date, locale, calendar \\ :gregorian),
30+
do: localize(date, locale, %{length: :medium, calendar: calendar})
31+
32+
def long(date, locale, calendar \\ :gregorian),
33+
do: localize(date, locale, %{length: :long, calendar: calendar})
34+
35+
def full(date, locale, calendar \\ :gregorian),
36+
do: localize(date, locale, %{length: :full, calendar: calendar})
37+
end

‎lib/cldrex/parsers/xml_parser.ex

+39-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ defmodule CLDRex.Parsers.XMLParser do
1212
def parse_main_data do
1313
@main_path
1414
|> File.ls!
15+
|> Enum.take(1)
1516
|> process_files
1617
end
1718

@@ -77,7 +78,7 @@ defmodule CLDRex.Parsers.XMLParser do
7778
xmap(doc, calendars: [
7879
~x"//dates/calendars/calendar"l,
7980
type: ~x"./@type",
80-
monthContexts: [
81+
month_contexts: [
8182
~x"./months/monthContext"l,
8283
name: ~x"./@type",
8384
formats: [
@@ -90,7 +91,7 @@ defmodule CLDRex.Parsers.XMLParser do
9091
]
9192
]
9293
],
93-
dayContexts: [
94+
day_contexts: [
9495
~x"./days/dayContext"l,
9596
name: ~x"./@type",
9697
formats: [
@@ -102,6 +103,21 @@ defmodule CLDRex.Parsers.XMLParser do
102103
label: ~x"./text()"
103104
]
104105
]
106+
],
107+
date_formats: [
108+
~x"./dateFormats/dateFormatLength"l,
109+
length: ~x"./@type",
110+
format: ~x"./dateFormat/pattern/text()"
111+
],
112+
time_formats: [
113+
~x"./timeFormats/timeFormatLength"l,
114+
length: ~x"./@type",
115+
format: ~x"./timeFormat/pattern/text()"
116+
],
117+
date_time_formats: [
118+
~x"./dateTimeFormats/dateTimeFormatLength"l,
119+
length: ~x"./@type",
120+
format: ~x"./dateTimeFormat/pattern/text()"
105121
]
106122
])
107123
end
@@ -110,7 +126,7 @@ defmodule CLDRex.Parsers.XMLParser do
110126
Enum.reduce xdoc, %{}, fn(data, _acc) ->
111127
{_, calendars} = data
112128
Enum.reduce(calendars, %{}, fn(cal, cacc) ->
113-
mc = Enum.reduce(cal.monthContexts, %{}, fn(mctxt, mcacc) ->
129+
mc = Enum.reduce(cal.month_contexts, %{}, fn(mctxt, mcacc) ->
114130
f = Enum.reduce(mctxt.formats, %{}, fn(fmt, facc) ->
115131
m = Enum.reduce(fmt.months, %{}, fn(month, macc) ->
116132
Map.put(macc, String.to_atom(to_string(month.month)), month.label)
@@ -120,7 +136,7 @@ defmodule CLDRex.Parsers.XMLParser do
120136
Map.put(mcacc, String.to_atom(to_string(mctxt.name)), f)
121137
end)
122138

123-
dc = Enum.reduce(cal.dayContexts, %{}, fn(dctxt, dcacc) ->
139+
dc = Enum.reduce(cal.day_contexts, %{}, fn(dctxt, dcacc) ->
124140
f = Enum.reduce(dctxt.formats, %{}, fn(fmt, facc) ->
125141
m = Enum.reduce(fmt.days, %{}, fn(day, macc) ->
126142
Map.put(macc, String.to_atom(to_string(day.day)), day.label)
@@ -130,7 +146,25 @@ defmodule CLDRex.Parsers.XMLParser do
130146
Map.put(dcacc, String.to_atom(to_string(dctxt.name)), f)
131147
end)
132148

133-
contexts = %{months: mc, days: dc}
149+
datec = Enum.reduce(cal.date_formats, %{}, fn(datectxt, dateacc) ->
150+
Map.put(dateacc, datectxt.length, datectxt.format)
151+
end)
152+
153+
timec = Enum.reduce(cal.time_formats, %{}, fn(timectxt, timeacc) ->
154+
Map.put(timeacc, timectxt.length, timectxt.format)
155+
end)
156+
157+
dtc = Enum.reduce(cal.date_time_formats, %{}, fn(dtctxt, dtacc) ->
158+
Map.put(dtacc, dtctxt.length, dtctxt.format)
159+
end)
160+
161+
contexts = %{
162+
months: mc,
163+
days: dc,
164+
date_formats: datec,
165+
time_formats: timec,
166+
date_time_formats: dtc
167+
}
134168

135169
Map.put(cacc, String.to_atom(to_string(cal.type)), contexts)
136170
end)

‎mix.exs

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ defmodule CLDRex.Mixfile do
2424
end
2525

2626
def application do
27-
[applications: [:logger]]
27+
[applications: [:logger, :timex]]
2828
end
2929

3030
defp deps do
31-
[{:sweet_xml, ">= 0.0.0"},
31+
[{:sweet_xml, "~> 0.6"},
32+
{:timex, "~> 2.2"},
3233
{:ex_doc, "~> 0.12", only: [:dev, :docs]}]
3334
end
3435

0 commit comments

Comments
 (0)
Please sign in to comment.