Displaing local time zone

H

Harry

I have a report which is distributed to users in different time zones. To
alleviate confusion I’d like to add a time zone designation; for example, “as
of 4:15 CETâ€, or “printed at 12:00 GMTâ€.

I’m not concerned about converting to the reader’s time zone, I’d simply
like to display the time zone in which the report was printed.

Is it possible to:
a) add the time zone via Access’ time formatting functions? (can't seem to
find it if there is...)
b) retrieve the user’s time zone directly from Windows?

Thanks
 
D

Douglas J. Steele

There's nothing in Access that I'm aware of to add time zones.

To get the time zone from Windows, check what Randy Birch has at
http://vbnet.mvps.org/code/locale/gettimezonenames.htm

(Obligatory warning: Randy's site is aimed at VB programmers. Because there
are some significant differences between the controls available for forms in
VB and in Access, some of his samples will not port directly into Access.
Look at this particular one, the only issue I can see is that he's assigning
the values of the functions to the .Text property of the two text boxes. In
Access, you can only use the .Text property when the control has focus. To
get around that, simply remove the .Text and use the default .Value
property)
 
R

RDub

Here is a way. It was actually quite complicated. Perhaps someone has a
better way.

Create a new form. But a button in it whose name is cmdGetTimeZone. Then
paste in all of the ode below.

Rdub

' ***** Code Starts *****
Option Compare Database
Option Explicit

' API to access VB6 String by pointer in order to copy memory
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)

' API Stuff to Get the Time Zone Info
Private Declare Function GetTimeZoneInformation Lib "kernel32" _
(lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long

Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type

Private Type TIME_ZONE_INFORMATION
Bias As Long
StandardName As String * 64
StandardDate As SYSTEMTIME
StandardBias As Long
DaylightName As String * 64
DaylightDate As SYSTEMTIME
DaylightBias As Long
End Type

Function ToUTF8(ByVal UTF16 As Long) As Byte()
' Convert a 16bit UTF-16BE to 2 or 3 UTF-8 bytes
Dim BArray() As Byte
If UTF16 < &H80 Then
ReDim BArray(0) ' one byte UTF-8
BArray(0) = UTF16 ' Use number as is
ElseIf UTF16 < &H800 Then
ReDim BArray(1) ' two byte UTF-8
BArray(1) = &H80 + (UTF16 And &H3F) ' Least Significant 6 bits
UTF16 = UTF16 \ &H40 ' Shift UTF16 number right 6 bits
BArray(0) = &HC0 + (UTF16 And &H1F) ' Use 5 remaining bits
Else
ReDim BArray(2) ' three byte UTF-8
BArray(2) = &H80 + (UTF16 And &H3F) ' Least Significant 6 bits
UTF16 = UTF16 \ &H40 ' Shift UTF16 number right 6 bits
BArray(1) = &H80 + (UTF16 And &H3F) ' Use next 6 bits
UTF16 = UTF16 \ &H40 ' Shift UTF16 number right 6 bits again
BArray(0) = &HE0 + (UTF16 And &HF) ' Use 4 remaining bits
End If
ToUTF8 = BArray ' Return UTF-8 bytes in an array
End Function

Function UniStrToUTF8(UniString) As Byte()
' Convert a Unicode string to a byte stream of UTF-8
Dim BArray() As Byte
Dim TempB() As Byte
Dim i As Long
Dim k As Long
Dim TLen As Long
Dim b1 As Byte
Dim b2 As Byte
Dim UTF16 As Long
Dim j
TLen = Len(UniString) ' Obtain length of Unicode input string
If TLen = 0 Then Exit Function ' get out if there's nothing to convert
k = 0
For i = 1 To TLen
' Work out the UTF16 value of the Unicode character
CopyMemory b1, ByVal StrPtr(UniString) + ((i - 1) * 2), 1
CopyMemory b2, ByVal StrPtr(UniString) + ((i - 1) * 2) + 1, 1
' Combine the 2 bytes into the Unicode UTF-16
UTF16 = b2 ' assign b2 to UTF16 before multiplying by 256 to avoid
overflow
UTF16 = UTF16 * 256 + b1
' Convert UTF-16 to 2 or 3 bytes of UTF-8
TempB = ToUTF8(UTF16)
' Copy the resultant bytes to BArray
For j = 0 To UBound(TempB)
ReDim Preserve BArray(k)
BArray(k) = TempB(j): k = k + 1
Next
ReDim TempB(0)
Next
UniStrToUTF8 = BArray ' Return the resultant UTF-8 byte array
End Function

Private Sub cmdGetTimeZone_Click()
Dim intStandardMonth As Integer, intStandardDay As Integer
Dim intDaylightMonth As Integer, intDaylightDay As Integer
Dim intMonthNow As Integer, intDayNow As Integer
Dim blnDaylightSavings As Boolean
Dim TZI As TIME_ZONE_INFORMATION

Call GetTimeZoneInformation(TZI)

' Determine if we need to account for daylight savings time.
' this is precise to the day, not to the hour, so if you need
' more precision, change this code to consider wHour, wMinute
' and even wSecond Parameter
intStandardMonth = TZI.StandardDate.wMonth
intStandardDay = TZI.StandardDate.wDay

intDaylightMonth = TZI.DaylightDate.wMonth
intDaylightDay = TZI.DaylightDate.wDay

intMonthNow = Month(Now)
intDayNow = Day(Now)

If intStandardMonth = intMonthNow Then
blnDaylightSavings = (intDayNow < intStandardDay)
ElseIf intDaylightMonth = intMonthNow Then
blnDaylightSavings = (intDayNow >= intDaylightDay)
Else
If intDaylightMonth < intStandardMonth Then
blnDaylightSavings = intMonthNow > intDaylightMonth _
And intMonthNow < intStandardMonth
Else
blnDaylightSavings = intMonthNow > intDaylightMonth _
Or intMonthNow < intStandardMonth
End If
End If

' Now print the Current Zime zone info
If blnDaylightSavings Then
Debug.Print UniStrToUTF8(TZI.DaylightName)
Else
Debug.Print UniStrToUTF8(TZI.StandardName)
End If
End Sub
'***** Code Ends *****
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top