Get Time to the Millisecond in ILE C
Date Posted: July 10, 2008 12:00 AM

Q: I have an ILE C program that needs to build an RPG-style timestamp value and pass it as a parameter to an RPG program. I want my timestamp to contain milliseconds like the RPG ones do. I've been using the traditional time() and localtime() APIs, but they don't provide the milliseconds. What's a poor C programmer to do?

A: The ILE environment has a set of APIs for manipulating dates and times. This set is versatile, and I like it much better than the traditional C APIs, such as time() and localtime().

Here's an example:

#include <stdio.h>
#include <string.h>
#include <leawi.h>

int main(int argc, char **argv) {

    int junk;
    double secs;
    char junk2[24];
    char result[27];

    memset(result, 0, sizeof(result));
    CEELOCT( &junk, &secs, junk2, NULL );
    CEEDATM( &secs, "YYYY-MM-DD-HH.MI.SS.999", result, NULL);
    strcat(result, "000");
    printf("%s\n", result);

    return 0;
}

In the preceding example, the CEELOCT API provides the current time (in the local time zone). The CEEDATM API formats that time into a timestamp, like an RPG program expects. The CEEDATM API provides values only down to the millisecond level, so I used strcat to add zeros for the microseconds.

Note that the CEEDATM API can format a date, time, or timestamp in just about any conceivable format, making these APIs very versatile. You can learn more about the ILE date and time APIs at the following link:
http://publib.boulder.ibm.com/infocenter/iseries/v5r4/topic/apis/ile4a1TOC.htm

One disadvantage of the ILE date and time APIs is that they work only in the ILE environment. In other words, they're not portable--but if you're calling an RPG program, you probably don't need your C program to work on multiple plaforms.

If you do need the portability, however, try the gettimeofday() API. It's harder to use and less versatile than the CEELOCT and CEEDATM APIs, but it's much more portable.


Want to use this article? Click here for options!
Want to subscribe? Click here!
There are no comments to display. Be the first to add your thoughts!
You must log on before posting a comment.

Are you a new visitor? Register Here
 

around the forums

PASE - HTMLDOC (Scott's binary version) Error: please Help!
Forum Name: RPG
16 May 2012 01:58 PM | Replies: 3
IFS directory structure
Forum Name: Systems Management
16 May 2012 11:52 AM | Replies: 2
IFS folder/file authority
Forum Name: Communications/Networking
16 May 2012 08:45 AM | Replies: 6

ProVIP Sponsors

BCD

Join Our Community!

Subscribe today to iPro Developer! iPro Developer is packed with technical know-how for developers of IBM i, iSeries, AS400 and System i. Sign up now to get your full subscriber benefits including:

  • Code available for download
  • Full access to the online article archive (including all System iNEWS ProVIP content)
  • Downloadable ebook with past 6 months of articles
  • Discounts on eLearning classes, self-paced training, in-person events, and more!
iPro Developer Newsletters
  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips