#include <Math.au3>


Func Date_Julian2JD(Const $DD, Const $MM, Const $JJ)
	Local $M = adjusted_month_from_month($MM)
	Local $J = adjusted_year_from_year($JJ, $MM)
	Local $y4800 = $J + 4800
	Local $a = division($y4800, 100)
	Local $b = Mod($y4800, 100)
	Return 36525 * $a + 1461 * division($b, 4) + 365 * Mod($b, 4) + division((7 * ($M - 2)), 12) + 30 * $M + 1 * $DD + 1721027 - 1753200
EndFunc   ;==>Date_Julian2JD

Func Date_Gregorian2JD(Const $DD, Const $MM, Const $JJ)
	Local $y4800 = $JJ + 4800
    Local $a = division($y4800, 100)
    Local $b = Mod($y4800 ,100)
    Return 146097 * division($a, 4) + 36524 * Mod($a , 4) + 1461 * division($b, 4) + 365 * Mod($b , 4) + division((7 * ($MM - 2)), 12) + 30 * $MM + 1 * $DD + 1721029 - 1753164
 	#cs
	Local $Y = $JJ, $M = $MM, $a, $b
	Local $D = $DD + $Tim / 24
	If $M = 1 Or $M = 2 Then
		$Y -= 1
		$M += 12
	EndIf
	$a = Floor($Y / 100)
	$b = 2 - $a + Floor($a / 4)
	if ($Y * 10000 + $M * 100 + $D) < 15821015 Then $b = 0
	Return Floor(365.25 * ($Y + 4716)) + Floor(30.6001 * ($M + 1)) + $D + $b - 1524.5
	#ce
EndFunc   ;==>Date_Gregorian2JD

Func Date_JD2Gregor(Const $JD, ByRef $aGregRet)
	Local $a, $b, $c, $D, $e, $f, $g, $h, $k, $l, $t, $M, $J, $m_strich, $j_strich

	$a = 1 * $JD + 32044
	$b = division($a, 146097)
	$c = Mod($a, 146097)
	$D = _Min(3, division($c, 36524))
	$e = $c - 36524 * $D
	$f = division($e, 1461)
	$g = Mod($e, 1461)
	$h = _Min(3, division($g, 365))
	$k = $g - 365 * $h
	$l = division((111 * $k + 41), 3395)
	$t = $k - 30 * $l - division((7 * $l + 7), 12) + 1
	$m_strich = $l + 3
	$j_strich = 400 * $b + 100 * $D + 4 * $f + 1 * $h - 4800
	$M = month_from_adjusted_month($m_strich)
	$J = year_from_adjusted_year($j_strich, $m_strich);

	$aGregRet[0] = $t
	$aGregRet[1] = $M

	If $J < 1 Then
		$aGregRet[2] = Abs($J - 1) * -1	; = vor unserer Zeit
	Else
		$aGregRet[2] = $J
	EndIf
EndFunc   ;==>Date_JD2Gregor


Func Date_JD2Julian(Const $JD, ByRef $aJulRet)
	Local $a, $b, $c, $D, $e, $f, $t, $m_strich, $j_strich, $M, $J

	$a = 1 * $JD + 32082
	$b = division($a, 1461)
	$c = Mod($a, 1461)
	$D = _Min(3, division($c, 365))
	$e = $c - 365 * $D
	$f = division((111 * $e + 41), 3395)
	$t = $e - 30 * $f - division((7 * $f + 7), 12) + 1
	$m_strich = $f + 3
	$j_strich = 4 * $b + 1 * $D - 4800
	$M = month_from_adjusted_month($m_strich)
	$J = year_from_adjusted_year($j_strich, $m_strich)

	$aJulRet[0] = $t ; Tag
	$aJulRet[1] = $M ; Monat

	If $J < 1 Then ; Jahr
		$aJulRet[2] = Abs($J - 1) * -1		; = vor unserer Zeit
	Else
		$aJulRet[2] = $J
	EndIf
EndFunc   ;==>Date_JD2Julian


Func Date_JD2MJD(Const $JD)
	Return $JD - 2400000.5
EndFunc   ;==>Date_JD2MJD


Func division(Const $zaehler, Const $nenner)
	Local $quotient = $zaehler / $nenner
	Local $rest = Mod($zaehler, $nenner)
	Return Round($quotient - ($rest / $nenner))
EndFunc   ;==>division

Func month_from_adjusted_month($m_strich)
	Return Mod(($m_strich + 11), 12) + 1
EndFunc   ;==>month_from_adjusted_month


Func year_from_adjusted_year($j_strich, $m_strich)
	Return $j_strich + division($m_strich, 13)
EndFunc   ;==>year_from_adjusted_year

Func adjusted_month_from_month($M)
	Return Mod((1 * $M + 9), 12) + 3
EndFunc   ;==>adjusted_month_from_month

Func adjusted_year_from_year($Y, $M)
	Return $Y - 1 + division((1 * $M + 7), 10)
EndFunc   ;==>adjusted_year_from_year


