Quantcast
Channel: Visual COBOL - Forum - Recent Threads
Viewing all articles
Browse latest Browse all 4356

RE: Get the date and time the local server

$
0
0

This is a native Windows API function and not a .NET Framework class so when it is being called from managed code P/Invoke.is being used under the covers. The warning you are getting is normal in this situation. Because this is a native WINAPI call I would recommend that you call it from a native COBOL program and then call that program from your managed code in order to get the returned time.

The reason the returned hours may be different than your local time is that the returned value is the UTC (Universal Coordinated Time) of the server, and must be adjusted by TOD_TIMEZONE minutes in order to get the correct local time.

This can be done by converting the time into a DateTime object and then using the ToLocalTime method.

So the managed code project would look something like the following (add a reference to the native .dll)

       identification division.
       program-id. program1.
       data division.
       working-storage section.
       01 UncServerName pic x(100) value z"myserver".
       01 net-api-status  pic x(4) comp-5 value 0.
       01 Buffer.
          05 tod_elapsedt pic x(4) comp-5.
          05 tod_msecs    pic x(4) comp-5.
          05 tod_hours    pic x(4) comp-5.
          05 tod_mins     pic x(4) comp-5.
          05 tod_secs     pic x(4) comp-5.
          05 tod_hunds    pic x(4) comp-5.
          05 tod_timezone pic s9(9) comp-5.
          05 tod_tinterval pic x(4) comp-5.
          05 tod_day      pic x(4) comp-5.
          05 tod_month    pic x(4) comp-5.
          05 tod_year     pic x(4) comp-5.
          05 tod_weekday  pic x(4) comp-5.
       01 localtime type DateTime.
       procedure division.

           call "getnetremotetod" 
              using uncservername
                    buffer
                    net-api-status
           end-call
           if net-api-status not = 0
              display "error = " net-api-status
              goback
           end-if
           
           set localtime to 
              new DateTime(tod_year, tod_month, tod_day, tod_hours, tod_mins, tod_secs, type DateTimeKind::Utc)::ToLocalTime
           
           display localtime
           goback.


and the native code program would look like:

      $set case 
       identification division.
       program-id. getnetremotetod.
       environment division.
       configuration section.
       special-names.
          call-convention 66 is winapi.
       data division.
       working-storage section.
       01 pp procedure-pointer.
       01 pBuffer       pointer.
       linkage section.
       01 UncServerName pic x(100).
       01 passedBuffer.
          05 pb_elapsedt pic x(4) comp-5.
          05 pb_msecs    pic x(4) comp-5.
          05 pb_hours    pic x(4) comp-5.
          05 pb_mins     pic x(4) comp-5.
          05 pb_secs     pic x(4) comp-5.
          05 pb_hunds    pic x(4) comp-5.
          05 pb_timezone pic s9(9) comp-5.
          05 pb_tinterval pic x(4) comp-5.
          05 pb_day      pic x(4) comp-5.
          05 pb_month    pic x(4) comp-5.
          05 pb_year     pic x(4) comp-5.
          05 pb_weekday  pic x(4) comp-5.
       01 net-api-status  pic x(4) comp-5.
       
       01 Buffer.
          05 tod_elapsedt pic x(4) comp-5.
          05 tod_msecs    pic x(4) comp-5.
          05 tod_hours    pic x(4) comp-5.
          05 tod_mins     pic x(4) comp-5.
          05 tod_secs     pic x(4) comp-5.
          05 tod_hunds    pic x(4) comp-5.
          05 tod_timezone pic s9(9) comp-5.
          05 tod_tinterval pic x(4) comp-5.
          05 tod_day      pic x(4) comp-5.
          05 tod_month    pic x(4) comp-5.
          05 tod_year     pic x(4) comp-5.
          05 tod_weekday  pic x(4) comp-5.
       
       procedure division using uncservername passedbuffer 
          net-api-status.
           
           set pp to entry "netapi32"
           call winapi "NetRemoteTOD"
              using function national-of(uncservername)
                    pBuffer
              returning net-api-status
           end-call
           if net-api-status not = 0
              goback
           end-if
           
           set address of buffer to pBuffer
           move buffer to passedbuffer
           if pBuffer not = null
              call winapi "NetApiBufferFree"
                 using pBuffer
              end-call
           end-if
           goback.


Viewing all articles
Browse latest Browse all 4356

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>