× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Hi Burkhard,

we have a Database with a Unix Time Field and I want to use
the Unix Function gmtime() I coded the following RPGLE.
But with these Parameter I get a wrong result in MYTime Structure.

There are two bugs in your program that are causing it to fail. But I'll explain those in a moment.

But, I wonder why you want to use gmtime()? You're programming in RPG, not C!! Why not take advantage of RPG's date and time functions? It's very easy to convert a Unix time field to an RPG timestamp field. Just do this:

     D EPOCH           C                   z'1970-01-01-00.00.00.000000'
     D myTS            s               Z
          .
          .
          Unixtime = 1173954308 ;
          myTS = EPOCH + %seconds(UnixTime);

Now that you've done that, you have a timestamp field that you can use with all of RPG's time operations -- and that makes it very easy to manipulate. (You can also retrieve your system's UTC offset if you want to, and use that to convert it to local time.)

But, if for some reason you really want to make it work with the gmtime() function, that's certainly doable.

There are 2 big problems with your code:

1) Your prototype for gmtime() is wrong.

2) You're trying to assign a memory address to a data structure,
    rather than to the pointer that the structure is based on.
    (Bob Cozzi already pointed this second bug out.)

Here's what you have for a prototype:

D gmtime          Pr              *   ExtProc('gmtime')
D  t_time                       10I 0 Value

However, if you look at the C prototype (it's in QSYSINC/H,TIME) it looks like this:
   struct tm *gmtime    ( const time_t * );

Can you see the difference? The RPG code passes the parameter by VALUE -- but the C code passes it by read-only reference. Since the API is expecting to receive it by reference, it gets very confused when you pass it by VALUE!! To fix this bug, change your prototype to look like this:

  D gmtime          Pr              *   ExtProc('gmtime')
  D  t_time                       10I 0 const

See? Now the parameter is passed by "const reference" (i.e. read-only reference).

The other problem, as Bob already pointed out, is that you're assigning the address to the data structure name instead of the pointer that it's based on. The way you have it coded, it shouldn't even compile, which makes me think that this was a mistake you made when you posted your code, rather than a mistake that's actually on your system.

Anyway, here's what you have:

      Unixtime = 1173954308 ;
      mytime = gmtime(Unixtime) ;

Here's what it should be:

       Unixtime = 1173954308 ;
       tm = gmtime(Unixtime) ;

After making these two changes, your program should work fine. (It does on my system!) But, again... I think using RPG's date/time support (as I demonstrated earlier) is much simpler, provides a result that's easier to work with, and will be more maintainable down the road.

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.