Days Caluclation between two dates



Software AG's platform-independent programming language with full support for open-source and Internet applications

Days Caluclation between two dates

Postby rummylion » Fri Jun 22, 2012 7:16 pm

Hi,

I am beginner to Natural language and I have a requirement in which I need to calculate days between two given dates.
Does Natural language has any inbuilt functions to do the same??? If not, Can any one please help me out the best possible way to achieve it.


Regards,
Rummylion.
rummylion
 
Posts: 3
Joined: Thu Jun 21, 2012 2:53 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Days Caluclation between two dates

Postby Akatsukami » Fri Jun 22, 2012 7:22 pm

Can the LE date manipulation functions be called from Natural?
"You have sat too long for any good you have been doing lately ... Depart, I say; and let us have done with you. In the name of God, go!" -- what I say to a junior programmer at least once a day
User avatar
Akatsukami
Global moderator
 
Posts: 1058
Joined: Sat Oct 16, 2010 2:31 am
Location: Bloomington, IL
Has thanked: 6 times
Been thanked: 51 times

Re: Days Caluclation between two dates

Postby rummylion » Fri Jun 22, 2012 7:36 pm

No, I am supposed to write this using Natural language only.
rummylion
 
Posts: 3
Joined: Thu Jun 21, 2012 2:53 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Days Caluclation between two dates

Postby dick scherrer » Sat Jun 23, 2012 12:06 am

Hello and welcome to the forum,

Maybe Ralph Zbrog will see this and reply.

Here is something he posted in another forum:
. . . there is a common practice is to convert your external date variable (eg: yyyymmdd) to a temporary D-format variable using a MOVE EDITED. The D-format makes comparisons and computations simple and straightforward.


With this guidance and the documentation, hopefully you can proceed.
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: Days Caluclation between two dates

Postby prino » Sat Jun 23, 2012 2:47 am

/**********************************************************************
* Convert a date to a Julian Day Number                               *
**********************************************************************/
cj: proc(fld, flm, fly, jdn);
dcl fld float     (16);
dcl flm float     (16);
dcl fly float     (16);
dcl jdn fixed bin (31);

  fly = fly + (flm - 2.85) / 12;

  jdn = floor(
          floor(
            floor(367 * fly) - 1.75 * floor(fly) + fld) -
              0.75 * floor(fly / 100)) + 1721115;
end cj;

/**********************************************************************
* Convert a Julian Day Number to a date                               *
**********************************************************************/
jc: proc(jdn, dd, mm, yyyy);
dcl jdn  fixed      (7);
dcl dd   fixed      (3);
dcl mm   fixed      (3);
dcl yyyy fixed      (5);

dcl flc  float     (16);
dcl fln  float     (16);

  fln  = jdn - 1721119.2;
  flc  = trunc(fln / 36524.25);
  fln  = fln + flc - trunc(flc / 4);

  yyyy = trunc(fln / 365.25);
  fln  = fln - trunc(365.25 * yyyy) - 0.3;
  mm   = trunc(fln / 30.6);
  dd   = trunc(fln - 30.6 * mm + 1);

  if mm > 9 then
    do;
      mm   = mm   - 9;
      yyyy = yyyy + 1;
    end;
  else
    mm = mm + 3;
end jc;
Robert AH Prins
robert.ah.prins @ the.17+Gb.Google thingy
User avatar
prino
 
Posts: 635
Joined: Wed Mar 11, 2009 12:22 am
Location: Vilnius, Lithuania
Has thanked: 3 times
Been thanked: 28 times

Re: Days Caluclation between two dates

Postby RGZbrog » Sun Jun 24, 2012 12:52 pm

Yes, Natural can handle date calculations natively, using any combination of date literals, date-format variables, or system date variable.

DEFINE DATA LOCAL
1 #FROM (D)  INIT <D'06/15/2012'>
1 #TO (D)    INIT <D'06/18/2012'>
1 #DAYS (N3)
END-DEFINE
#DAYS := D'06/18/2012' - D'06/15/2012'
WRITE #DAYS 'days between D"06/18/2012" and D"06/15/2012"'
*
#DAYS := #TO - #FROM
WRITE #DAYS 'days between #FROM and #TO'
*
#DAYS := *DATX - D'06/15/2012'
WRITE #DAYS 'days between *DATX and D"06/15/2012"'
END

Page     1                                                   06/24/12  00:19:56
 
   3 days between D'06/18/2012' and D'06/15/2012'
   3 days between #FROM and #TO
   9 days between *DATX and D'06/15/2012''

Alpha format date values must be converted to internal date format prior to the calculations.

Note that my date literals may not compile on your system, as the date format will be customized for your location. My Natural system expects date literals in the form D'MM/DD/YYYY'.
User avatar
RGZbrog
 
Posts: 101
Joined: Mon Nov 23, 2009 1:34 pm
Location: California, USA
Has thanked: 0 time
Been thanked: 0 time

Re: Days Caluclation between two dates

Postby rummylion » Mon Jun 25, 2012 6:09 pm

Thank you for your help.
rummylion
 
Posts: 3
Joined: Thu Jun 21, 2012 2:53 pm
Has thanked: 0 time
Been thanked: 0 time


Return to Natural