       {days + days_offset, {parts, ppd}}
     end
   end
+
+  # Note that this function does not add the extra leap day for a leap year.
+  # If you want to add that leap day when appropriate,
+  # add the result of leap_day_offset/2 to the result of days_before_month/1.
+  defp days_before_month(1), do: 0
+  defp days_before_month(2), do: 31
+  defp days_before_month(3), do: 59
+  defp days_before_month(4), do: 90
+  defp days_before_month(5), do: 120
+  defp days_before_month(6), do: 151
+  defp days_before_month(7), do: 181
+  defp days_before_month(8), do: 212
+  defp days_before_month(9), do: 243
+  defp days_before_month(10), do: 273
+  defp days_before_month(11), do: 304
+  defp days_before_month(12), do: 334
+
+  defp leap_day_offset(_year, month) when month < 3, do: 0
+  defp leap_day_offset(year, _month) do
+    if leap_year?(year), do: 1, else: 0
+  end
+
+  defp days_to_year(days) do
+    year = Integer.floor_div(days, @days_per_nonleap_year)
+    {year, days_before_year} = days_to_year(year, days, days_in_previous_years(year))
+    {year, days - days_before_year}
+  end
+
+  defp days_to_year(year, days1, days2) when days1 < days2 do
+    days_to_year(year - 1, days1, days_in_previous_years(year - 1))
+  end
+
+  defp days_to_year(year, _days1, days2) do
+    {year, days2}
+  end
+
+  defp days_in_previous_years(0), do: 0
+  defp days_in_previous_years(year) do
+    previous_year = year - 1
+    Integer.floor_div(previous_year, 4) -
+      Integer.floor_div(previous_year, 100) +
+      Integer.floor_div(previous_year, 400) +
+      previous_year * @days_per_nonleap_year + @days_per_leap_year
+  end
+
+  # Note that 0 is the first day of the month.
+  defp year_day_to_year_date(_extra_day, day_of_year) when day_of_year < 31 do
+    {1, day_of_year}
+  end
+  defp year_day_to_year_date(extra_day, day_of_year) when day_of_year < (59 + extra_day) do
+    {2, day_of_year - 31}
+  end
+  defp year_day_to_year_date(extra_day, day_of_year) when day_of_year < (90 + extra_day) do
+    {3, day_of_year - (59 + extra_day)}
+  end
+  defp year_day_to_year_date(extra_day, day_of_year) when day_of_year < (120 + extra_day) do
+    {4, day_of_year - (90 + extra_day)}
+  end
+  defp year_day_to_year_date(extra_day, day_of_year) when day_of_year < (151 + extra_day) do
+    {5, day_of_year - (120 + extra_day)}
+  end
+  defp year_day_to_year_date(extra_day, day_of_year) when day_of_year < (181 + extra_day) do
+    {6, day_of_year - (151 + extra_day)}
+  end
+  defp year_day_to_year_date(extra_day, day_of_year) when day_of_year < (212 + extra_day) do
+    {7, day_of_year - (181 + extra_day)}
+  end
+  defp year_day_to_year_date(extra_day, day_of_year) when day_of_year < (243 + extra_day) do
+    {8, day_of_year - (212 + extra_day)}
+  end
+  defp year_day_to_year_date(extra_day, day_of_year) when day_of_year < (273 + extra_day) do
+    {9, day_of_year - (243 + extra_day)}
+  end
+  defp year_day_to_year_date(extra_day, day_of_year) when day_of_year < (304 + extra_day) do
+    {10, day_of_year - (273 + extra_day)}
+  end
+  defp year_day_to_year_date(extra_day, day_of_year) when day_of_year < (334 + extra_day) do
+    {11, day_of_year - (304 + extra_day)}
+  end
+  defp year_day_to_year_date(extra_day, day_of_year) do
+    {12, day_of_year - (334 + extra_day)}
+  end
+
+  defp iso_seconds_to_datetime(seconds) do
+    {days, rest_seconds} = div_mod(seconds, @seconds_per_day)
+
+    date = date_from_iso_days(days)
+    time = seconds_to_time(rest_seconds)
+    {date, time}
+  end
+
+  defp seconds_to_time(seconds) when seconds in 0..(@seconds_per_day - 1) do
+    {hour, rest_seconds} = div_mod(seconds, @seconds_per_hour)
+    {minute, second} = div_mod(rest_seconds, @seconds_per_minute)
+
+    {hour, minute, second}
+  end
 end

  @seconds_per_hour 60 * 60
  @seconds_per_day 24 * 60 * 60 # Note that this does _not_ handle leap seconds.
  @microseconds_per_second 1_000_000

  @doc """
  Returns the `t:Calendar.iso_days` format of the specified date.

  def date_to_iso_days(1970, 1, 1) do
    719528
  end
  def date_to_iso_days(year, month, day) when year in 0..9999 do
    true = day <= days_in_month(year, month)
    days_in_previous_years(year) + days_before_month(month) + leap_day_offset(year, month) + day - 1
  end

  # Converts count of days since 0000-01-01 to {year, month, day} tuple.
  @doc false
  def date_from_iso_days(days) when days in 0..3652424 do
    {year, day_of_year} = days_to_year(days)
    extra_day = if leap_year?(year), do: 1, else: 0
    {month, day_in_month} = year_day_to_year_date(extra_day, day_of_year)
    {year, month, day_in_month + 1}
  end

  defp div_mod(int1, int2) do
