Get the time and date from a serverTag(s): Powerscript WinAPI/Registry


First we need these local function declarations
[local function declaration]
FUNCTION ULONG GetTimeOfDayInfo &
    ( REF s_timeofday dest, ULONG source, ULONG size ) & 
  LIBRARY "KERNEL32.DLL" ALIAS FOR "RtlMoveMemory"

FUNCTION ULONG NetRemoteTOD &
    (REF CHAR server[], REF ULONG bufferPtr) LIBRARY "NETAPI32.DLL"
FUNCTION ULONG LocalhostTOD &
    (ref long null, REF ULONG bufferPtr) LIBRARY "NETAPI32.DLL" &
  ALIAS FOR "NetRemoteTOD"
this structure
[s_timeofday structure]
elapsedt uLong
msecs uLong
hours uLong
mins uLong
secs uLong
hunds uLong
timezone Long
tinterval uLong
day uLong
month uLong
year uLong
weekday uLong
and these functions
[powerscript]

// ------------------------------------------------------------
of_StringToUnicode (String as_string, ref character ac_unicode[])

int li_loop, li_len, li_uni

li_len = Len( as_string )
FOR li_loop = 1 TO li_len
    li_uni++
    ac_unicode[ li_uni ] = Mid( as_string, li_loop, 1 )
    li_uni++
    ac_unicode[ li_uni ] = Char( 0 )
NEXT

li_uni++
ac_unicode[ li_uni ] = Char( 0 )
li_uni++
ac_unicode[ li_uni ] = Char( 0 ) 

// ------------------------------------------------------------
datetime of_relativetimedate(datetime adt_start, long al_offset)

// stolen from the PFC!
datetime ldt_null
date ld_sdate
time lt_stime
long ll_date_adjust
long ll_time_adjust, ll_time_test

//Initialize date and time portion
ld_sdate = date(adt_start)
lt_stime = time(adt_start)

//Find out how many days are contained
//Note: 86400 is # of seconds in a day
ll_date_adjust = al_offset /  86400
ll_time_adjust = mod(al_offset, 86400)

//Adjust date portion
ld_sdate = RelativeDate(ld_sdate, ll_date_adjust)

//Adjust time portion
//  Allow for time adjustments periods crossing over days
//  Check for time rolling forwards a day
IF ll_time_adjust > 0 THEN
    ll_time_test = SecondsAfter(lt_stime,time('23:59:59'))
    IF ll_time_test < ll_time_adjust THEN
        ld_sdate = RelativeDate(ld_sdate,1)
        ll_time_adjust = ll_time_adjust - ll_time_test -1
        lt_stime = time('00:00:00')
    END IF
    lt_stime = RelativeTime(lt_stime, ll_time_adjust)
//Check for time rolling backwards a day
ELSEIF  ll_time_adjust < 0 THEN
    ll_time_test = SecondsAfter(lt_stime,time('00:00:00'))
    IF ll_time_test > ll_time_adjust THEN
        ld_sdate = RelativeDate(ld_sdate,-1)
        ll_time_adjust = ll_time_adjust - ll_time_test +1
        lt_stime = time('23:59:59')
    END IF
    lt_stime = RelativeTime(lt_stime, ll_time_adjust)
END IF

RETURN (datetime(ld_sdate,lt_stime))
then the actual call to get the time and date from a server (or the localhost), we need to adjust the value returned because of the timezone offset.
[powerscript]

char lc_logonserver[]
string ls_logonserver
boolean lb_logonserver

ulong  ul_prtTimeOfDayInfo
ulong  ul_rc
long ll_null

s_timeofday lstr_timedate
ContextKeyword lcxk_base
String ls_result
string ls_values[]

datetime ldt_locale
date ld_server
time lt_server
datetime ldt_server

// get the logon server
this.GetContextService("Keyword", lcxk_base) 
lcxk_base.GetContextKeywords("LOGONSERVER", ls_values) 
IF Upperbound(ls_values) > 0 THEN
   ls_logonserver = ls_values[1]
   // transform logon server to unicode 
   of_StringToUnicode(ls_logonserver, lc_logonserver)
   lb_logonserver = true
ELSE
   // lc_logonserver is null  -->  no server -> localhost
   lb_logonserver = false
END IF

// get the current time of day
IF lb_logonserver THEN
   ul_rc = NetRemoteTOD(lc_logonserver, ul_prtTimeOfDayInfo)
ELSE
   ll_null = 0
   ul_rc = LocalhostTOD(ll_null, ul_prtTimeOfDayInfo)
END IF
//

IF NOT ul_rc = 0 THEN
   MessageBox("Oups", String (ul_rc))
ELSE
   // convert to our structure
   GetTimeOfDayInfo(lstr_timedate, ul_prtTimeOfDayInfo, 48)
   ls_result = string(lstr_timedate.year) + "-" &
       + string(lstr_timedate.month) &
       + "-" + string(lstr_timedate.day) &
       + " " + string(lstr_timedate.hours) + ":" &
       + string(lstr_timedate.mins) &
       + ":" + string(lstr_timedate.secs)
   MessageBox("result before conversion" , ls_result )

   // convert to local time taking into account the timezone
   ld_server = Date (string(lstr_timedate.year) + "-" &
       + string(lstr_timedate.month) + &
       "-" + string(lstr_timedate.day))
   lt_server = Time (string(lstr_timedate.hours) + ":" &
       + string(lstr_timedate.mins) + &
       ":" + string(lstr_timedate.secs))
    
   ldt_locale = of_relativeDatetime &
        (datetime(ld_server, lt_server), &
        - (lstr_timedate.timezone * 60))
   Messagebox("result after conversion" , &
       String(ldt_locale, "yyyy-mm-dd hh:mm"))
END IF

See also this HowTo


blog comments powered by Disqus