Absent Member.
Absent Member.

[archive] datetime data to cobol format

[Migrated content. Thread originally posted on 29 March 2007]

Hello everyone,
I've a
03 st-mtime pic x(4) comp-x.
containing the modify time of a file in datetime (longint) format (it's a part of the output of "_stat" WinAPI function): I'd like to convert it into a cobol freindly
01 f-datetime.
02 f-date pic 9(08).
02 f-time pic 9(08).
format. Has anyone an helpful suggestion?
Thanks In Advance, Giovanni.
9 Replies
Absent Member.
Absent Member.

RE: [archive] datetime data to cobol format

st-mtime is the time in seconds since a given date. It is of course possible to translate this into date & time, but... Since you already are using _stat to get this value, why not use another C function to translate it into human reading? You can for instance use the ctime() function, which will give you a string. But to get all the little bits in one, use the gmtime() function.
All of these are C runtime library functions. Not API functions.

If you want to use the API, the function you should use is GetFileTime(), to use this you will also have to use OpenFile() to get a handle and CloseFile(), but this should not be too much trouble.

Finally, you will use FileTimeToSystemTime() to translate into a human readable format. Heck! When I read this, I realize you are probably better off with the stat() and gmtime() functions. Less work same result. Good hunt!
Absent Member.
Absent Member.

RE: [archive] datetime data to cobol format

Hi Gisle,
I decided to follow the gmtime() "direction", 'cos using the _stat() I've already loaded the msvcrt.dll that exports gmtime() too. Now I've this problem:
I use:
call "@[DISPLAY]:gmtime" using
by reference st-mtime
giving w-gmtime-pointer.
it works fine returning in w-gmtime-pointer the pointer to a tc structure: but now how can I acces that struct?
I cannot use a line like
set address of w-my-cobol-tc to w-gmtime-pointer
because Acu syntax allows it only if w-my-cobol-tc is a linkage item.
Bye Giovanni.
Absent Member.
Absent Member.

RE: [archive] datetime data to cobol format

I need to see the whole code to state exactly what is wrong. Can you please post the code for calling stat and gmtime (as small as possible), this way we use this as a educational case for all that want to use DLLs in a Thin Client scenario.
Note though, not all versions of ACUCOBOL-GT supports accessing external data on the Thin Client, so it might be that you have to upgrade to do this.
Absent Member.
Absent Member.

RE: [archive] datetime data to cobol format

we currently deploy for Acu 7.0. Here's an abstract of the code:
01 util-finfo.
04 uf-file-info.
05 uf-file-size pic x(08) comp-x.
05 uf-file-date pic 9(08) comp-x.
05 uf-file-time pic 9(08) comp-x.

01 stat.
02 st-dev pic x(4) comp-x.
02 st-ino pic x(2) comp-x.
02 st-mode pic x(2) comp-x.
02 st-nlink pic x(2) comp-x.
02 st-uid pic x(2) comp-x.
02 st-gid pic x(2) comp-x.
02 filler pic x(2) comp-x.
02 st-rdev pic x(4) comp-x.
02 st-size pic x(4) comp-x.
02 st-atime pic x(4) comp-x.
02 st-mtime pic x(4) comp-x.
02 st-ctime pic x(4) comp-x.
77 return-value signed-int.
01 actual-file-name pic x(260).
01 w-gmtime-handle usage handle.
01 w-gmtime-r.
02 w-tm-sec pic 9(02).
02 w-tm-min pic 9(02).
02 w-tm-hour pic 9(02).
02 w-tm-mday pic 9(02).
02 w-tm-mon pic 9(02).
02 w-tm-year pic 9(02).
02 w-tm-wday pic 9(02).
02 w-tm-yday pic 9(02).
02 w-tm-isdst pic 9(02).

call "msvcrt.dll@__cdecl"
on exception
perform my_error_messages
exit paragraph

call "@[DISPLAY]:_stat" using actual-file-name, stat
giving return-value
if return-value is not equal to 0 then
exit paragraph
* Swap the bytes to get file size in bytes
move st-size(1:1) to uf-file-size(8:1)
move st-size(2:1) to uf-file-size(7:1)
move st-size(3:1) to uf-file-size(6:1)
move st-size(4:1) to uf-file-size(5:1)

call "@[DISPLAY]:gmtime" using
by reference st-mtime
giving w-gmtime-handle.
* Now I've to fill the w-gmtime-r struct whith the data pointed
* by w-gmtime-handle, and the flow use w-gmtime-r to fill the
* uf-file-date and uf-file-time datas
Absent Member.
Absent Member.

RE: [archive] datetime data to cobol format

Unfortunately, what you try to accomplish is not doable with the TC as the function gmtime is returning a pointer. If this should work, the phrase WITH MEMORY SIZE had to be applicable for the GIVING phrase as well, which it is not. You may however extend an enhancement request for this.
Here is code that works locally though, the reason you had to swap was because you used COMP-X, not COMP-N. There is something wrong with the stat structure, as you found yourself, it is required to add 2 bytes in there, but they are not documented anywhere...

       IDENTIFICATION               DIVISION.
       PROGRAM-ID.                  TEMPLATE.
       ENVIRONMENT                  DIVISION.
       CONFIGURATION                SECTION.

       01 stat-rec.
          03 st-dev                 PIC X(4) COMP-N.
          03 st-ino                 PIC X(2) COMP-N.
          03 st-mode                PIC X(2) COMP-N.
          03 st-nlink               PIC X(2) COMP-N.
          03 st-uid                 PIC X(2) COMP-N.
          03 st-gid                 PIC X(4) COMP-N.
          03 st-rdev                PIC X(4) COMP-N.
          03 st-size                PIC X(4) COMP-N.
          03 st-atime               PIC X(4) COMP-N.
          03 st-mtime               PIC X(4) COMP-N.
          03 st-ctime               PIC X(4) COMP-N.

       77 WS-Display                PIC Z(3)BZ(3)BZ(2)9.

       01 ACTUAL-FILE-NAME          PIC X(260).
       01 TimeRec.
          03 tm-sec                 PIC X(4) COMP-N.
          03 tm-min                 PIC X(4) COMP-N.
          03 tm-hour                PIC X(4) COMP-N.
          03 tm-mday                PIC X(4) COMP-N.
          03 tm-mon                 PIC X(4) COMP-N.
          03 tm-year                PIC X(4) COMP-N.
          03 tm-wday                PIC X(4) COMP-N.
          03 tm-yday                PIC X(4) COMP-N.
          03 tm-isdst               PIC X(4) COMP-N.
       01 Ptr                       PIC X(4) COMP-N.
       01 AmountToCopy              UNSIGNED-INT.

       77 w-tm-sec                  pic 99.
       77 w-tm-min                  pic 99.
       77 w-tm-hour                 pic 99.
       77 w-tm-mday                 pic 99.
       77 w-tm-mon                  pic 99.
       77 w-tm-year                 pic 99.
       77 w-tm-wday                 pic 99.
       77 w-tm-yday                 pic 99.

           STRING  "tmstat.acu"     DELIMITED BY SIZE
                   LOW-VALUES       DELIMITED BY SIZE
                   INTO             ACTUAL-FILE-NAME.
           CALL    "MSVCRT.DLL"
           CALL    "_stat"          USING
                   BY REFERENCE     Actual-File-Name
                   BY REFERENCE     Stat-rec.
           MOVE    ST-Size          TO WS-Display.
           DISPLAY "File size: "    WS-Display " Bytes".
           CALL    "gmtime"         USING
                   BY REFERENCE     ST-mtime
                   GIVING           Ptr.
           SET     AmountToCopy     TO SIZE OF TimeRec.
           CALL    "C$MEMCPY"       USING
                   BY REFERENCE     TimeRec
                   BY VALUE         Ptr
                   BY VALUE         AmountToCopy.
           MOVE    tm-sec           TO W-tm-sec.
           MOVE    tm-min           TO W-tm-min.
           MOVE    tm-hour          TO W-tm-hour.
           MOVE    tm-mday          TO W-tm-mday.
           MOVE    tm-mon           TO W-tm-mon.
           MOVE    tm-year          TO W-tm-year.
           MOVE    tm-wday          TO W-tm-wday.
           MOVE    tm-yday          TO W-tm-yday.
           ADD     1                TO w-tm-mon.
           DISPLAY "The file was last modified on: "
                   w-tm-mon "/" w-tm-mday "/20" w-tm-year " "
                   w-tm-hour ":" w-tm-min ":" w-tm-sec.

           IF      tm-isdst         NOT = 0
                   DISPLAY "It is even daylight savings now"
                   DISPLAY "Sorry pal, no daylight savings".

           DISPLAY "Apart of this, the day is:"
           DISPLAY "The " w-tm-yday " since January 1st".
           DISPLAY "and the " w-tm-wday " day of week".
           ACCEPT  OMITTED.
           STOP    RUN.
Absent Member.
Absent Member.

RE: [archive] datetime data to cobol format

Hi Gisle,
Ok next week I'll try to suggest our Italian Acu representatives to explore the enhancement request.
I made a couple of tests, on your solution:
it work for the size matter (no more swapping is needed), but it seemes useless for the tm structure. Debugging it (on a WinXpPro machine with a couple of months of Hardware and OS life) makeing "_stat" on a local file I found st-mtime valued, "gmtime" returns me a pointer, C$MEMCOPY seemes working but the values in tm structure are nonsense. I tried using ctime instead of gmtime and the memcopying pointer to a pic x(50) ('cos ctime returns a string pointer) but again nonsense values. Any more suggestion?
How can I investigate more deeply if gmtime works out?
Once more Thanks for support!
Absent Member.
Absent Member.

RE: [archive] datetime data to cobol format

If you compile the provided example, with no compiler switches (check your environment too) and execute it with no config variables, are you saying that it does not work? It works like a champ for me. If this is the case, what version are you executing with?
Or is it your own version that does not work? In which case you should look step by step for differences between my sample and yours.
Absent Member.
Absent Member.

RE: [archive] datetime data to cobol format

Hi Gisle,
maybe I need some holidays: your test prog works fine but in Wrun32 environment.
I was trying it in a TC env, and in that context it won't work: 'cos, I suppose, C$MEMCOPY copies memory buffers on server side, not on client one. So I derived this solution using again msvcrt, and I using "memmove" on client side to copy buffer from client to server data item.
On TC env works fine even if with some specs (see inline comments).

I declared:

01 stat-rec.
03 st-dev pic x(4) comp-n.
03 st-ino pic x(2) comp-n.
03 st-mode pic x(2) comp-n.
03 st-nlink pic x(2) comp-n.
03 st-uid pic x(2) comp-n.
03 st-gid pic x(4) comp-n.
03 st-rdev pic x(4) comp-n.
03 st-size pic x(4) comp-n.
03 st-atime pic x(4) comp-n.
03 st-mtime pic x(4) comp-n.
03 st-ctime pic x(4) comp-n.

01 w-lctime-buf-pointer pic x(04) comp-n.
01 w-lctime-buf-size pic x(04) comp-n.
01 w-lctime-rec.
02 w-lc-sec pic x(04) comp-n.
02 w-lc-min pic x(04) comp-n.
02 w-lc-hour pic x(04) comp-n.
02 w-lc-mday pic x(04) comp-n.
02 w-lc-mon pic x(04) comp-n.
02 w-lc-year pic x(04) comp-n.
02 w-lc-wday pic x(04) comp-n.
02 w-lc-yday pic x(04) comp-n.
* 02 w-lc-isdst pic x(04) comp-n.
01 w-my-date pic 9(08).
01 w-my-date-r redefines w-my-date.
02 w-my-year pic 9(04).
02 w-my-year-r redefines w-my-year.
03 w-my-year-sec pic 9(02).
03 w-my-year-dec pic 9(02).
02 w-my-mon pic 9(02).
02 w-my-day pic 9(02).
01 w-my-time pic 9(08).
01 w-my-time-r redefines w-my-time.
02 w-my-hour pic 9(02).
02 w-my-min pic 9(02).
02 w-my-sec pic 9(02).
02 w-my-hsec pic 9(02).
* ... than ... here's proc to subst gmtime and memcopy calls:
call "@[DISPLAY]:localtime" using
by reference st-mtime
giving w-lctime-buf-pointer.

initialize w-lctime-rec
set w-lctime-buf-size to size of w-lctime-rec
call "@[DISPLAY]:memmove" using
by reference w-lctime-rec
by value w-lctime-buf-pointer
by value w-lctime-buf-size
* 1st spec:
* according to MSDN year would must be from 1970
* but I noticed that w-lc-year returned is + 70
move w-lc-year to w-my-year
add 1900 to w-my-year
* 2nd spec:
* returned month is -1 (starts from 0)
move w-lc-mon to w-my-mon
add 1 to w-my-mon
move w-lc-mday to w-my-day
move w-lc-sec to w-my-sec
move w-lc-min to w-my-min
move w-lc-hour to w-my-hour
move 0 to w-my-hsec

What do you think about it?
bye Giovanni.
Absent Member.
Absent Member.

RE: [archive] datetime data to cobol format

Yes it does not work oin TC environment, because the function is returned as a pointer the WITH SIZE frase is not available.

My compliments to the solution of using memmove, brilliant!
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.