Date Stuff
snippets and parts of manuals...b/c its all not in 1
place....
Snippets
Procedures
Date Data Type - info from IBM docs
Move operation of Date fields (very handy) - tables from IBM docs
Snippets
6 digit date field to a real date field
eval FullDate = %date(MYDATE:*YMD)
Current Date
put the current date/time defined in the D spec or use qRun_Date from PSDS
D CURDATE S D
D CURTIME S T
C eval CURDATE = %date
C eval CURTIME = %time
Subtract months from current Date * subtract 18 months from the current day
D Date18mths S D
C CurDate subdur 18:*months Date18mths
---OR-----
* forget defining something
* subtract 18 months from the current day
C %date subdur 18:*months Date18mths
Date/Timestamp errors
Left justify that string date before using %date function * FileDate is a varying field, so move it left to a 10a field then date stuff will work
* this date ' 5/8/1997' will NOT work in the eval statement(Date/Timestamp error)
* this date '5/8/1997 ' will work in the eval C clear DateChar
C movel FileDate DateChar
C eval DateTest= %date(DateChar:*USA)
Procedures
Procedure "Is Work Day"
Simple little procedure to figure out if its Monday thru Friday * Prototypes
D IsWorkday PI N
D @Date D datfmt(*iso)
* call
C if IsWorkday(mydate) = *on
*======================================================*
* IsWorkDay: Determine if a given date is a normal
* workday.
*======================================================*
P IsWorkday B
D IsWorkday PI N
D @Date D datfmt(*iso)
* Define @BaseDate as a Sunday
D @BaseDate S D inz(d'2000-01-02')
D @Days S 9 0
D @DayOfWeek S 1 0
D @WorkDay S N
*
* How many days are there between our base date and
* the passed date?
C @BaseDate subdur @Date @Days:*d
*
* Based on the number of days, figure out the day of
* week of the passed date (0=Sunday, 1=Monday, etc.)
C eval @DayOfWeek = %rem(@Days: 7)
*
* If date falls on a weekend, it is not a workday...
C if @DayOfWeek = 0 or
C @DayOfWeek = 6
C eval @WorkDay = *off
C*
* Otherwise, it is...
C else
C eval @WorkDay = *on
C endif
*
C return @WorkDay
P IsWorkday E
Day of Week Procedure - cut and paste this into
your program *=====================================================================
* DayOfWeek - procedure to get "day of week", with
* 1=SUN,2=MON,3=TUE,4=WED,5=THU,6=FRI,7=SAT
* other examples show MON as 1, but we aint on the Dilbert calendar
* copy this in your D specs
D*DayOfWeek PR 1P 0
D* DateIn D
* this is your call
C* dou DayOfWeek(SatDate) = 7
C* subdur 1:*d SatDate
C* enddo
* OR this is your call (DOW is just a 1,0 field)
C* eval DOW = DayOfWeek(SatDate)
*=====================================================================
P DayOfWeek B
D PI 1P 0
D DateIn D
*
D Weeks S 7 0
D DayNbr S 1P 0
D AnySaturday S D INZ(d'1997-04-26')
*
C DateIn SUBDUR AnySaturday Weeks:*D
C DIV 7 Weeks
C MVR DayNbr
C IF DayNbr <1
C EVAL DayNbr = DayNbr + 7
C ENDIF
C RETURN DayNbr
P DayOfWeek E
This is from http://publib.boulder.ibm.com/iseries/v5r2/ic2924/books/c092508402.htm
WebSphere Development Studio ILE RPG
Reference
Date Data Type
Date fields have a predetermined size and format. They can be
defined on the definition specification. Leading and trailing zeros are required
for all date data.
Date constants or variables used in comparisons or assignments
do not have to be in the same format or use the same separators. Also, dates
used for I/O operations such as input fields, output fields or key fields are
also converted (if required) to the necessary format for the operation.
The default internal format for date variables is *ISO. This
default internal format can be overridden globally by the control specification
keyword DATFMT and individually by the definition specification keyword DATFMT.
The hierarchy used when determining the internal date format and
separator for a date field is
- From the DATFMT keyword specified on the definition
specification
- From the DATFMT keyword specified on the control
specification
- *ISO
There are three kinds of date data formats, depending on the
range of years that can be represented. This leads to the possibility of a date
overflow or underflow condition occurring when the result of an operation is a
date outside the valid range for the target field. The formats and ranges are as
follows:
Number of
Digits in Year |
Range of Years
|
2 (*YMD,
*DMY, *MDY, *JUL) |
1940 to
2039 |
3 (*CYMD,
*CDMY, *CMDY) |
1900 to
2899 |
4 (*ISO,
*USA, *EUR, *JIS, *LONGJUL) |
0001 to
9999 |
Table 13 lists the RPG-defined formats for date data
and their separators.
For examples on how to code date fields, see the examples in:
Table 13. RPG-defined date formats
and separators for Date data type
Format Name
|
Description
|
Format (Default
Separator) |
Valid
Separators |
Length
|
Example
|
2-Digit Year Formats
|
*MDY
|
Month/Day/Year |
mm/dd/yy
|
/ - . ,
'&' |
8
|
01/15/96
|
*DMY
|
Day/Month/Year |
dd/mm/yy
|
/ - . ,
'&' |
8
|
15/01/96
|
*YMD
|
Year/Month/Day |
yy/mm/dd
|
/ - . ,
'&' |
8
|
96/01/15
|
*JUL
|
Julian
|
yy/ddd
|
/ - . ,
'&' |
6
|
96/015
|
4-Digit Year Formats
|
*ISO
|
International Standards Organization |
yyyy-mm-dd
|
-
|
10
|
1996-01-15
|
*USA
|
IBM USA
Standard |
mm/dd/yyyy
|
/
|
10
|
01/15/1996
|
*EUR
|
IBM
European Standard |
dd.mm.yyyy
|
.
|
10
|
15.01.1996
|
*JIS
|
Japanese
Industrial Standard Christian Era |
yyyy-mm-dd
|
-
|
10
|
1996-01-15
|
Table 14 lists the *LOVAL, *HIVAL, and default values
for all the RPG-defined date formats.
Table 14. Date
Values
Format name
|
Description
|
*LOVAL
|
*HIVAL
|
Default Value
|
2-Digit Year Formats |
*MDY
|
Month/Day/Year |
01/01/40
|
12/31/39
|
01/01/40
|
*DMY
|
Day/Month/Year |
01/01/40
|
31/12/39
|
01/01/40
|
*YMD
|
Year/Month/Day |
40/01/01
|
39/12/31
|
40/01/01
|
*JUL
|
Julian
|
40/001
|
39/365
|
40/001
|
4-Digit Year Formats |
*ISO
|
International Standards Organization |
0001-01-01
|
9999-12-31 |
0001-01-01
|
*USA
|
IBM USA
Standard |
01/01/0001
|
12/31/9999 |
01/01/0001
|
*EUR
|
IBM
European Standard |
01.01.0001
|
31.12.9999 |
01.01.0001
|
*JIS
|
Japanese
Industrial Standard Christian Era |
0001-01-01
|
9999-12-31 |
0001-01-01
|
Several formats are also supported for fields used by the MOVE, MOVEL, and
TEST operations only. This support is provided for compatibility with externally
defined values that are already in a 3-digit year format and the 4-digit year
*LONGJUL format. It also applies to the 2-digit year formats when *JOBRUN is
specified.
*JOBRUN should be used when the field which it is describing is known to have
the attributes from the job. For instance, a 12-digit numeric result of a TIME
operation will be in the job date format.
Table 15 lists the valid externally defined date
formats that can be used in Factor 1 of a MOVE, MOVEL, and TEST operation.
Table 15. Externally defined date
formats and separators
Format Name
|
Description
|
Format (Default
Separator) |
Valid
Separators |
Length
|
Example
|
2-Digit Year Formats
|
*JOBRUN1 |
Determined at runtime from the DATFMT, or DATSEP
job values. |
3-Digit Year
Formats2 |
*CYMD
|
Century
Year/Month/Day |
cyy/mm/dd
|
/ - . ,
'&' |
9
|
101/04/25
|
*CMDY
|
Century
Month/Day/Year |
cmm/dd/yy
|
/ - . ,
'&' |
9
|
104/25/01
|
*CDMY
|
Century
Day/Month/Year |
cdd/mm/yy
|
/ - . ,
'&' |
9
|
125/04/01
|
4-Digit Year Formats
|
*LONGJUL
|
Long
Julian |
yyyy/ddd
|
/ - . ,
'&' |
8
|
2001/115
|
Notes:
- *JOBRUN is valid only for character or numeric dates
with a 2-digit year since the run-time job attribute for DATFMT can only
be *MDY, *YMD, *DMY or *JUL.
- Valid values for the century character 'c' are:
'c' Years
-----------------------
0 1900-1999
1 2000-2099
. .
. .
. .
9 2800-2899
|
from http://publib.boulder.ibm.com/iseries/v5r2/ic2924/books/c092508430.htm#HDRZZMOVE
WebSphere(R) Development Studio ILE RPG Reference
Table 53. Moving a Character Field to
a Date-Time Field
Factor 1
specifies the format of the Factor 2 entry |
Factor 1 Entry
|
Factor 2 (Character)
|
Result Field |
Value
|
DTZ Type
|
*MDY-
|
11-19-75
|
75/323 |
D(*JUL) |
*JUL
|
92/114
|
23/04/92 |
D(*DMY) |
*YMD
|
14/01/28
|
01/28/2014 |
D(*USA) |
*YMD0
|
140128
|
01/28/2014 |
D(*USA) |
*USA
|
12/31/9999
|
31.12.9999 |
D(*EUR) |
*ISO
|
2036-05-21
|
21/05/36 |
D(*DMY) |
*JUL
|
45/333
|
11/29/1945 |
D(*USA) |
*MDY/
|
03/05/33
|
03.05.33 |
D(*MDY.) |
*CYMD&
|
121 07 08
|
08.07.2021 |
D(*EUR) |
*CYMD0
|
1210708
|
07,08,21 |
D(*MDY,) |
*CMDY.
|
107.08.21
|
21-07-08 |
D(*YMD-) |
*CDMY0
|
1080721
|
07/08/2021 |
D(*USA) |
*LONGJUL-
|
2021-189
|
08/07/2021 |
D(*EUR) |
*HMS&
|
23 12 56
|
23.12.56 |
T(*ISO) |
*USA
|
1:00 PM
|
13.00.00 |
T(*EUR) |
*EUR
|
11.10.07
|
11:10:07 |
T(*JIS) |
*JIS
|
14:16:18
|
14.16.18 |
T(*HMS.) |
*ISO
|
24.00.00
|
12:00 AM |
T(*USA) |
Blank
|
1991-09-14-13.12.56.123456 |
1991-09-14-13.12.56.123456 |
Z(*ISO) |
*ISO
|
1991-09-14-13.12.56.123456 |
1991-09-14-13.12.56.123456 |
Z(*ISO) |
Table 54. Moving a Numeric Field to a
Date-Time Field
Factor 1
specifies the format of the Factor 2 entry |
Factor 1 Entry1
|
Factor 2 (Numeric)
|
Result Field |
Value
|
DTZ Type
|
*MDY
|
111975
|
75/323 |
D(*JUL) |
*JUL
|
92114
|
23/04/92 |
D(*DMY) |
*YMD
|
140128
|
01/28/2014 |
D(*USA) |
*USA2 |
12319999
|
31.12.9999 |
D(*EUR) |
*ISO
|
20360521
|
21/05/36 |
D(*DMY) |
*JUL
|
45333
|
11/29/1945 |
D(*USA) |
*MDY
|
030533
|
03.05.33 |
D(*MDY.) |
*CYMD
|
1210708
|
08.07.2021 |
D(*EUR) |
*CMDY
|
1070821
|
21-07-08 |
D(*YMD-) |
*CDMY
|
1080721
|
07/08/2021 |
D(*USA) |
*LONGJUL
|
2021189
|
08/07/2021 |
D(*EUR) |
*USA
|
*DATE
(092195)3 |
1995-09-21 |
D(*JIS) |
Blank
|
*DATE
(092195)3 |
1995-09-21 |
D(*JIS) |
*MDY
|
UDATE
(092195)3 |
21.09.1995 |
D(*EUR) |
*HMS
|
231256
|
23.12.56 |
T(*ISO) |
*EUR
|
111007
|
11:10:07 |
T(*JIS) |
*JIS
|
141618
|
14.16.18 |
T(*HMS.) |
*ISO
|
240000
|
12:00 AM |
T(*USA) |
Blank4 |
19910914131256123456 |
1991-09-14-13.12.56.123456 |
Z(*ISO) |
Notes:
- A separator of zero (0) is not allowed in factor 1 for
movement between date, time or timestamp fields and numeric classes.
- Time format *USA is not allowed for movement between
time and numeric classes.
- For *DATE and UDATE, assume that the job date in the
job description is of *MDY format and contains 092195. Factor 1 is
optional and will default to the correct format. If factor 2 is *DATE,
and factor 1 is coded, it must be a 4-digit year date format. If factor
2 is UDATE, and factor 1 is coded, it must be a 2-digit year date
format.
- For moves of timestamp fields, factor 1 is optional. If
it is coded it must be *ISO or *ISO0.
|
Table 55. Moving a Date-Time Field
to a Character Field
Factor 1 Entry
|
Factor 2
|
Result Field (Character)
|
Value
|
DTZ Type
|
*JUL
|
11-19-75 |
D(*MDY-) |
75/323
|
*DMY-
|
92/114 |
D(*JUL) |
23-04-92
|
*USA
|
14/01/28 |
D(*YMD) |
01/28/2014
|
*EUR
|
12/31/9999 |
D(*USA) |
31.12.9999
|
*DMY,
|
2036-05-21 |
D(*ISO) |
21,05,36
|
*USA
|
45/333 |
D(*JUL) |
11/29/1945
|
*USA0
|
45/333 |
D(*JUL) |
11291945
|
*MDY&
|
03/05/33 |
D(*MDY) |
03 05 33
|
*CYMD,
|
03
07 08 |
D(*MDY&); |
108,03,07
|
*CYMD0
|
21/07/08 |
D(*DMY) |
1080721
|
*CMDY
|
21-07-08 |
D(*YMD-) |
107/08/21
|
*CDMY-
|
07/08/2021 |
D(*USA) |
108-07-21
|
*LONGJUL& |
08/07/2021 |
D(*EUR) |
2021 189
|
*ISO
|
23
12 56 |
T(*HMS&); |
23.12.56
|
*EUR
|
11:00 AM |
T(*USA) |
11.00.00
|
*JIS
|
11.10.07 |
T(*EUR) |
11:10:07
|
*HMS,
|
14:16:18 |
T(*JIS) |
14,16,18
|
*USA
|
24.00.00 |
T(*ISO) |
12:00 AM
|
Blank
|
2045-10-27-23.34.59.123456 |
Z(*ISO) |
2045-10-27-23.34.59.123456 |
Table 56. Moving a Date-Time Field
to a Numeric Field
Factor 1 Entry
|
Factor 2
|
Result Field (Numeric)
|
Value
|
DTZ Type
|
*JUL
|
11-19-75 |
D(*MDY-) |
75323
|
*DMY-
|
92/114 |
D(*JUL) |
230492
|
*USA
|
14/01/28 |
D(*YMD) |
01282014
|
*EUR
|
12/31/9999 |
D(*USA) |
31129999
|
*DMY,
|
2036-05-21 |
D(*ISO) |
210536
|
*USA
|
45/333 |
D(*JUL) |
11291945
|
*MDY&
|
03/05/33 |
D(*MDY) |
030533
|
*CYMD,
|
03
07 08 |
D(*MDY&); |
1080307
|
*CMDY
|
21-07-08 |
D(*YMD-) |
1070821
|
*CDMY-
|
07/08/2021 |
D(*USA) |
1080721
|
*LONGJUL& |
08/07/2021 |
D(*EUR) |
2021189
|
*ISO
|
23
12 56 |
T(*HMS&); |
231256
|
*EUR
|
11:00 AM |
T(*USA) |
110000
|
*JIS
|
11.10.07 |
T(*EUR) |
111007
|
*HMS,
|
14:16:18 |
T(*JIS) |
141618
|
*ISO
|
2045-10-27-23.34.59.123456 |
Z(*ISO) |
20451027233459123456 |
Table 57. Moving Date-Time Fields to
Date-Time Fields
Assume that
the initial value of the timestamp is 1985-12-03-14.23.34.123456.
|
Factor 1 |
Factor 2 |
Result Field |
Value
|
DTZ Type
|
Value
|
DTZ Type
|
N/A
|
1986-06-24 |
D(*ISO) |
86/06/24 |
D(*YMD) |
N/A
|
23 07 12 |
D(*DMY&); |
23.07.2012 |
D(*EUR) |
N/A
|
11:53 PM |
T(USA) |
23.53.00 |
T(*EUR) |
N/A
|
19.59.59 |
T(*HMS.) |
19:59:59 |
T(*JIS) |
N/A
|
1985-12-03-14.23.34.123456 |
Z(*ISO.) |
1985-12-03-14.23.34.123456 |
Z(*ISO) |
N/A
|
75.06.30 |
D(*YMD.) |
1975-06-30-14.23.34.123456 |
Z(*ISO) |
N/A
|
09/23/2234 |
D(*USA) |
2234-09-23-14.23.34.123456 |
Z(*ISO) |
N/A
|
18,45,59 |
T(*HMS,) |
1985-12-03-18.45.59.000000 |
Z(*ISO) |
N/A
|
2:00 PM |
T(*USA) |
1985-12-03-14.00.00.000000 |
Z(*ISO) |
N/A
|
1985-12-03-14.23.34.123456 |
Z(*ISO.) |
12/03/85 |
D(*MDY) |
N/A
|
1985-12-03-14.23.34.123456 |
Z(*ISO.) |
12/03/1985 |
D(*USA) |
N/A
|
1985-12-03-14.23.34.123456 |
Z(*ISO.) |
14:23:34 |
T(*HMS) |
N/A
|
1985-12-03-14.23.34.123456 |
Z(*ISO.) |
02:23 PM |
T(*USA) |
Table 58. Moving a Date field to a
Character field
The result
field is larger than factor 2. Assume that Factor 1 contains *ISO and that
the result field is defined as
D Result_Fld 20A INZ('ABCDEFGHIJabcdefghij')
|
Operation Code
|
Factor 2 |
Value of Result Field after move operation
|
Value
|
DTZ Type
|
MOVE
|
11 19 75 |
D(*MDY&); |
'ABCDEFGHIJ1975-11-19' |
MOVE(P)
|
11 19 75 |
D(*MDY&); |
'
1975-11-19' |
MOVEL
|
11 19 75 |
D(*MDY&); |
'1975-11-19abcdefghij' |
MOVEL(P)
|
11 19 75 |
D(MDY&); |
'1975-11-19 ' |
Table 59. Moving a Time field to a
Numeric field
The result
field is larger than factor 2. Assume that Factor 1 contains *ISO and that
the result field is defined as
D Result_Fld 20S INZ(11111111111111111111)
|
Operation Code
|
Factor 2 |
Value of Result Field after move operation
|
Value
|
DTZ Type
|
MOVE
|
9:42 PM |
T(*USA) |
11111111111111214200 |
MOVE(P)
|
9:42 PM |
T(*USA) |
00000000000000214200 |
MOVEL
|
9:42 PM |
T(*USA) |
21420011111111111111 |
MOVEL(P)
|
9:42 PM |
T(*USA) |
21420000000000000000 |
Table 60. Moving a Numeric field to a
Time field
Factor 2 is
larger than the result field. The highlighted portion shows the part of
the factor 2 field that is moved. |
Operation Code
|
Factor 2 |
Result Field |
DTZ Type
|
Value
|
MOVE
|
11:12:13:14 |
T(*EUR) |
12.13.14 |
MOVEL
|
11:12:13:14 |
T(*EUR) |
11.12.13 |
Table 61. Moving a Numeric field to a
Timestamp field
Factor 2 is
larger than the result field. The highlighted portion shows the part of
the factor 2 field that is moved. |
Operation Code
|
Factor 2 |
Result Field |
DTZ Type
|
Value
|
MOVE
|
12340618230323123420123456 |
Z(*ISO) |
1823-03-23-12.34.20.123456 |
MOVEL
|
12340618230323123420123456 |
Z(*ISO) |
1234-06-18-23.03.23.123420 |
You are here: Home-Computer Tips & Help-AS400-RPG Dates
Next Topic: RPG snippets
|