I am working on a project where I need to convert back and forth between date in month-day-year and in Modified Julian Date formats.
I have conversion routines that I have used and verified in C under both Keil C51 compiler and gcc.
I use an on-line converter that has also been checked:
http://www.csgnetwork.com/julianmodifdateconv.html
The routines were obtained from reputable sources (see references in code comments)
I adapted the routines for B4A and B4J. The changes are only the way variables are declared. Otherwise it is plain vanilla arithmetic.
When I run the code under B4A or B4J, the calculations are off by a day or two both ways but in the same direction so they do not even cancel each other.
Here is the log:
According to the web calculator, MJD 51647 is April 13, 2000.
I have been working on this for a little while and can't seem to find what's wrong.
I suspect it is either an order of precedence issue or the way variable types may be handled, or maybe the way Java optimizes calculations that may result in overflow?
The gcc code uses the same variable types (int and long) and I believe they actually match (ints are signed 32 bit integers and longs are signed 64 bit integers).
Any pointer to potential differences between Java and C would be appreciated.
I have conversion routines that I have used and verified in C under both Keil C51 compiler and gcc.
I use an on-line converter that has also been checked:
http://www.csgnetwork.com/julianmodifdateconv.html
The routines were obtained from reputable sources (see references in code comments)
I adapted the routines for B4A and B4J. The changes are only the way variables are declared. Otherwise it is plain vanilla arithmetic.
B4X:
'=============================================================================
' These functions are adapted to B4A from Tom VanBaak's web site, leapsecond.com
' For background on Julian Day And Modified Julian Day, see http://tycho.usno.navy.mil/mjd.html
'=============================================================================
' Convert year/month/day calendar date to MJD (Modified Julian Day).
' - year is (4-digit calendar year), month is (1-12), day is (1-31)
' - valid For Gregorian dates from 17-Nov-1858 (adapted from sci.astro FAQ)
Sub ymd_to_mjd( sd As DateType ) As Long
Dim year, month, day As Int, mjd As Long
year = sd.y
month = sd.m
day = sd.d
mjd = _
367 * year _
- 7 * (year + (month + 9) / 12) / 4 _
- 3 * ((year + (month - 9) / 7) / 100 + 1) / 4 _
+ 275 * month / 9 _
+ day + 1721028 - 2400000
Return( mjd )
End Sub ' ymd_to_mjd()
' Convert Modified Julian day (mjd) to year/month/day calendar date.
' - year is (4-digit calendar year), month is (1-12), day is (1-31)
' - adapted from Fliegel/van Flandern ACM 11/#10 p 657 Oct 1968
Sub mjd_to_ymd( mjd As Long ) As DateType
Dim J, C, Y, M, d, mo, y As Long
Dim sd As DateType
J = mjd + 2400001 + 68569
C = 4 * J / 146097
J = J - (146097 * C + 3) / 4
Y = 4000 * (J + 1) / 1461001
J = J - 1461 * Y / 4 + 31
M = 80 * J / 2447
d = J - 2447 * M / 80
J = M / 11
sd.d = d
mo = M + 2 - (12 * J)
sd.m = mo
y = 100 * (C - 49) + Y + J
sd.y = y
Return( sd )
End Sub ' mjd_to_ymd()
Here is the log:
B4X:
date in: 4-15-2000
mjd is 51647
date out: 4-12-2000
I have been working on this for a little while and can't seem to find what's wrong.
I suspect it is either an order of precedence issue or the way variable types may be handled, or maybe the way Java optimizes calculations that may result in overflow?
The gcc code uses the same variable types (int and long) and I believe they actually match (ints are signed 32 bit integers and longs are signed 64 bit integers).
Any pointer to potential differences between Java and C would be appreciated.
Last edited: