Reading UTC (GMT) time offset from registry

Reading UTC (GMT) time offset from registry

Postby James Bott » Sat Dec 06, 2008 7: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


Code: Select all  Expand view  RUN
#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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Reading UTC (GMT) time offset from registry

Postby Enrico Maria Giordano » Sat Dec 06, 2008 8:02 pm

Code: Select all  Expand view  RUN
#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
User avatar
Enrico Maria Giordano
 
Posts: 8718
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Postby James Bott » Sat Dec 06, 2008 8: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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Postby James Bott » Sat Dec 06, 2008 8: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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Postby James Bott » Sat Dec 06, 2008 9:13 pm

OK, I solved the problem by renaming the first two functions. It is unfortunate that FWH's functions don't support the same parameters.

Antonio, a possible solution might be to use Enrico's functions (with a different name) and then create a FWH functions just to change the order of the parameters and pass them on to Enrico's functions. This would prevent existing code from breaking and still allow us to use the extra parameters.

Of course, this assumes that Enrico will allow his functions to be used.

James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Postby Enrico Maria Giordano » Sat Dec 06, 2008 9: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
User avatar
Enrico Maria Giordano
 
Posts: 8718
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Postby Biel EA6DD » Tue Dec 09, 2008 9:53 am

Hi James,
This is how I retriev the GMT from the registry.
Code: Select all  Expand view  RUN
//------------------
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/
User avatar
Biel EA6DD
 
Posts: 682
Joined: Tue Feb 14, 2006 9:48 am
Location: Mallorca

Postby James Bott » Tue Dec 09, 2008 3: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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Postby James Bott » Tue Dec 09, 2008 4: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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Postby Biel EA6DD » Tue Dec 09, 2008 4: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/
User avatar
Biel EA6DD
 
Posts: 682
Joined: Tue Feb 14, 2006 9:48 am
Location: Mallorca


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 48 guests