Monday, May 9, 2016

SilkPerformer: Get Current Date/Time in GMT

SilkPerformer gives a function to retrieve a formatted time string in the local time but not in GMT. Here's a little function that will fetch the current GMT time in the specified format. Of course, if there are better ways to do it, please feel free to share.
  1.   function ESGGetGMTDateTime(  
  2.       psFormat : string optional  
  3.   ) : string  
  4.   var  
  5.     sDateTime : string;  
  6.     sFormat   : string init "%Y-%m-%dT%H:%M:%SZ";  
  7.     nDateCurr : number;  
  8.     nTimeCurr : number;  
  9.   begin  
  10.     if Strlen(psFormat) > 0 then sFormat := psFormat end;  
  11.     nDateCurr := GetCurrentDate();  
  12.     nTimeCurr := GetCurrentTime();   
  13.     nTimeCurr := nTimeCurr + (GetTime(TIME_GMT) - GetTime(TIME_LOCAL));  
  14.     if nTimeCurr >= 86400 then  
  15.       nDateCurr := IncDate(nDateCurr, 1);  
  16.       nTimeCurr := nTimeCurr - 86400;  
  17.     elseif nTimeCurr < 0 then  
  18.       nDateCurr := IncDate(nDateCurr, -1);  
  19.       nTimeCurr := 86400 + nTimeCurr;  
  20.     end;  
  21.     FormatDateTime(nDateCurr, nTimeCurr, sFormat, sDateTime);  
  22.     ESGGetGMTDateTime := sDateTime;  
  23.   end ESGGetGMTDateTime;  

No comments:

Post a Comment