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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating international time

Status
Not open for further replies.

AndyGroom

Programmer
May 23, 2001
972
0
0
GB
Can anyone help me out with some code for working out what the time is in another country, taking daylight saving time into account where necessary?

I've tried searching for a solution but all I can find are finished products, not source code.

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
I wrote a piece of freeware about 10 years ago called Multiclock that does exactly this. I'll try and dig out the source code tonight if noone else has provided a solution beforwe then
 
Looks hopeful - thanks!

I'm 90% of the way there already with the coding, I'm just stuck on the daylight savings bit.

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
Hugh, that code was useful but I can't see how to enhance it for what I actually need. It only seems to contain functions for dealing with the local time.

For example, I'm in the UK and I want to show the current time in New Zealand.

Whilst I can use these functions to discover the daylight-savings-exempt time in the UK and then add 12 hours for NZ, how do I know whether I need to add or subtract an hour for daylight savings in NZ?

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
Actually, a quick perusal of the code example linked to by HughLerwill shows that it does have the necessary API call, SystemTimeToTzSpecificLocalTime, for establishing the time in foreign parts, but then only uses it to get the local time the PC is in.

The function it is contained in is called UTCtoLocal. You can see there that tzi contains the current time zone (since that it what the GetTimeZoneInformation API call retrieves). However what neither the example nor the documentation for the API call make clear is that you can feed it the time zone information for any time zone you are interested in and the current UTC time, ansd it will tell you the local time for that timezone

It might be slightly easier to see this in this extract from my program (the stuff after the Else was to calculate DST on W9x boxes because they don't have the API call):
Code:
[blue]Sub GetLocalTime(tziTime As TIME_ZONE_INFORMATION, lpLocal As SYSTEMTIME)
' Gets the correct local time for a specified timezone, and returns it in the lpLocal parameter
' Hmm, cannot for the life of me figure out why I wrote this as a sub rather than a function

    Dim lResult As Long
    Dim lpTime As SYSTEMTIME
    Dim ApplyDaylight As Boolean
    Dim mytime As Date
    
    Call GetSystemTime(lpTime)
    If strPlatformKey = "Windows NT" Then
        Call SystemTimeToTzSpecificLocalTime(tziTime, lpTime, lpLocal)
    Else
        ApplyDaylight = False
        If tziTime.DaylightDate.wDay <> 0 Then ApplyDaylight = TZIToDate(tziTime.DaylightDate) > Now() And TZIToDate(tziTime.StandardDate) < Now()
        mytime = DateAdd("n", -tziTime.Bias + tziTime.DaylightBias * ApplyDaylight, CDate(lpTime.wDay & " " & Format(lpTime.wMonth, "mmm") & " " & lpTime.wYear & " " & lpTime.wHour & ":" & lpTime.wMinute & ":" & lpTime.wSecond))
    'Call SystemTimeToTzSpecificLocalTime(tziTime, lpTime, lpLocal)
    'lpLocal.wDay = CLng(Format(mytime, "dd"))
        lpLocal.wHour = CLng(Format(mytime, "h"))
        lpLocal.wMinute = CLng(Format(mytime, "n"))
        lpLocal.wSecond = CLng(Format(mytime, "s"))
    End If
End Sub[/blue]

All the time zone information is held in the registry at

HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones

So all you've got left to do is figure out how to read the time zone information you want from the registry ...
 
Thanks guys, that seems straight forward, I just need to match up the time zone names from the registry with the ones already in my code and it should work.

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
Sorry HughLerwill, but that function just gets whatever is the current time zone which in turn means that SystemTimeToTzSpecificLocalTime simply returns the current local time (and is exactly what is used in the example you linked to previously).
 
<Multiclock

I downloaded it, too. The link is on a thread somewhere on this site, but I've never been able to find it again.
 
Any tips on how to get the name of a timezone (eg "Greenwich standard time") into the TIME_ZONE_INFORMATION.StandardName variable?

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
(and it is worth saying that, although not mentioned on the website, it also runs fine on Vista)
 
Sorry guys, I'm actually struggling with this now, I think I'm confusing myself more than anything else!

My problem is specifying a different country in the TZI structure. The definition I have is:

Code:
Private Type TIME_ZONE_INFORMATION
        Bias As Long
        StandardName(0 To 31) As Integer
        StandardDate As SYSTEMTIME
        StandardBias As Long
        DaylightName(0 To 31) As Integer
        DaylightDate As SYSTEMTIME
        DaylightBias As Long
End Type

I've tried passing StandardName as follows:

Code:
N$ = "Greenwich standard time"
For A& = 0 to Len(N$)
  tzi.StandardName(A&) = Asc(Mid$(N$, A& + 1, 1))
Next A&

...but that doesn't work. Is my definition correct, should it be a 32-byte integer array or something else?

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
>My problem is specifying a different country in the TZI structure

Let's just check why you are trying to do this first, as I think you may be approaching this wrong.
 
When you suggested that by filling in the TZI structure you could return the values for a specific time zone, I assumed that I could specify which time zone I am trying to get the settings for.

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
No, what I said was: read the time zone information you want from the registry
 
Sorry Strongm - my link was supposed to point a one titled 'RegQueryValueEx: Identify Time Zones by Time Zone Bias' on the same site but ended up pointing at a different one - pretty wierd.
Anyway a Google for 'RegQueryValueEx: Identify Time Zones by Time Zone Bias' gets it in first place, closely followed by
 
Ah - now that's closer ... (I'm holding off showing my version because it is trickly to cleanly extract it from the program)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top