Newsletters Subscriptions Media Kit About Us Contact Search Home

mgo
OS/400 Edition
Volume 3, Number 44 -- July 11, 2003

More Free-Format RPG Date-Conversion Solutions


Dear Readers:

In the May 7 issue, I lamented that there was no simple way to convert a variable of the date data type to a numeric format in free-format RPG calculation specs. Here are some workarounds I have received from readers.

--Ted


I include a data structure (mmddyyyyDS) that divides a date into month, day, and year portions and includes both numeric and character subfields for the entire date. To convert the date, I use four EVAL operations: three to load the year, month, and day subfields, and one to assign the converted date to a numeric or character variable.

DWorkDate         S               D

DmmddyyyyDS       DS
D  mmddyyyy                      8S 0
D  Month                         2S 0 Overlay(mmddyyyy:1)
D  Day                           2S 0 Overlay(mmddyyyy:3)
D  Year                          4S 0 Overlay(mmddyyyy:5)
D  MonthDay                      4S 0 Overlay(mmddyyyy:1)
D  Char_mmddyyyy                 8A   Overlay(mmddyyyy:1)

DNumericDate      S              8S 0
DCharDate         S              8A

 /free
    // convert WorkDate to numeric and character formats
       Year           =  %subdt(WorkDate:*years);
       Day            =  %subdt(WorkDate:*days);
       Month          =  %subdt(WorkDate:*months);
       NumericDate    =  mmddyyyy;
       CharDate       =  Char_mmddyyyy;
/end-free

I know this is just another way to beat the system, but it works and it keeps one in free-format code.

--Gene



While I'm stuck at V5R1, I use a combination of multiplication and the %SUBDT built-in function to convert a date to a numeric.

D MyDate          s               d
D MyNumeric       s              8s 0

 /free
      // convert to MMDDYYYY format
      MyNumeric = (%subdt(MyDate:*m) * 1000000) +
                  (%subdt(MyDate:*d) * 10000) +
                  (%subdt(MyDate:*y));

      // convert to YYYYMMDD format
      MyNumeric = (%subdt(MyDate:*y) * 10000) +
                  (%subdt(MyDate:*m) * 100) +
                  (%subdt(MyDate:*d));
 /end-free

Yes, this is a little complicated, but I don't need any procedures or functions. You can modify this for any format of course, except for Julian.

--Doug



Thanks for your article about date conversions, which is something I found to be a problem when I started to use free format RPG recently. Our box is on V5R1.

I got around the problem by using data structures. Here's some code I use.

D TodayN          S               D   inz(*sys)

 // CYYMMDD format
D todaydat        S              7  0

D date            DS
D  dateN                  1      7  0
D  CC                            1  0 overlay(dateN)
D  YY                            2  0 overlay(dateN:2)
D  MM                            2  0 overlay(dateN:4)
D  DD                            2  0 overlay(dateN:6)

D Today           DS            10
D  year                   3      4
D  month                  6      7
D  day                    9     10

 /free
     // load system date into todaydat in CYYMMDD format
        Today = %char(TodayN);
        date = '1' + year + month + day;
        todaydat = dateN;
 /end-free

No doubt that it could be written more elegantly than the way I've coded it!

--Bob


Sponsored By
WORKSRIGHT SOFTWARE

600 Billion

That's how much a recent independent study estimated U.S. businesses spend on dirty data. How much of that 600 billion is spent by your company? Cleanse your dirty ZIP Codes and mailing addresses with our software and save big bucks.

WorksRight Software, Inc.
Phone: 601-856-8337
E-mail: software@worksright.com
Web site: www.worksright.com


THIS ISSUE
SPONSORED BY:

Damon Technologies
WorksRight Software


BACK ISSUES

TABLE OF
CONTENTS

Uses for USER

More Free-Format RPG Date-Conversion Solutions

Reader Feedback and Insights: More "Why Java?"


Editors
Howard Arner
Joe Hertvik
Ted Holt
David Morris
Shannon O'Donnell

Managing Editor
Shannon Pastore

Publisher and
Advertising Director:

Jenny Thomas

Advertising Sales Representative
Kim Reed

Contact the Editors
Do you have a gripe, inside dope or an opinion?
Email the editors:
editors@itjungle.com

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.