Get the date and time the local server

I need to get the date and time of the local server and write a file.
It can not be the workstation because the user can change them.
Is there some function or class that execute this process?

VC 2.2 .NET

WINSERVER 2003

  • You can use the NetRemoteTOD WINAPI function to do this.

    The docs can be found here:

    A COBOL example of this would be:

           identification division.
           program-id. Program1.
    
           environment division.
           configuration section.
           special-names.
              call-convention 66 is winapi.
           data division.
           working-storage section.
           01 pp procedure-pointer.
           01 UncServerName pic x(100)
              value z"myservername".
           01 pBuffer       pointer.
           01 net-api-status  pic x(4) comp-5 value 0.
           01 format-date-time.
              05 format-year  pic 9(4).
              05              pic x value "/".
              05 format-month pic 9(2).
              05              pic x value "/".
              05 format-day   pic 9(2).
              05              pic x value space.
              05 format-hours pic 9(2).
              05              pic x value ":".
              05 format-minutes pic 9(2).
              05              pic x value ":".
              05 format-seconds pic 9(2).
              
           linkage section.
           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.
    
               
               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
                  display "error = " net-api-status
                  goback
               end-if
               
               set address of buffer to pBuffer
               
               move tod_year to format-year
               move tod_month to format-month
               move tod_day to format-day
               move tod_hours to format-hours
               move tod_mins to format-minutes
               move tod_secs to format-seconds
               display format-date-time
               
               if pBuffer not = null
                  call winapi "NetApiBufferFree"
                     using pBuffer
                  end-call
               end-if
               goback.
    
           end program Program1.
    
  • Verified Answer

    Hi Chris,

    The your example worked in Net Express but two hours forward already in Visual Cobol 2.2 .NET show the following error (attached). It would be the missing of a namespace?

  • Verified Answer

    Hi Chris,

    The your example worked in Net Express but two hours forward already in Visual Cobol 2.2 .NET show the following error (attached). It would be the missing of a namespace?

  • Verified Answer

    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.