Skip to content

Commit 90e09bb

Browse files
committed
Fix day_of_week and add tests for it
1 parent 3500cbc commit 90e09bb

File tree

4 files changed

+67
-16
lines changed

4 files changed

+67
-16
lines changed

lib/jalaali.ex

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ defmodule Jalaali do
77
"""
88

99
@days_offset 1721060
10+
@breaks [-61, 9, 38, 199, 426, 686, 756, 818, 1111, 1181, 1210, 1635, 2060,
11+
2097, 2192, 2262, 2324, 2394, 2456, 3178]
1012

1113
@doc """
1214
Converts an erlang date from Gregorian to Jalaali date in erlang format
@@ -204,16 +206,13 @@ defmodule Jalaali do
204206
"""
205207
@spec jal_cal(Integer.t) :: Map.t
206208
def jal_cal(jy) do
207-
breaks = [-61, 9, 38, 199, 426, 686, 756, 818, 1111, 1181, 1210, 1635, 2060, 2097, 2192, 2262, 2324,
208-
2394, 2456,
209-
3178]
210209
gy = jy + 621
211210

212211
if jy < -61 or jy >= 3178 do
213212
raise "Invalid Jalaali year #{jy}"
214213
end
215214

216-
{jump, jp, leap_j} = calc_jlimit(breaks, jy, {Enum.at(breaks, 0), -14}, 1)
215+
{jump, jp, leap_j} = calc_jlimit(jy, {Enum.at(@breaks, 0), -14}, 1)
217216

218217
n = jy - jp
219218

@@ -246,15 +245,15 @@ defmodule Jalaali do
246245
%{leap: leap, gy: gy, march: march}
247246
end
248247

249-
@spec calc_jlimit(List.t, Integer.t, {Integer.t, Integer.t}, Integer.t) :: {Integer.t, Integer.t, Integer.t}
250-
defp calc_jlimit(breaks, jy, {jp, leap_j}, index) do
251-
jm = Enum.at(breaks, index)
248+
@spec calc_jlimit(Integer.t, {Integer.t, Integer.t}, Integer.t) :: {Integer.t, Integer.t, Integer.t}
249+
defp calc_jlimit(jy, {jp, leap_j}, index) do
250+
jm = Enum.at(@breaks, index)
252251
jump = jm - jp
253252
cond do
254253
jy < jm ->
255254
{jump, jp, leap_j}
256255
true ->
257-
calc_jlimit(breaks, jy, {jm, leap_j + div(jump, 33) * 8 + div(mod(jump, 33), 4)}, index + 1)
256+
calc_jlimit(jy, {jm, leap_j + div(jump, 33) * 8 + div(mod(jump, 33), 4)}, index + 1)
258257
end
259258
end
260259

@@ -279,21 +278,21 @@ defmodule Jalaali do
279278

280279
jm = 7 + div(k, 30)
281280
jd = mod(k, 30) + 1
282-
{jy, jm, jd} # HACK: remove duplication
281+
{jy, jm, jd}
283282
r.leap == 1 ->
284283
jy = jy - 1
285284
k = k + 180
286285

287286
jm = 7 + div(k, 30)
288287
jd = mod(k, 30) + 1
289-
{jy, jm, jd} # HACK: remove duplication
288+
{jy, jm, jd}
290289
true ->
291290
jy = jy - 1
292291
k = k + 179
293292

294293
jm = 7 + div(k, 30)
295294
jd = mod(k, 30) + 1
296-
{jy, jm, jd} # HACK: remove duplication
295+
{jy, jm, jd}
297296
end
298297
end
299298

lib/jalaali/calendar.ex

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ defmodule Jalaali.Calendar do
88
@type year :: 0..9999
99
@type month :: 1..12
1010
@type day :: 1..31
11+
@type day_of_week :: 1..7
12+
@type hour :: 0..23
13+
@type minute :: 0..59
14+
@type second :: 0..60
15+
@type microsecond :: Integer.t
1116

1217
@seconds_per_minute 60
1318
@seconds_per_hour 60 * 60
@@ -25,17 +30,32 @@ defmodule Jalaali.Calendar do
2530
Jalaali.is_leap_jalaali_year(year)
2631

2732
@impl true
28-
def day_of_week(year, month, day), do:
29-
Calendar.ISO.day_of_week(year, month, day)
33+
@spec day_of_week(year, month, day) :: day_of_week
34+
@doc """
35+
Returns day of week on a spesific set of year, month and day
36+
"""
37+
def day_of_week(year, month, day) do
38+
{:ok, date} = Date.new(year, month, day, __MODULE__)
39+
iso_date = Date.convert!(date, Calendar.ISO)
40+
Calendar.ISO.day_of_week(iso_date.year, iso_date.month, iso_date.day)
41+
end
3042

3143
@doc """
3244
Converts the given date into a string.
3345
"""
3446
@impl true
47+
@spec date_to_string(year, month, day) :: String.t
3548
def date_to_string(year, month, day) do
3649
zero_pad(year, 4) <> "-" <> zero_pad(month, 2) <> "-" <> zero_pad(day, 2)
3750
end
3851

52+
@doc """
53+
Converts a Date struct to string human readable format
54+
55+
- Extended type of string date. e.g.: "2017-01-05" `:extended`
56+
- Basic type of string date. e.g.: "20170105" `:basic`
57+
"""
58+
@spec date_to_string(year, month, day, :extended | :basic) :: String.t
3959
def date_to_string(year, month, day, :extended), do:
4060
date_to_string(year, month, day)
4161

@@ -44,15 +64,16 @@ defmodule Jalaali.Calendar do
4464
end
4565

4666
@doc """
47-
Converts the datetime (without time zone) into a string.
67+
Converts the datetime (without time zone) into a human readable string.
4868
"""
4969
@impl true
70+
@spec naive_datetime_to_string(year, month, day, hour, minute, second, microsecond) :: String.t
5071
def naive_datetime_to_string(year, month, day, hour, minute, second, microsecond) do
5172
date_to_string(year, month, day) <> " " <> time_to_string(hour, minute, second, microsecond)
5273
end
5374

5475
@doc """
55-
Convers the datetime (with time zone) into a string.
76+
Convers the datetime (with time zone) into a human readable string.
5677
"""
5778
@impl true
5879
def datetime_to_string(year, month, day, hour, minute, second, microsecond,

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Jalaali.Mixfile do
22
use Mix.Project
33

4-
@version "0.2.0"
4+
@version "0.2.1"
55

66
def project do
77
[app: :jalaali,

test/jalaali_test.exs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,35 @@ defmodule JalaaliTest do
5656

5757
assert gre_to_jal == now_jal
5858
end
59+
60+
test "Day number" do
61+
now = DateTime.utc_now()
62+
jal_now = DateTime.convert!(now, Jalaali.Calendar)
63+
64+
assert Date.day_of_week(now) == Date.day_of_week(jal_now)
65+
end
66+
67+
test "Day number in hundred days" do
68+
unix = DateTime.to_unix(DateTime.utc_now()) + 100 * 24 * 60 * 60
69+
70+
now = DateTime.from_unix!(unix)
71+
jal_date = DateTime.convert!(now, Jalaali.Calendar)
72+
73+
assert Date.day_of_week(now) == Date.day_of_week(jal_date)
74+
end
75+
76+
test "Day number in hundred days ago" do
77+
unix = DateTime.to_unix(DateTime.utc_now()) - 100 * 24 * 60 * 60
78+
79+
now = DateTime.from_unix!(unix)
80+
jal_date = DateTime.convert!(now, Jalaali.Calendar)
81+
82+
assert Date.day_of_week(now) == Date.day_of_week(jal_date)
83+
end
84+
85+
test "Day number on a specific date" do
86+
{:ok, now} = Date.new(1321, 7, 7, Jalaali.Calendar)
87+
88+
assert Date.day_of_week(now) == 2
89+
end
5990
end

0 commit comments

Comments
 (0)