Skip to content

Commit 11a493e

Browse files
committed
Further optimize from_gregorian_seconds by inlining more code
1 parent 496cb2c commit 11a493e

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

lib/elixir/lib/calendar/iso.ex

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,15 +1772,6 @@ defmodule Calendar.ISO do
17721772
{days, day_fraction}
17731773
end
17741774

1775-
@doc false
1776-
def gregorian_seconds_to_naive_datetime(seconds, microsecond) do
1777-
{days, seconds} = div_rem(seconds, @seconds_per_day)
1778-
{hours, seconds} = div_rem(seconds, @seconds_per_hour)
1779-
{minutes, seconds} = div_rem(seconds, @seconds_per_minute)
1780-
{year, month, day} = date_from_iso_days(days)
1781-
{year, month, day, hours, minutes, seconds, {microsecond, 6}}
1782-
end
1783-
17841775
@doc false
17851776
def iso_days_to_unit({days, {parts, ppd}}, unit) do
17861777
day_microseconds = days * @parts_per_day

lib/elixir/lib/calendar/naive_datetime.ex

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,22 +1052,44 @@ defmodule NaiveDateTime do
10521052
"""
10531053
@doc since: "1.11.0"
10541054
@spec from_gregorian_seconds(integer(), Calendar.microsecond(), Calendar.calendar()) :: t
1055-
def from_gregorian_seconds(
1056-
seconds,
1057-
{microsecond, precision} \\ {0, 0},
1058-
calendar \\ Calendar.ISO
1059-
)
1055+
def from_gregorian_seconds(seconds, microsecond_precision \\ {0, 0}, calendar \\ Calendar.ISO)
1056+
1057+
def from_gregorian_seconds(seconds, {microsecond, precision}, Calendar.ISO)
10601058
when is_integer(seconds) do
1059+
{days, seconds} = div_rem(seconds, 24 * 60 * 60)
1060+
{hours, seconds} = div_rem(seconds, 60 * 60)
1061+
{minutes, seconds} = div_rem(seconds, 60)
1062+
{year, month, day} = Calendar.ISO.date_from_iso_days(days)
1063+
1064+
%NaiveDateTime{
1065+
calendar: Calendar.ISO,
1066+
year: year,
1067+
month: month,
1068+
day: day,
1069+
hour: hours,
1070+
minute: minutes,
1071+
second: seconds,
1072+
microsecond: {microsecond, precision}
1073+
}
1074+
end
1075+
1076+
defp div_rem(int1, int2) do
1077+
div = div(int1, int2)
1078+
rem = int1 - div * int2
1079+
1080+
if rem >= 0 do
1081+
{div, rem}
1082+
else
1083+
{div - 1, rem + int2}
1084+
end
1085+
end
1086+
1087+
def from_gregorian_seconds(seconds, {microsecond, precision}, calendar)
1088+
when is_integer(seconds) do
1089+
iso_days = Calendar.ISO.gregorian_seconds_to_iso_days(seconds, microsecond)
1090+
10611091
{year, month, day, hour, minute, second, {microsecond, _}} =
1062-
case calendar do
1063-
Calendar.ISO ->
1064-
# Optimized version that skips unnecessary iso days conversion
1065-
Calendar.ISO.gregorian_seconds_to_naive_datetime(seconds, microsecond)
1066-
1067-
_ ->
1068-
iso_days = Calendar.ISO.gregorian_seconds_to_iso_days(seconds, microsecond)
1069-
calendar.naive_datetime_from_iso_days(iso_days)
1070-
end
1092+
calendar.naive_datetime_from_iso_days(iso_days)
10711093

10721094
%NaiveDateTime{
10731095
calendar: calendar,

0 commit comments

Comments
 (0)