30 pts.
 Convert an *iso date to *longjul in CLP
I am trying to convert an *iso date to a *longjul date in a CL program so I can subtract 6 days from today. It is crashing with a message "Date contains too many or too few numeric characters." My code is : Definitions dcl var(&julend) type(*char) len(7) dcl var(&jul) type(*dec) len(8 0) Code cvtdat date(&today) tovar(&julend) fromfmt(*iso) + tofmt(*longjul) tosep(*none) chgvar var(&jul) value(&julend) chgvar var(&jul) value(&jul - 6) chgvar var(&julend) value(&jul) cvtdat date(&julend) tovar(&begdte) + fromfmt(*longjul) tofmt(*iso) tosep(*none) I think my problem is I'm going from 2013-01-04 back 6 days to 2012-12-28. Am I using the correct number in my subtraction of 6 days ? Cheers, Jenny

Software/Hardware used:
ASKED: January 6, 2013  11:19 PM
UPDATED: January 7, 2013  1:29 PM

Answer Wiki:
Last Wiki Answer Submitted:  Be the first to answer this question.
All Answer Wiki Contributors:  Be the first to answer this question. Michael Tidmarsh   11,405 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

If your starting date is ’2013-01-04′, the *LONGJUL value will be 2013004. The result of (2013004 – 6) will be 2012998. You probably can guess that the 998th day of 2012 will be quite a ways beyond the end of the year.
 
In general, you can’t subtract numeric values from dates. You should only subtract “durations”. However, CL doesn’t support date duration operations.
 
You could write a small RPG (or COBOL, C or REXX) procedure and bind it into your program. Or you could code some ugly CL that manipulates values to simulate date duration math. Personally, since this already about CL, I would use the ILE CEE* date APIs to turn dates into numeric values and back into dates.
 
Here’s a simple example that you can do some testing with:
pgm ( +
&FromDate +
&NbrDays +
&ResultDate +
)

dcl &FromDate *char 10
dcl &NbrDays *dec ( 3 )
dcl &ResultDate *char 10

dcl &RtnVal1 *int
dcl &RtnVal2 *int
dcl &PicStr *char 10 value( ‘YYYY-MM-DD’ )

callprc CEEDAYS ( +
&FromDate +
&PicStr +
&RtnVal1 +
*omit +
)

chgvar &RtnVal2 ( &RtnVal1 – &NbrDays )

callprc CEEDATE ( +
&RtnVal2 +
&PicStr +
&ResultDate +
*omit +
)

Exit:

return

endpgm

That proc receives a date in ‘YYYY-MM-DD’ format and a number of days. It subtracts the number of days from the date and returns the new resulting date. You can look at the documentation for CEEDAYS to see how it turns the date into a number. After the CL subtracts &NbrDays from that number, the API docs will describe how CEEDATE turns the new number back into a date.
 
Any discussion that you need for details can be continued here. I hope the formatting of the source code works out and you can read it. Be aware that this doesn’t do any validation of the incoming values; the error code (function code, fc) is omitted. That can be covered if the docs are too confusing.
 
Tom

 108,205 pts.

 

Well, I guess it came out readable, but not presented in formatting. I’ll leave it alone for now.
 
Tom

 108,205 pts.

 

Thanks, I ended up writing a small rpg program to just add or subtract number of days to an *iso date and called that from the CL.  Cheers, Jenny

 30 pts.

 

Vendor response.
Using the eXtreme CL Date and Time Support (one time charge, $19.95), available with V5R4 and all following releases, you could use the Change Date command (ChgDatXCL) as shown iin the following program (using your data values):
Pgm                                                              Dcl        Var(&Today)   Type(*Char) Len(10) Value(’2013-01-04′) Dcl        Var(&Answer)  Type(*Char) Len(10)                                                                                      ChgDatXCL  Var(&Answer) Fmt(*ISO) +                                           DatAdj((*Sub 6 *Days)) BasVal(&Today) +                          BasFmt(*ISO)                                                                                                         SndPgmMsg  Msg(&Answer) ToPgmQ(*Ext)                                                                                              EndPgm                                                          
To give you a feel for what the command supports, the online documentation can be found at http://www.powercl.com/xcl/xclcommands/chgdatxcl
I realize you’ve already written a RPG program to meet your specific need, but this (and other commands in Date and Time) might be useful in the future.
Bruce
End vendor response
 

 6,055 pts.