Post Reply 
Date Functions
01-16-2015, 08:40 AM (This post was last modified: 01-16-2015 04:57 PM by Dieter.)
Post: #11
RE: Date Functions
(01-15-2015 10:37 PM)Wolfgang Wrote:  30 Years ago i coded COBOL and ASSEMBLER and C on Mainframes.

At that time I tried my first programs in PL/I. ;-) But I soon switched to Pascal – never looked back.

Quote:And i saw, that smart coding after compiling often ran with lower speed....

I think we can have both elegant, readable and fast code.

Quote:Next time i'll take a critical look to the code (next weeks).
Everybody is invited to help me in a constructively way like this!

Here is a first suggestion for the N2J routine. It handles dates between 1 Jan 4713 BC and 31 Dec 9999. Input between 5 and 14 Oct 1582 (undefined) is rejected.

Caution! I never wrote a single line of code for the Prime calculator, so the following code most probably will cause syntax errors and will not run without more or less significant modifications. I assume the Prime uses something similar to Pascal, so I tried it this way. ;-)

Code:
EXPORT N2J(nd)
BEGIN
LOCAL B,D,M,Y,YMD;

D:=IP(ABS(nd));
M:=IP(100*FP(ABS(nd)));
Y:=SIGN(nd)*IP(10000*FP(100*ABS(nd)));

YMD:=10000*Y+100*M+D; // build YYYYMMDD

IF YMD < 15821015 THEN     // Julian date?
   BEGIN
   IF YMD > 15821004 THEN MSGBOX("Invalid date (undefined)"); KILL; END;     // invalid date during Jul-Greg transition
   IF Y < -4712 THEN MSGBOX("Invalid date (< 1 Jan 4713 B.C.)"); KILL; END;  // invalid date < 1 Jan 4713 B.C.
   B:=0;                   // no offset for Julian date
   END
ELSE
   B:=2-IP(Y/100)+IP(Y/400);  // offset for Gregorian date
END;

IF M<3 THEN Y:=Y-1; M:=M+12;
RETURN IP(365.25*(Y+4716)) + IP(30.6*(M+1)) + D + B - 1524;

END;

This is not much more than half the original size, and I think it's far more readable. Maybe you can give it a try.

Dieter
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Date Functions - Thomas_Sch - 05-08-2014, 12:49 PM
RE: Date Functions - Wolfgang - 09-19-2014, 05:55 PM
RE: Date Functions - Wolfgang - 01-14-2015, 10:17 PM
RE: Date Functions - ww63 - 01-15-2015, 08:16 AM
RE: Date Functions - Dieter - 01-15-2015, 01:21 PM
RE: Date Functions - Thomas_Sch - 01-15-2015, 08:35 AM
RE: Date Functions - ww63 - 01-15-2015, 08:38 AM
RE: Date Functions - Wolfgang - 01-15-2015, 03:35 PM
RE: Date Functions - Dieter - 01-15-2015, 07:49 PM
RE: Date Functions - Wolfgang - 01-15-2015, 10:37 PM
RE: Date Functions - Dieter - 01-16-2015 08:40 AM
RE: Date Functions - Dieter - 01-18-2015, 06:25 PM
RE: Date Functions - salvomic - 04-09-2015, 03:03 PM
RE: Date Functions - Wolfgang - 01-19-2015, 04:47 PM
RE: Date Functions - Dieter - 01-19-2015, 08:25 PM
RE: Date Functions - Wolfgang - 04-10-2015, 06:38 PM
RE: Date Functions - salvomic - 04-10-2015, 07:27 PM
RE: Date Functions - Wolfgang - 04-13-2015, 10:03 PM
RE: Date Functions - salvomic - 04-13-2015, 10:17 PM
RE: Date Functions - Wolfgang - 04-13-2015, 10:50 PM
RE: Date Functions - salvomic - 04-14-2015, 05:17 AM
RE: Date Functions - Thomas_Sch - 04-14-2015, 07:10 AM
RE: Date Functions - salvomic - 04-14-2015, 08:21 AM
RE: Date Functions - salvomic - 04-23-2016, 04:48 PM
RE: Date Functions - ggauny@live.fr - 07-05-2016, 07:42 AM
RE: Date Functions - ggauny@live.fr - 07-05-2016, 07:53 AM



User(s) browsing this thread: 4 Guest(s)