?
Solved

Julian Dates

Posted on 1999-01-06
15
Medium Priority
?
503 Views
Last Modified: 2010-04-04
Can someone please help.

I'm running a program where i'm converting dates to julian days.

When I run the formula in D3 i get the correct answer for today 199/01/06. When I rin it in D4 I get the wrong answer with the exact same formula. Why?

The prog is as follows:= D4 answer=805073198 D3 answer=2451185.

function date2julian(date:tdatetime):integer;
var y,m,d:word;
    jd:integer;

begin
     decodeDate(date,y,m,d);
     jd:= ( 1461 * ( y + 4800 + ( m - 14 ) div 12 ) ) div 4 +
          ( 367 * ( m - 2 - 12 * ( ( m - 14 ) div 12 ) ) ) div 12 -
          ( 3 * ( ( y + 4900 + ( m - 14 ) div 12 ) div 100 ) ) div 4 +
          d - 32075;
     date2julian:=jd;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
edit1.text:=inttostr(date2julian(now));
end;
0
Comment
Question by:smithsj1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 2
  • +1
15 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 1354978
Hi
try changing jd to a real not a integer then use FloatToStr()
Regards Barry

0
 
LVL 20

Expert Comment

by:Madshi
ID: 1354979
Hmmmm. I tested it with Delphi4. The result is 2451185!!!!!!

Regards, Madshi.
0
 

Author Comment

by:smithsj1
ID: 1354980
Madshi, Barry

It's very strange. I tried converting to real still no success. Could it meybe be the fact that I'm using NT and not 95.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:smithsj1
ID: 1354981
By the way when I try this.

var d:tdatetime;

d:=now;

The answer for d=36166 for D4 and D3.
0
 
LVL 17

Expert Comment

by:inthe
ID: 1354982
Hmmm i just tested  with delphi 3 and delphi 4 on win98 and the
result was 2451185 on both
did you know  on the Delphi Super Page (http://sunsite.icm.edu.pl/delphi/) there is a JDATE component that gives Julian dates,week numbers, etc and it's free .
maybe worth a try
Regards Barry

0
 
LVL 20

Expert Comment

by:Madshi
ID: 1354983
Hmmm. Please try to split your calculation into several pieces. I mean do this:
(1) Check if "(m-14) div 12" is the same for Delphi3/4.
(2) Check if "(y+4900+(m-14) div 12)" is the same...
(3) and so on...

There must be a difference somewhere, but don't ask me where...

Regards, Madshi.
0
 
LVL 1

Expert Comment

by:wamoz
ID: 1354984
The date you quote in your example is "199/01/06"
Do you mean "1999/01/06" ?

It is possible that you are testing using a date for which the Julian day number is actually negative, and D4 is using a larger word size than D3, or something like that.

Can you define Julian date for me? I believe it's a count of days from a reference date, but what's the reference date?

Also, since TDateTime is merely a fractional count of days from a reference date, can't you just cast to integer and compensate any difference between the Julian reference date and the Windows reference date?
0
 

Author Comment

by:smithsj1
ID: 1354985
Madshi

I checked the complete formula line by line.

Line1 converts the year to days. Line2 Month to days. Line3 leaves the days.

When I assosiate a variable to each line the error comes up in the first line "the year".

Wamoz
The reference date for Julian is -4713/11/24 which will return 0. What I've decided to do is use your suggestion and add the difference between Julian and TDateTime refence to TdateTime. However, what's worrying is why I get incorrect answer in D4 - will this error affect any other formulas.

Thanks for the help.
Regards Steve
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1354986
Steve,

     jd:= ( 1461 * ( y + 4800 + ( m - 14 ) div 12 ) ) div 4;

jd is integer, so it can hold a value up to 2147483647.
y,m are word, so they can only hold a value up to 65535.
But if you calculate (y+4800)*1461, you'll get the value 9933339. So what does Delphi do with these value? Does Delphi shorten it to word size? Or does Delphi convert the whole calculation to integer size? I am not sure which way is the way it has to be?
If you want the calculation to be restricted to word size you should write "((blabla*blabla) and $FFFF)". If you want the calculation to run with integer you should do something like this:  "1461 * (integer(y)+4800)".
I don't know why the result of the calculation differs at your computer from Delphi3 to Delphi4 and why it does not differ at my computer. Perhaps there's a hidden compiler parameter somewhere. However, if you are careful with the types, everything should be fine...

Regards, Madshi.
0
 

Author Comment

by:smithsj1
ID: 1354987
Guys I found the problem but with no reasons. What I did was broke the formula into the smallest components then checked each vallue in D4 and D3 the conclusion was that D4 refuses to calculate the M-14 part of the formula, which in this case is 1-14=-13.

I have now amended my function as per below; If I substitute the A1 part of the formula with (m-14) I get the wrong answer but when I calculate this part firs I don't have any problems. Could the reason possible be the number of indented brackets that D4 can handle. (I installed D4 on another PC minimum install and had the same problems).

Thanks for all you comments.
Regards Steve

function date2julian(date:tdatetime):integer;
    var y,m,d:word;
        jd,YD,MD,DD,e,a1:integer;

begin
   decodeDate(date,y,m,d);
   a1:=m-14;
   YD:= ((1461*(y+4800+((a1) div 12))) div 4);
   MD:= ((367*(m-2-12*((a1) div 12))) div 12);
   DD:= ((3*((y+4900+((a1) div 12)) div 100)) div 4);
   e:=  (d - 32075);
   jd:=YD+MD-DD+e;
   date2julian:=jd;
end;

0
 
LVL 20

Expert Comment

by:Madshi
ID: 1354988
I'm quite sure that Delphi has no problems with brackets.

Hmm. Have you installed UpdatePack #2? Perhaps this is the difference between our Delphi4 tests?

Regards, Madshi.
0
 

Author Comment

by:smithsj1
ID: 1354989
Madshi

You hit the nail on the head. I'd downloaded the update but never installed it.

All is now well.

Thank a lot.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1354990
:-)

That's great!

Hmmm. I'm not sure: Shall I answer the question to get the points?
0
 

Author Comment

by:smithsj1
ID: 1354991
Yes go ahead.
0
 
LVL 20

Accepted Solution

by:
Madshi earned 300 total points
ID: 1354992
Thank you...  :-)))

Regards, Madshi.
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question