@@ -110,30 +110,12 @@ defmodule NaiveDateTime do
110110 @ spec utc_now ( Calendar . calendar ( ) | :native | :microsecond | :millisecond | :second ) :: t
111111 def utc_now ( calendar_or_time_unit \\ Calendar.ISO )
112112
113- def utc_now ( Calendar.ISO ) do
114- { :ok , { year , month , day } , { hour , minute , second } , microsecond } =
115- Calendar.ISO . from_unix ( :os . system_time ( ) , :native )
116-
117- % NaiveDateTime {
118- year: year ,
119- month: month ,
120- day: day ,
121- hour: hour ,
122- minute: minute ,
123- second: second ,
124- microsecond: microsecond ,
125- calendar: Calendar.ISO
126- }
127- end
128-
129113 def utc_now ( time_unit ) when time_unit in [ :microsecond , :millisecond , :second , :native ] do
130114 utc_now ( time_unit , Calendar.ISO )
131115 end
132116
133117 def utc_now ( calendar ) do
134- calendar
135- |> DateTime . utc_now ( )
136- |> DateTime . to_naive ( )
118+ utc_now ( :native , calendar )
137119 end
138120
139121 @ doc """
@@ -158,7 +140,20 @@ defmodule NaiveDateTime do
158140 @ spec utc_now ( :native | :microsecond | :millisecond | :second , Calendar . calendar ( ) ) :: t
159141 def utc_now ( time_unit , calendar )
160142 when time_unit in [ :native , :microsecond , :millisecond , :second ] do
161- DateTime . utc_now ( time_unit , calendar ) |> DateTime . to_naive ( )
143+ { :ok , { year , month , day } , { hour , minute , second } , microsecond } =
144+ Calendar.ISO . from_unix ( System . os_time ( time_unit ) , time_unit )
145+
146+ % NaiveDateTime {
147+ year: year ,
148+ month: month ,
149+ day: day ,
150+ hour: hour ,
151+ minute: minute ,
152+ second: second ,
153+ microsecond: microsecond ,
154+ calendar: Calendar.ISO
155+ }
156+ |> convert! ( calendar )
162157 end
163158
164159 @ doc """
@@ -1265,6 +1260,10 @@ defmodule NaiveDateTime do
12651260 { :ok , t } | { :error , :incompatible_calendars }
12661261
12671262 # Keep it multiline for proper function clause errors.
1263+ def convert ( % NaiveDateTime { calendar: calendar } = ndt , calendar ) do
1264+ { :ok , ndt }
1265+ end
1266+
12681267 def convert (
12691268 % {
12701269 calendar: calendar ,
0 commit comments