• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Find Time and Date Durations in RPG

    December 8, 2010 Hey, Ted

    Time and date calculations fall short in ILE RPG. I have seen many examples of handling date or time math on IT Jungle and other Web sites. All of them calculate one unit of time only–either years, days, hours, minutes, etc. In the application I am working on, I need to find an interval in days, hours and minutes, but I have found nothing that subtracts one date from another and gets a true answer in mixed units.

    –Al

    RPG does not have date and time durations, as SQL and Query do. (For more information about durations, see the articles listed below.) You’ll have to write your own solution, Al, but it isn’t difficult.

    Write a subprocedure that accepts two timestamp values and returns whatever units you need. Take a look at the source code for module TIMEDIFF. This module contains a subprocedure named CalcTimeDiff (calculate time difference), which calculates elapsed times in days, hours and minutes.

    H nomain
    H option(*srcstmt)
    
     /copy prototypes,TimeDiff
    
    P CalcTimeDiff    b                   export
    D CalcTimeDiff    pi
    D  Time1                          z   value
    D  Time2                          z   value
    D  ElapsedDays                   3p 0
    D  ElapsedHours                  3p 0
    D  ElapsedMins                   3p 0
    
     /free
         ElapsedDays = %diff(Time1: Time2: *days);
         if ElapsedDays > *zero;
            Time1 -= %days(ElapsedDays);
         endif;
    
         ElapsedHours = %diff(Time1: Time2: *hours);
         if ElapsedHours > *zero;
            Time1 -= %hours(ElapsedHours);
         endif;
    
         ElapsedMins = %diff(Time1: Time2: *minutes);
    
         return;
     /end-free
    P                 e
    

    Here’s the procedure prototype, also called TIMEDIFF, but stored in source physical file PROTOTYPES.

    D CalcTimeDiff    pr
    D  inTime1                        z   value
    D  inTime2                        z   value
    D  ouElapDay                     3p 0
    D  ouElapHour                    3p 0
    D  ouElapMinute                  3p 0
    

    Use Create RPG Module (CRTRPGMOD) to create a module from the RPG source code.

    CRTRPGMOD MODULE(MYLIB/TIMEDIFF)
              SRCFILE(MYLIB/QRPGLESRC)
              SRCMBR(TIMEDIFF)
    

    Leave it as a module or convert the module to a service program, according to your preference.

    I threw together an example calling program. First, here’s a printer file for a job history report.

    A                                      REF(JOBHIST)
    A          R HEADER                    SPACEB(1)
    A                                     2'Job No'
    A                                     9'WkCtr'
    A                                    15'Start date/time'
    A                                    35'End date/time'
    A                                    55'Days'
    A                                    61'Hrs'
    A                                    65'Mins'
    A          R DETAIL                    SPACEB(1)
    A            JOBNO     R              1
    A            WORKCTR   R            + 1
    A            DATEON    R            + 1
    A            TIMEON    R            + 1
    A            DATEOFF   R            + 1
    A            TIMEOFF   R            + 1
    A            ELAPDAY        3S 0    + 1EDTCDE(M)
    A            ELAPHOUR       3S 0    + 1EDTCDE(M)
    A            ELAPMIN        3S 0    + 1EDTCDE(M)
    

    Here’s the RPG program that reads the job history file and builds the report. Notice the call to CalcTimeDiff.

    H option(*srcstmt: *nodebugio)
    
    Fjobhist   if   e           k disk
    Fqad0421p  o    e             printer
    
     /copy prototypes,TimeDiff
    
     /free
         *inlr = *on;
    
         write Header;
         dow '1';
            read jobhistr;
            if %eof();
               leave;
            endif;
            CalcTimeDiff (
                 %date(DateOff: *iso) + %time(TimeOff: *hms):
                 %date(DateOn:  *iso) + %time(TimeOn:  *hms):
                 ElapDay: ElapHour: ElapMin);
            write Detail;
         enddo;
         return;
     /end-free
    

    And here’s the report. Notice the elapsed time–in days, hours, and minutes–between job-on and job-off in the last three columns.

    Job No WkCtr Start date/time     End date/time       Days  Hrs Mins
     56882 A1010 2010-12-01  8:22:55 2010-12-01  8:24:13             1
     56883 A1020 2010-12-01  8:30:14 2010-12-01 11:18:32        2   48
     56884 B2050 2010-12-01  8:12:14 2010-12-02 15:54:03   1    7   41
     56885 B2085 2010-12-01  9:01:02 2010-12-01  9:25:09            24
     56886 A1340 2010-12-01  9:01:02 2010-12-01  8:50:16            10-
    

    The nice thing about ILE is that you don’t have to wait for IBM to add an opcode to the compiler to implement a feature you need. You can create your own opcodes. What a pity that so many RPG programmers still don’t know how to use subprocedures.

    RELATED STORIES

    Calculate Ages with Query/400

    Query and Date Durations

    Back To Basics: The Date Data Type



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Midrange Dynamics North America

    Git up to speed with MDChange!

    Git can be lightning-fast when dealing with just a few hundred items in a repository. But when dealing with tens of thousands of items, transaction wait times can take minutes.

    MDChange offers an elegant solution that enables you to work efficiently any size Git repository while making your Git experience seamless and highly responsive.

    Learn more.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    VAULT400:  Which is right for you? Online back-up, DR, HA Webinar. Dec. 16, Jan. 20
    LANSA:  FREE Webinar. "Think Beyond Modernization." Dec 16
    PowerTech:  FREE Webinar! 7 Habits Of Highly Secure Organizations. Dec. 15, 10 am CT

    IT Jungle Store Top Book Picks

    BACK IN STOCK: Easy Steps to Internet Programming for System i: List Price, $49.95

    The iSeries Express Web Implementer's Guide: List Price, $49.95
    The iSeries Pocket Database Guide: List Price, $59
    The iSeries Pocket SQL Guide: List Price, $59
    The iSeries Pocket WebFacing Primer: List Price, $39
    Migrating to WebSphere Express for iSeries: List Price, $49
    Getting Started with WebSphere Express for iSeries: List Price, $49
    The All-Everything Operating System: List Price, $35
    The Best Joomla! Tutorial Ever!: List Price, $19.95

    UNIT 4 Opens Hybrid Cloud Division, Buys Polish ERP Developer JDE EnterpriseOne Costs Less on i OS Than Windows or Linux, ITG Says

    Leave a Reply Cancel reply

Volume 10, Number 37 -- December 8, 2010
THIS ISSUE SPONSORED BY:

WorksRight Software
neuObjects
inFORM Decisions

Table of Contents

  • The Top 10 IBM i Security Exposures, Part 2
  • Find Time and Date Durations in RPG
  • Admin Alert: The Nine Stages of a Power i Upgrade

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • FAX/400 And CICS For i Are Dead. What Will IBM Kill Next?
  • Fresche Overhauls X-Analysis With Web UI, AI Smarts
  • Is It Time To Add The Rust Programming Language To IBM i?
  • Is IBM Going To Raise Prices On Power10 Expert Care?
  • IBM i PTF Guide, Volume 27, Number 20
  • POWERUp 2025 –Your Source For IBM i 7.6 Information
  • Maxava Consulting Services Does More Than HA/DR Project Management – A Lot More
  • Guru: Creating An SQL Stored Procedure That Returns A Result Set
  • As I See It: At Any Cost
  • IBM i PTF Guide, Volume 27, Number 19

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle