Skip to content

Commit 1438536

Browse files
committed
Fix days_in_month/2 and month/2 for Julian calendars
1 parent bc1bdcc commit 1438536

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

lib/cldr/calendar/calendars/julian_compiler.ex

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,7 @@ defmodule Cldr.Calendar.Julian.Compiler do
111111
end
112112

113113
def days_in_year(year) do
114-
first_day = date_to_iso_days(year, @new_year_starting_month, @new_year_starting_day)
115-
first_day_next_year = date_to_iso_days(year + 1, @new_year_starting_month, @new_year_starting_day)
116-
first_day_next_year - first_day
114+
last_iso_day_of_year(year) - first_iso_day_of_year(year) + 1
117115
end
118116

119117
# Here we use month to mean ordinal month. Therefore if the calendar
@@ -128,23 +126,20 @@ defmodule Cldr.Calendar.Julian.Compiler do
128126
adjusted_month =
129127
Cldr.Math.amod(ordinal_month + @new_year_starting_month - 1, @months_in_year)
130128

131-
adjusted_year =
132-
if adjusted_month < @new_year_starting_month, do: year + 1, else: year
133-
134129
cond do
135130
# The first month of the year will be short since the year starts
136131
# part way through the month
137132
adjusted_month == @new_year_starting_month ->
138133
days_in_julian_month = Cldr.Calendar.Julian.days_in_month(year, ordinal_month)
139-
days_in_julian_month - @new_year_starting_day
134+
days_in_julian_month - @new_year_starting_day + 1
140135

141136
# The last month of the year will be "long" since the first part of the
142137
# first month that is before the start of year will be included
143138
adjusted_month == @last_month_of_year ->
144139
start_of_month =
145-
date_to_iso_days(adjusted_year, adjusted_month, 1)
140+
date_to_iso_days(year, adjusted_month, 1)
146141
start_of_next_month =
147-
date_to_iso_days(adjusted_year, @new_year_starting_month, @new_year_starting_day)
142+
date_to_iso_days(year + 1, @new_year_starting_month, @new_year_starting_day)
148143
start_of_next_month - start_of_month
149144

150145
true ->
@@ -153,11 +148,10 @@ defmodule Cldr.Calendar.Julian.Compiler do
153148
end
154149

155150
def year(year) do
156-
first_day = date_to_iso_days(year, @new_year_starting_month, @new_year_starting_day)
157-
{:ok, first_date} = Date.new(year, @new_year_starting_month, @new_year_starting_day, __MODULE__)
151+
{year, month, day} = first_day_of_year(year)
152+
{:ok, first_date} = Date.new(year, month, day, __MODULE__)
158153

159-
last_day = date_to_iso_days(year, @new_year_starting_month, @new_year_starting_day) - 1
160-
{year, month, day} = date_from_iso_days(last_day)
154+
{year, month, day} = last_day_of_year(year)
161155
{:ok, last_date} = Date.new(year, month, day, __MODULE__)
162156

163157
Date.range(first_date, last_date, 1)
@@ -171,13 +165,10 @@ defmodule Cldr.Calendar.Julian.Compiler do
171165
adjusted_month =
172166
Cldr.Math.amod(ordinal_month + @new_year_starting_month - 1, @months_in_year)
173167

174-
adjusted_year =
175-
if adjusted_month < @new_year_starting_month, do: year + 1, else: year
176-
177168
first_day =
178169
if adjusted_month == @new_year_starting_month, do: @new_year_starting_day, else: 1
179170

180-
{:ok, first} = Date.new(adjusted_year, adjusted_month, first_day, __MODULE__)
171+
{:ok, first} = Date.new(year, adjusted_month, first_day, __MODULE__)
181172
first_iso_days = date_to_iso_days(year, adjusted_month, first_day)
182173
days_in_month = days_in_month(year, ordinal_month)
183174

@@ -205,42 +196,55 @@ defmodule Cldr.Calendar.Julian.Compiler do
205196
end
206197

207198
def day_of_year(year, month, day) do
208-
adjusted_year =
209-
if month < @new_year_starting_month, do: year + 1, else: year
210-
211-
first_day = date_to_iso_days(year, @new_year_starting_month, @new_year_starting_day)
212-
this_day = date_to_iso_days(adjusted_year, month, day)
199+
first_day = first_iso_day_of_year(year)
200+
this_day = date_to_iso_days(year, month, day)
213201
this_day - first_day + 1
214202
end
215203

216-
def calendar_year(year, month, day) when year_rollover(month, day) do
217-
year - 1
218-
end
219-
220204
def calendar_year(year, _month, _day) do
221205
year
222206
end
223207

208+
def extended_year(year, month, day) when year_rollover(month, day) do
209+
calendar_year(year, month, day)
210+
end
211+
212+
def cyclic_year(year, month, day) do
213+
calendar_year(year, month, day)
214+
end
215+
224216
def related_gregorian_year(year, month, day) do
225217
iso_days = date_to_iso_days(year, month, day)
226218
{year, _month, _day} = Cldr.Calendar.Gregorian.date_from_iso_days(iso_days)
227219
year
228220
end
229221

230-
def extended_year(year, month, day) when year_rollover(month, day) do
231-
year - 1
222+
def first_day_of_year(year) do
223+
month = @new_year_starting_month
224+
day = @new_year_starting_day
225+
{year, month, day}
232226
end
233227

234-
def extended_year(year, month, day) when year_rollover(month, day) do
235-
year
228+
def last_day_of_year(year) do
229+
last_day = first_iso_day_of_year(year + 1) - 1
230+
date_from_iso_days(last_day)
236231
end
237232

238-
def cyclic_year(year, month, day) do
239-
calendar_year(year, month, day)
233+
def first_iso_day_of_year(year) do
234+
{year, month, day} = first_day_of_year(year)
235+
date_to_iso_days(year, month, day)
236+
end
237+
238+
def last_iso_day_of_year(year) do
239+
{year, month, day} = last_day_of_year(year)
240+
date_to_iso_days(year, month, day)
241+
end
242+
243+
def leap_year?(year) do
244+
last_iso_day_of_year(year) - first_iso_day_of_year(year) + 1 == 366
240245
end
241246

242247
defdelegate valid_date?(year, month, day), to: Cldr.Calendar.Julian
243-
defdelegate leap_year?(year), to: Cldr.Calendar.Julian
244248
defdelegate week(year, week), to: Cldr.Calendar.Julian
245249
defdelegate weeks_in_year(year), to: Cldr.Calendar.Julian
246250
defdelegate months_in_year(year), to: Cldr.Calendar.Julian

0 commit comments

Comments
 (0)