@@ -1052,22 +1052,44 @@ defmodule NaiveDateTime do
1052
1052
"""
1053
1053
@ doc since: "1.11.0"
1054
1054
@ 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 )
1060
1058
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
+
1061
1091
{ 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 )
1071
1093
1072
1094
% NaiveDateTime {
1073
1095
calendar: calendar ,
0 commit comments