23
Fri, Feb
3 New Articles

The CL Corner: More Granular Date and Time Support

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

Date addition is nice, but what about hours?

 

In response to the recent articles on working with date durations, I received a note from Lori N. asking if similar support exists for smaller time frames. Specifically, Lori says, "Thanks for the articles; they are very helpful. I have a monitoring job that checks a data area to make sure we have sent updates to another system within a specific time frame. I used CEEDATE to get today and yesterday, but I'd also like to use a time interval so I can say 'If we have not sent anything in the last 6 hours, send an alert.' Is there something that takes the current time and subtracts a specific interval so I can compare the last times? If not, I'll have to be satisfied with checking dates. Thank you for your help with this."

 

To which I respond, "Be satisfied with checking dates rather than hours? We should never compromise on such a basic need when working on i!"

 

There are many ways to satisfy this requirement, and in today's article we'll examine one approach based on the CEELOCT API. In future articles, we'll also look at some of the many alternatives that exist for the CL developer. Back in "So You're Looking for a Date?," we introduced the Get Current Local Time (CEELOCT) API, which returns the local time. The API documentation can be found here, and I've repeated the API's parameter list below:

 

Required Parameters:

1

output_Lilian

Output

INT4

2

output_seconds

Output

FLOAT8

3

output_Gregorian

Output

CHAR23


Omissible Parameter:

4

fc

Output

FEEDBACK

 

In that article, we concentrated on the first parameter, output_Lilian, which is a 4-byte signed integer representing the current local date in Lilian format. At that time, we briefly discussed the other parameters but effectively ignored them. Today we'll take a closer look at the second parameter, output_seconds.

 

Output_seconds is an 8-byte floating point value that represents the number of seconds since 00:00:00 October 14, 1582. That is, an output_seconds value of 86 401 represents 00:00:01 October 15, 1582; a value of 13 431 693 600 represents 10:00:00 June 1, 2008; and 13 431 693 601 is equivalent to 10:00:01 June 1, 2008. Similar to how output_Lilian allows us to easily add and subtract durations involving days, output_seconds allows us to easily add and subtract seconds. In our example program, we'll use a duration of 21 600 seconds, the number of seconds in six hours.

 

The one minor hurdle we have to overcome is that CL doesn't support floating point variables. But we won't let a little problem like that deter us from our mission! We will define output_seconds as an 8-byte character variable and then use the Copy Numeric Value (CPYNV) API (actually a Machine Interface instruction) to convert the 8-byte floating point value to a 15-digit decimal value. We won't go into the details of the Copy Numeric Value API, but the API basically allows us to convert a numeric value from one format to another. In our case, we will convert from a floating point format to a decimal (*DEC) format. The Copy Numeric Value API documentation can be found here and is briefly discussed a bit later in this article.

 

First, we need a program to perform some action--for instance, sending updates, as in Lori's note, and then updating a data area with the time the send was complete. The following program, Send Update (SNDUPD, performs such a function:

 

Pgm

Dcl Var(&Snd_Float) Type(*Char) Len(8)

Dcl Var(&CurLilDt) Type(*Int)

Dcl Var(&CurGregDt) Type(*Char) Len(23)

/* Send the updates and then: */

CallPrc Prc('CEELOCT') Parm((&CurLilDt) (&Snd_Float) +

(&CurGregDt) (*Omit))

ChgDtaAra DtaAra(QGPL/LSTSNDTIME) Value(&Snd_Float)

EndPgm

 

The above program sends the updates (shown as a comment), calls the CEELOCT API to retrieve the current date and time, and then stores the returned floating point value for the current date and time (the variable &Snd_Float) in the data area QGPL/LSTSNDTIME. This data area is created with the following command:

 

CRTDTAARA DTAARA(QGPL/LSTSNDTIME) TYPE(*CHAR) LEN(8)

The following program, Check Last Send (CHKLSTSND), retrieves the value of the data area QGPL/LSTSNDTIME, determines if more than six hours have elapsed since the last send operation, and then takes appropriate action.

 

Pgm

Dcl Var(&Snd_Float) Type(*Char) Len(8)

Dcl Var(&Snd_Dec) Type(*Dec) Len(15 0)

Dcl Var(&Cur_Float) Type(*Char) Len(8)

Dcl Var(&Cur_Dec) Type(*Dec) Len(15 0)

Dcl Var(&Alert_Time) Type(*Dec) Value(21600)

Dcl Var(&Delay_Time) Type(*Dec)

Dcl Var(&Float_Dfn) Type(*Char) Len(7) +

Value(X'01000800000000')

Dcl Var(&Dec_Dfn) Type(*Char) Len(7) +

Value(X'03000F00000000')

Dcl Var(&CurLilDt) Type(*Int)

Dcl Var(&CurGregDt) Type(*Char) Len(23)

Loop: RtvDtaAra DtaAra(QGPL/LSTSNDTIME) RtnVar(&Snd_Float)

CallPrc Prc('_LBCPYNV') Parm((&Snd_Dec) (&Dec_Dfn) +

(&Snd_Float) (&Float_Dfn))

CallPrc Prc('CEELOCT') Parm((&CurLilDt) (&Cur_Float) +

(&CurGregDt) (*Omit))

CallPrc Prc('_LBCPYNV') Parm((&Cur_Dec) (&Dec_Dfn) +

(&Cur_Float) (&Float_Dfn))

If Cond((&Cur_Dec - &Snd_Dec) > &Alert_Time) +

Then(Do)

SndPgmMsg Msg('Time to send alert') +

ToPgmQ(*Ext)

ChgVar Var(&Delay_Time) Value(300)

EndDo

Else Cmd(ChgVar Var(&Delay_Time) +

Value(&Alert_Time - (&Cur_Dec - &Snd_Dec) +

+ 1))

DlyJob Dly(&Delay_Time)

GoTo CmdLbl(Loop)

EndPgm

 

Within CHKLSTSND, we first declare the variables we will be using. &Snd_Float is an 8-byte character field that will contain the floating point value stored by the SNDUPD program. Note that there is an assumption that the SNDUPD program has run at least one time prior to calling program CHKLSTSND. &Snd_Dec is defined as TYPE(*DEC) with 15 digits and no decimal positions. This declared size for &Snd_Dec is sufficient to hold a number of seconds since October 14, 1582, that exceeds the year 9999 (which should be sufficient for our needs) and will be used to hold the decimal equivalent of &Snd_Float. &Cur_Float is an 8-byte character field that will store the floating point value of the number of seconds since October 14, 1582, and the current local date and time. &Cur_Dec is the 15-digit decimal equivalent of &Cur_Float. &Alert_Time is a decimal value used to store the number of seconds in a duration of six hours (21 600 seconds), and &Delay_Time is a decimal value used to control how often the CHKLSTSND program runs.

 

&Float_Dfn is a 7-byte character field that defines the type of numeric value the Copy Numeric Value API is converting from. The initialized value x' 01000800000000' defines an 8-byte floating point value. &Dec_Dfn is a 7-byte character field that defines the type of numeric value the Copy Numeric Value API is to convert to. The initialized value x' 03000F00000000' defines a 15-digit packed decimal variable with no decimal positions. Details on these initialization values can be found here for those of you with inquiring minds.

 

The last two variables, &CurLilDt and &CurGregDt, are needed when calling the CEELOCT API but are not used in the current program.

 

When run, CHKLSTSND first retrieves the floating point value stored by SNDUPD in the data area QGPL/LSTSNDTIME. CHKLSTSND then uses the Copy Numeric Value API to convert this floating point value to a decimal value and stores the result in &Snd_Dec. The program calls the CEELOCT API to retrieve the current date and time in seconds (the variable &Cur_Float) and calls Copy Numeric Value to convert this floating point value to the decimal variable &Cur_Dec.

 

Having now obtained the time in seconds for both the last send function and the current time, CHKLSTSND compares the difference (&Cur_Dec - &Snd_Dec) to the maximum number of seconds that should elapse between send operations (the variable &Alert_Time). If more than &Alert_Time seconds have elapsed, CHKLSTSND sends an alert (shown for simplicity as a SNDPGMMSG) and then sets &Delay_Time to 300 seconds (or five minutes).

 

If the amount of elapsed time is less than &Alert_Time, the program calculates how many seconds of &Alert_Time remain after the last successful send operation and sets &Delay_Time to this value (plus one second).

 

The program then uses the DLYJOB command to delay &Delay_Time seconds and loops back to retrieving the floating point value stored by SNDUPD. If CHKLSTSND is in an alert status, the program will alert the operator every five minutes that something is wrong and continue with such alerts until SNDUPD successfully updates the data area QGPL/LSTSNDTIME. Otherwise, the program will sleep until an alertable situ

Bruce Vining

Bruce Vining is president and co-founder of Bruce Vining Services, LLC, a firm providing contract programming and consulting services to the System i community. He began his career in 1979 as an IBM Systems Engineer in St. Louis, Missouri, and then transferred to Rochester, Minnesota, in 1985, where he continues to reside. From 1992 until leaving IBM in 2007, Bruce was a member of the System Design Control Group responsible for OS/400 and i5/OS areas such as System APIs, Globalization, and Software Serviceability. He is also the designer of Control Language for Files (CLF).A frequent speaker and writer, Bruce can be reached at This email address is being protected from spambots. You need JavaScript enabled to view it.. 


MC Press books written by Bruce Vining available now on the MC Press Bookstore.

IBM System i APIs at Work IBM System i APIs at Work
Leverage the power of APIs with this definitive resource.
List Price $89.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • 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.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • 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

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • 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: