Math and Variable question.

Pages: 12
I am coding a elapsed time calculator which uses julian dates.
It is pretty much done except for the fact that my formula is messed up.
The formula
JDN = (1461 * (Y + 4800 + (M - 14)/12))/4 +(367 * (M - 2 - 12 * ((M - 14)/12)))/12 - (3 * ((Y + 4900 + (M - 14)/12)/100))/4 + D - 32075; // julian day number algorithm

But when I subtract the two julian dates I find out that the dates when converted to elapsed time like 6 years, 10 days 5 hours 10 minutes 8 seconds
it doesn't take into account leap years...
I cannot find a formula that takes into account leap years..
so my thought is to set every year into an integer and use a mother load of if statements... but that I dont think would work too well and it would be a pain to code. Some one said I could just divide the elapsed julian dates by 365.2425 which would somewhat take into account leap years. But the same person said it could be off by a couple days to months. So I need to try to find a way to take into account leap years. Can some one please lead me in the right direction.
1. Convert both instants to seconds since a fixed instant.
2. Subtract.
3. Convert back to years-weeks-days-hours-minutes-seconds.
Last edited on
How would that compensate for leap years?
If I did that I would some how have to add 86400 seconds every leap year from the date started.
(86400 Being the amount of seconds in a day.)
Who ever came up with the concept of leap years should be shot...
sorry bout that
Last edited on
Simply multiply the year by 86400*365.25 (the Julian calendar has different rules than the Gregorian calendar). The result shouldn't ever be off by more than a day. Either that, or you'll have to count how many leap days there have been between the dates, which isn't exactly easy.

Who ever came up with the concept of leap years should be shot...
Hey, I was born a leap day!
The alternative would be increasingly inaccurate calendars.
Last edited on
Either that, or you'll have to count how many leap days there have been between the dates, which isn't exactly easy.


Not easy... But I am up for the challenge. And I am assuming that this would be 99.9%
accurate.
How would i do this...
i.e Increments, If statements, a whole TON of integers ; I am guessing a combination of all three.
Hey, I was born a leap day!

Sorry, This is messing me up so badly.
I am trying to go with almost 100% accuracy.
"Who the hell forgot to sync the earth's rotation with it's orbit around the sun before going live with this simulation?"
Damn r/l. The graphics are great but all the quests suck and the server time is always off.

What?
"Who the hell forgot to sync the earth's rotation with it's orbit around the sun before going live with this simulation?"


And What???
Damn r/l. The graphics are great but all the quests suck and the server time is always off.


I have no idea what you two are talking about...

Simply multiply the year by 86400*365.25 (the Julian calendar has different rules than the Gregorian calendar). The result shouldn't ever be off by more than a day. Either that, or you'll have to count how many leap days there have been between the dates, which isn't exactly easy.


So how do I count the leap days in between dates?
So how do I count the leap days in between dates?
And rob you of the fun of figuring it out?
Well, Can you rob some of my fun?
This is all I asked
i.e Increments, If statements, a whole TON of integers ;
Last edited on
A leap year in the Gregorian calender is a (bissextile) year of 366 days if its numerical designation is divisible by 4 and if they are centurial years are also divisible by 400. So years 1600, 2000 and 2400 are leap years and years 1700, 1800, 1900 and 2100 are not leap years. Meeus-- Astronomical Algorithms --pp62.
Last edited on
BTW Your formula is horrible to read and has caused my eyes to cross..
From the same source:
To convert Gregorian date (D,M,Y)
1. If M>2 leave Y and M unchanged else if M==1 or M==2 replace Y with Y-1 and M with M+12.

2. A== int(Y/100) and B ==2-A+int(A/4).

3. JD (Julian Day)== int(365.25(Y+4716)) + int(30.6001(M+1)) + D + B - 1524.5.

Note: D can be any real number i.e. hold a fraction of a day like 17.46 or 11:2:24 hrs on 17th day.
Last edited on
@buffbill Is this how to account for leap years with 99.9% accuracy
BTW Your formula is horrible to read and has caused my eyes to cross..

Yours Is too...
Edit: Your formula looks like C++ code. But I cant make it out since it is so close together.
Buffbill Where can I download your source?
Last edited on
The source is by Jean Meeus. The name is as above.
The accuracy is very high and >99.9%
To convert Gregorian date (D,M,Y)

I don't need to convert back to gregorian calender date I need to find elapsed time between dates
1. take the time and the date into an int. Like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
/* Global Vars. */

// \/ year month day
int y = 1996;
int m = 03;
int d = 31;  // day month year 

// \/ hour minute second
int  h = 23;
int mn = 59;
int s = 59;
int jdn1 , jdn2;
double tod; // time of day
double sec // dividing tod by seconds in a day to come up with a fractional julian day 


and convert into a julian day:
1
2
3
jdn1 = (1461 * (Y + 4800 + (M - 14)/12))/4;
jdn2 = jdn1 + (367 * (M - 2 - 12 * ((M - 14)/12)))/12 - (3 * ((Y + 4900 + (M - 14)/12)/100))/4 + D - 32075;
/* above caculated julian date with correct user input; */



and turn it into a fractional day

1
2
3
4
5
6
7
8
9
 
/* finding the amount of seconds elapsed in that day */
tod =  (H * 3600) + (MN * 60) + S;

/* turning it into a fractional day */
sec = TOD1/86400;

/* adding back jdn 2 to sec because that was an int */
out = jdn2 + sec; 


Do this with 2 dates and store both as a variable...
And subtract i.e
today's julian date is
2455217.17222
and the julian date computed above
would be
2450174.49999

so

2455217.17222 - 2450174.49999 = 5042.67223

so what I am asking is how to convert that to elapsed time (in julian days [i.e 5042.67223]) 100% accurately in compliance with leap years.
and are those formulas I used to convert those dates compliant with leap years.

And when I subtract the two dates (if all of the math was compliant with leap years) how do I make a number like 5042.67223 into elapsed time.
With a format like Years, Months, Days, Hours, Minutes, Seconds.
I have formulas to get the week day, minutes, seconds, and hours.
But this will all get thrown off it even One formula isn't leap year compliant.
Buffbill - Ive googled and googled and I can't find any thing to help me because looks like this has only been done in languages like java and ?php? and all the java source code is messed up i.e no formulas.
So a little help?
NOTE: I shortened my julian day formula so maybe you guys could read it better.
Last edited on
I think I did your formula right but I never have had to do something like this so don't bash:
Weird compile errors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <iomanip>
using namespace std;
int d,m,y,a,b;
int jd;
int main()
{
    cout << "d" << endl;
    cin >> d;
    cout << "m" << endl;
    cin >> m;
    cout << "y" << endl;
    cin >> y;
    cout << endl;
cout << setprecision (16);
if (m > 2) {

        y=0 && m=0;

} else if (m == 1 || m == 2) {

        y = y - 1 && m = m + 12;

     }

a = (y/100);
 b = 2 - a + (a / 4);
jd = (365.25(y +4716)) + (30.6001(m+1)) + d + b - 1524.5; 

cout << "= " << jd << endl;
cin.get();
return 0;
}

compile errors:
17 H:\System\Apps\5C4ADEE2-CAE9-4EC0-BC55-FC1CC3537AF0\Data\jdn_short_formula_test.cpp non-lvalue in assignment
19 H:\System\Apps\5C4ADEE2-CAE9-4EC0-BC55-FC1CC3537AF0\Data\jdn_short_formula_test.cpp non-lvalue in assignment
23 H:\System\Apps\5C4ADEE2-CAE9-4EC0-BC55-FC1CC3537AF0\Data\jdn_short_formula_test.cpp `3.6525e+2' cannot be used as a function
23 H:\System\Apps\5C4ADEE2-CAE9-4EC0-BC55-FC1CC3537AF0\Data\jdn_short_formula_test.cpp `3.06001000000000011880274541908875107765197753906e+1' cannot be used as a function


Edit Please read previous 2 posts.
Last edited on
Who`s bashing?
No.....if (m>2)y=y && m=m; It is only if m<=2 that y and m have to be adjusted.
Please stop calling JD Julian Date......it is the number of days after the year 4716 on January 1 at 12:00:00 (noon)......JD is Julian Day.
This time was chosen by astronomers so that they could be confident that there were no written records prior.
Clearly you need to carry out two JD calculations by the same method to obtain the elapsed time between to-day`s date and the date selected. Yes the formula adjusts for leap years.
BTW 2455217.17222 is 21-1-1010 at 11:2:24 hours
Your answer from this program is in days.
The decimal portion can be converted to hours by multiplying by 24 and noting the whole number.
Discard whole hours from the new result and multiply the residual decimal part by 60 to obtain minutes.
Discard whole minutes from the new result and multiply the residual decimal part by 60 to obtain seconds.
Who`s bashing? Oh sorry I meant no one bash because that is the first time i coded a formula like that (using some of those operators)
Last edited on
Grumble Grumble repost


what are you trying to do on line 17. That syntax makes no sense. You're misusing the && operator... and assigning 2 variables to themselves? What's the point of that?

Same problem on line 19. If you want to do 2 assignments, then just do 2 assignments. There's no use for && here:

1
2
3
4
} else if ( m <= 2 ) {
    y = y - 1; // first assigment  -- or:  y -= 1;  
    m = m + 12;  // second assignment  --  or:  m += 12;
}


I'm assuming you want to multiply on line 23. C is not like algebra:

x(2 + y)

In algebra, this means multiplly x by (2+y)
In C, this means call function x with (2+y) as a parameter.

So when you do this:
365.25(y +4716)

It's thinking 365.25 is supposed to be a function name. If you want to mutiply, use the multiplication operator:

365.25 * (y+4716)
@buffbill
Your answer from this program is in days.

So it is not possible to get the answer in years and days??? (as well as hours minutes and seconds)


BTW 2455217.17222 is 21-1-1010 at 11:2:24 hours


No... right now is 2455217.50278 ( 12:05 PM on 01/21/2010)
JD 2455217.17222 is CE 2010 January 20 16:07:59.8 UT Wednesday (a couple hours ago)
JD 2450174.49999 is CE 1996 March 31 23:59:59.1 UT Sunday
All computed form http://aa.usno.navy.mil/data/docs/JulianDate.php
(Astronomical Applications Department of the U.S. Navy) Which I am sure is right.
BTW there are two formats for julian dates,
CE and BCE (CE being the standard)
there is also two different types of regular julian day "measurements" Julian date (JD) and Julian Day Number (JDN)
(And there are also different types of julian calenders)

I told you this because I am curious to know what you are using as whether JD it is julian day number or julian date, CE or BCE because you got
BTW 2455217.17222 is 21-1-1010 at 11:2:24 hours
Last edited on
Pages: 12