FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Reading UTC (GMT) time offset from registry
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Reading UTC (GMT) time offset from registry
Posted: Sat Dec 06, 2008 07:19 PM
I am trying to read the time offset from the registry. It is in DWord format. How can I convert this to decimal?

My test code is below.

Regards,
James


#include "fivewin.ch"
#define  HKEY_LOCAL_MACHINE  2147483650  // 0x80000002

// This is the value we want
//"HKEY_LOCAL_MACHINE\System\CurrentControlSet\" &_
//        "Control\TimeZoneInformation\ActiveTimeBias"

function main()

  msgInfo( len( getUTCTimeOffset() ) )

return nil


// Purpose: To read the UTC (GMT) time offset from the registry
function getUTCTimeOffset()

   local nHandle, nValue

   if RegOpenKey( HKEY_LOCAL_MACHINE,;
      "System\CurrentControlSet\Control\TimeZoneInformation",;
      @nHandle ) == 0

      RegQueryValue( nHandle, "ActiveTimeBias", @nValue )

      RegCloseKey( nHandle )

   endif

return nValue
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Reading UTC (GMT) time offset from registry
Posted: Sat Dec 06, 2008 08:02 PM
#include "Fivewin.ch"


#define HKEY_LOCAL_MACHINE 2147483650

#define REG_DWORD 4

#define KEY_ALL_ACCESS 983103


FUNCTION MAIN()

    LOCAL hKey := 0

    LOCAL nType := REG_DWORD

    LOCAL cData := SPACE( 256 )

    LOCAL nSize := LEN( cData )

    LOCAL nData

    REGOPENKEY( HKEY_LOCAL_MACHINE, "System\CurrentControlSet\Control\TimeZoneInformation", 0, KEY_ALL_ACCESS, @hKey )

    REGQUERYVALUE( hKey, "ActiveTimeBias", 0, @nType, @cData, @nSize )

    REGCLOSEKEY( hKey )

    nData = BIN2L( cData )

    ? nData

    RETURN NIL


DLL32 FUNCTION REGOPENKEY( hKey AS LONG, cSubKey AS LPSTR, nOptions AS DWORD, nSamDesired AS DWORD, @nHandle AS PTR ) AS LONG;
      PASCAL FROM "RegOpenKeyExA" LIB "advapi32.dll"

DLL32 FUNCTION REGQUERYVALUE( hKey AS LONG, cValueName AS LPSTR, nReserved AS LONG, @nType AS PTR, @cData AS LPSTR, @nSize AS PTR ) AS LONG;
      PASCAL FROM "RegQueryValueExA" LIB "advapi32.dll"

DLL32 FUNCTION REGCLOSEKEY( hKey AS LONG ) AS LONG;
      PASCAL FROM "RegCloseKey" LIB "advapi32.dll"


EMG
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Reading UTC (GMT) time offset from registry
Posted: Sat Dec 06, 2008 08:27 PM

Enrico,

Thanks, but your code does not seem to be working for me. If I look at the value using REGEDIT I get 480 which is in seconds and translates to 8 hours which is correct for my location. Your function is returning 538976288.

I had already tried BIN2L() with my function which didn't work either.

Any ideas?

James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Reading UTC (GMT) time offset from registry
Posted: Sat Dec 06, 2008 08:33 PM

Enrico,

OK, I found the problem, and yours is working now. I had left out your DLL32's since those functions seem to already be FWH functions, but I noticed yours have different parameters. When I added your DLL32's it works fine. I wonder if putting yours in is going to break other FWH code that may be calling those DLL functions?

James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Reading UTC (GMT) time offset from registry
Posted: Sat Dec 06, 2008 09:32 PM
James Bott wrote:Of course, this assumes that Enrico will allow his functions to be used.


There's no problem at all for me.

EMG
Posts: 682
Joined: Tue Feb 14, 2006 09:48 AM
Reading UTC (GMT) time offset from registry
Posted: Tue Dec 09, 2008 09:53 AM
Hi James,
This is how I retriev the GMT from the registry.
//------------------
FUNCTION TimeZone()
   Local oReg, nRetVal
   oReg := TReg32():New(HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Control\TimeZoneInformation", .f. )
   nRetVal := oReg:Get( "ActiveTimeBias", 0 )
   oReg:Close()
   nRetVal := Round( nRetVal / 60, 0 )
Return nRetVal
//--------------
FUNCTION Gmt()
   LOCAL cGmt:=Time()
   cGmt:=Str(Val(SubStr(cGmt,1,2))+TimeZone(),2)+SubStr(cGmt,3)
RETURN cGmt
Saludos desde Mallorca
Biel Maimó
http://bielsys.blogspot.com/
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Reading UTC (GMT) time offset from registry
Posted: Tue Dec 09, 2008 03:46 PM

Biel,

Thanks for the sample.

In my case I need the offset from GMT to the local time. I might be able calculate this from the GMT and time() but the difficulty is that the current offset changes depending on our (US) Daylight Savings Time. For those who don't know what this is, we change our time by one hour twice a year and to complicate things we changed when we do this last year.

So, our computers do this change automatically, and the new current offset is stored in the registry.

I need the offset for use with TSMTP. It needs to have the offset, but now that I think about it, the class may be using this just to find GMT from the local time--I will have to check this out.

Regardless, I do think we need to provide access to the other parameters that the FW registry functions do not now have, so we need a solution for this also.

Regards,
James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Reading UTC (GMT) time offset from registry
Posted: Tue Dec 09, 2008 04:30 PM

Biel,

Oops! I didn't actually look at your code closely--I just read your comment saying it was how you find GMT. I see you have code to find the offset too--and it works. Thanks much!

Regards,
James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 682
Joined: Tue Feb 14, 2006 09:48 AM
Reading UTC (GMT) time offset from registry
Posted: Tue Dec 09, 2008 04:38 PM

We were typing reply at the same time.
Well, yo have seen the function now. Happy to know is running well.

Best Regards

Saludos desde Mallorca
Biel Maimó
http://bielsys.blogspot.com/

Continue the discussion