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.