Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GMT with PC set to BST

Status
Not open for further replies.

David Higgs

Programmer
May 6, 2012
390
GB
My application requires "Time" to be recorded in GMT throughout the Year i.e ignore BST offset.
Currently my application takes the "Time" from the PC which is now in BST. I would like to automatically convert the "Time" in BST to GMT without having to remember if BST is or isn't in effect. Is this possible?

Regards,
David
 
You don't have to "remember" if BST is in effect. You can easily determine it programmatically.

BST is in effect if the current date is on or after the last Sunday in April AND before the last Sunday in October. VFP provides plenty of date-related functions to enable you to determine that.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Even simpler: GetSystemTime always gives you the unbiased GMT time aka UTC.

Code:
? SystemTime()

FUNCTION  SystemTime()
    LOCAL lcBuffer, lnY, lnM, lnD, lnHH, lnMM, lnSS, lcDate, lcTime, ltSystemTime
    lcBuffer = SPACE(16)
    GetSystemTime(@lcBuffer) 
    
    lnY  = CTOBIN(SUBSTR(lcBuffer,  1, 2),"2RS")
    lnM  = CTOBIN(SUBSTR(lcBuffer,  3, 2),"2RS")
    lnD  = CTOBIN(SUBSTR(lcBuffer,  7, 2),"2RS")
    
    lnHH = CTOBIN(SUBSTR(lcBuffer,  9, 2),"2RS")
    lnMM = CTOBIN(SUBSTR(lcBuffer, 11, 2),"2RS")
    lnSS = CTOBIN(SUBSTR(lcBuffer, 13, 2),"2RS")
    
    ltSystemTime = DATETIME(lnY, lnM, lnD, lnHH, lnMM, lnSS)
    RETURN ltSystemTime
EndFunc

Function GetSystemTime()
   Lparameters lcBuffer
   Declare GetSystemTime In kernel32 String @lpSystemTime
   Return GetSystemTime(@lcBuffer)
EndFunc

Don't think too much about why GetSystemTime calls itself, here. Once the declaration is through, the VFP user defined function is not called, the API function has precedence. If any code CLEARS DLLs the API declaration is redone. This code is therefore error prone and stable and does not do the declare with every call.

You may put this inside your main.prg at the end (aside of the [tt]? SystemTime()[/tt] line, which only is there to demo the call) and then may call SystemTime() at any place in code you now may do a DateTime() call. As long as your software runs it'll also be usable as default value of a tablefield, though I would then also put this code into DBC stored procedures to make double sure the declaration is found.

Bye, Olaf.
 
Mike, Thank you for your input, much appreciated.

Olaf, Thank you for the code, which as expected, does exactly what I'm looking for.

Regards,
David
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top