Hi,
Thought I'd cracked it, but the algorithm below (VB) today produced a JD result that was one day less than the true version. Nothing to do with pre-noon because I tried this at 5.00 pm local time (which is Greenwich anyway!)
Update - please read right to the end...
Public Function julian(Y As Integer, m As Integer, d As Integer) As Long
Dim a As Integer, b As Integer
a = Int(Y / 100)
b = 2 - a + Fix(a / 4)
If m > 2 Then
'do nothing
Else
Y = Y - 1
m = m + 12
End If
julian = Int(365.25 * (Y + 4716)) + Fix(30.6001 * (m + 1)) + d + b - 1524.5
End Function
(I have used 'fix' where the value inside the function could be negative even though in a practical sense today it couldn't be, so fix(-2.4) gives -2 as output. What could I be doing wrong?
OK - I don't think the problem is with the algorithm, it's what I was doing after the function ran. I was subtracting 0.5 day from the result whereas if I add 0.5d the result comes out correctly. A note for the unwary maybe?
Hi Michael
If it helps, here's an implementation of calendar date to JD from Meeus, 1991, Astronomical Algorithms, ch 7:
https://sourceforge.net/p/vstar/code/1453/tree/tags/DEV-AAVSO-06May2017/src/org/aavso/tools/vstar/util/date/MeeusDateUtil.java
starting around line 97, for comparison with what you're doing (preserving Meeus's terse variable names in most cases), along with a bunch of unit tests:
https://sourceforge.net/p/vstar/code/1453/tree/tags/DEV-AAVSO-06May2017/test/org/aavso/tools/vstar/util/date/DateUtilTestBase.java
The directories in which those source files exist also contain HJD conversion source (code, test).
Addition: it looks like you may be using Meeus as a reference as well, directly or indirectly. The main differences appear to be that I'm checking for whether a date is before or after the Gregorian Calendar came into effect and the slightly different rounding in the last line of the function.
David
Hi David,
Yes, and of course for my purposes it's purely non-past dates that matter, therefore the 'easy' Meeus. The problem I had with some of his online refs was the text quality of the scanned article - it was hard to tell +, * and - apart sometimes! Actually used the updated/corrected version with my software tonight, and everything now fine. Thanks for your help.
No worries Michael.
David