TechTalk: Determining the day of the week with OPNQRYF.

General
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

OPNQRYF does not have a function that returns the day of week, but you can create your own. OPNQRYF has a %DAYS function, which returns the ordinal value of a date (number of consecutive days) since January 1 of the year A.D. 1. If you subtract the ordinal values of two dates, you find the number of days between the two dates. Dividing by seven tells how many weeks there are between the two dates.

The really interesting thing, however, is that the remainder of the division is the number of weekdays the first date is offset from the second. If the remainder is 0, the two dates are on the same day of the week. If the remainder is 1, the first date is on the weekday following the second. If the remainder is 6, the first date is on the weekday preceding the second. If the remainder is negative, the first date is before the second, and you must add 7 to it to determine the day of the week.

The second date, then, must be a date that falls on whichever day you want to be the standard. If you use January 7, 1940, as the second date, the standard will be Sunday, because January 7, 1940, fell on a Sunday.

6 illustrates how to determine the day of week with OPNQRYF. One of the fields in file MYDATA is JDATE, a six-digit packed decimal field that stores a date in YYMMDD format. OPNQRYF formats the data according to the record layout of file DAYSPF, which contains the fields in MYDATA and a one-digit packed decimal field called DAY.

Figure 6 illustrates how to determine the day of week with OPNQRYF. One of the fields in file MYDATA is JDATE, a six-digit packed decimal field that stores a date in YYMMDD format. OPNQRYF formats the data according to the record layout of file DAYSPF, which contains the fields in MYDATA and a one-digit packed decimal field called DAY.

OPNQRYF converts JDATE to the character field CDATE and adds editing slashes to put the date into MM/DD/YY format in field EDATE. Then it determines the number of days between EDATE and January 7, 1940, divides by seven, and stores the remainder in field DAY. DAY has a value from 0 (Sunday) to 6 (Saturday).

You may prefer to have OPNQRYF return a value from 1 to 7 instead. If so, change the calculation of DAY so it adds 1 to the remainder. You may also prefer to use a day other than Sunday as the base day. If so, change the second date in the calculations for DAY to some date that falls on the desired base day.

- Ted Holt


TechTalk: Determining the day of the week with OPNQRYF.

Figure 6: Determining the Day of Week with OPNQRYF

 /* Field JDATE in DAYSPF is a 6-digit packed + decimal field in YYMMDD format */ OVRDBF FILE(DAYSPF) TOFILE(MYDATA) SHARE(*YES) OPNQRYF FILE((MYDATA)) + FORMAT(DAYSPF) + MAPFLD((CDATE '%DIGITS(JDATE)' *CHAR 6) + (EDATE '%SST(CDATE 3 2) *CAT "/" *CAT + %SST(CDATE 5 2) *CAT "/" *CAT + %SST(CDATE 1 2)') + (DAY '(%DAYS(EDATE) - %DAYS("01/07/40")) // 7')) CALL DAYSR /* written as if it will read DAYSPF */ CLOF OPNID(MYDATA) DLTOVR FILE(DAYSPF) 
TED HOLT

Ted Holt is IT manager of Manufacturing Systems Development for Day-Brite Capri Omega, a manufacturer of lighting fixtures in Tupelo, Mississippi. He has worked in the information processing industry since 1981 and is the author or co-author of seven books. 


MC Press books written by Ted Holt available now on the MC Press Bookstore.

Complete CL: Fifth Edition Complete CL: Fifth Edition
Become a CL guru and fully leverage the abilities of your system.
List Price $79.95

Now On Sale

Complete CL: Sixth Edition Complete CL: Sixth Edition
Now fully updated! Get the master guide to Control Language programming.
List Price $79.95

Now On Sale

IBM i5/iSeries Primer IBM i5/iSeries Primer
Check out the ultimate resource and “must-have” guide for every professional working with the i5/iSeries.
List Price $99.95

Now On Sale

Qshell for iSeries Qshell for iSeries
Check out this Unix-style shell and utilities command interface for OS/400.
List Price $79.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: