Visual Basic Locale/Regionalization Routines

EnumTimeFormats: Regional Locale Time Settings
     
Posted:   Saturday May 16, 1999
Updated:   Monday December 26, 2011
     
Applies to:   VB5, VB6
Developed with:   VB6, Windows NT4
OS restrictions:   None
Author:   VBnet - Randy Birch
     

Related:  

SetLocaleInfo: Change System Long and Short Date Formats
WM_TIMECHANGE: Detect System Changes to the Date/Time
RegQueryValueEx: Identify Time Zones by Time Zone Bias
EnumDateFormats: Regional Locale Date Settings
EnumTimeFormats: Regional Locale Time Settings
GetLocaleInfo: Regional Locale Date Settings
       
 Prerequisites
VB5 or VB6.

Locales.bas created in GetLocaleInfo: Regional Locale Date Settings.


The national language support functions let applications set the locale for the user, identifying the language in which the user carries out work and retrieves strings, representing times, dates, and other information, that are correctly formatted for the given language and location of the world. National language support also includes support for keyboard layouts and language-specific fonts.

Like EnumDateFormats, EnumTimeFormats allows the developer to interrogate the system to retrieve the valid system time formats via EnumTimeFormatsProc, an application defined-callback function used with the EnumTimeFormats function. It receives a pointer to a string buffer containing a time format string.

The single value returned from the callback - lpTimeFormatString - is a pointer to a string buffer containing a null-terminated time format string.  lpTimeFormatString should be an LPWSTR for the Unicode (W) version of EnumTimeFormats, and an LPSTR for the ANSI (A) version of EnumTimeFormats.

 BAS Module Code
Either add the following code to Locales.bas, or add another BAS module to the project:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Declare Function EnumTimeFormats Lib "kernel32" _
   Alias "EnumTimeFormatsA" _
  (ByVal lpTimeFmtEnumProc As Long, _
   ByVal Locale As Long, _
   ByVal dwFlags As Long) As Long

Public Declare Sub CopyMemory Lib "kernel32" _
   Alias "RtlMoveMemory" _
  (Destination As Any, _
   Source As Any, _
   ByVal Length As Long)

Public Const LOCALE_USE_CP_ACP = &H40000000


Public Function EnumTimeProc(lpTimeFormatString As Long) As Long

  'application-defined callback function for EnumTimeFormats

   Dim buffer As String
   Dim pos As Integer
   
  'pad a string to hold the format
   buffer = Space$(32)
   
  'copy the string pointed to by the return value
   CopyMemory ByVal buffer, lpTimeFormatString, ByVal Len(buffer)
   
  'add to the list
   pos = InStr(buffer, Chr$(0))
   
   If pos Then
      buffer = Left$(buffer, pos - 1)
   End If
   
   Form2.List1.AddItem "   " & buffer
   
  'and return 1 to continue enumeration
   EnumTimeProc = 1
   
End Function
 Form Code
Create a form containing 4 textboxes (Text1-Text4), a command button (Command1) and a listbox (List1). Label as desired, and add the following code to the form:

Option Explicit


Private Sub Command1_Click()

   Dim LCID As Long
   
  'get the user's Locale ID
   LCID = GetSystemDefaultLCID()
   
  'show localized name of language
   Text1.Text = GetUserLocaleInfo(LCID, LOCALE_SLANGUAGE)
   
  'Enumerate the available time formats. 
  'Note that LOCALE_USE_CP_ACP is the only valid flag.
   Call EnumTimeFormats(AddressOf EnumTimeProc, LCID, LOCALE_USE_CP_ACP)
      
  'Show the user's time format string
   Text2.Text = GetUserLocaleInfo(LCID, LOCALE_STIMEFORMAT)
   
  'Show the AM designator
   Text3.Text = GetUserLocaleInfo(LCID, LOCALE_S1159)

  'Show the PM designator
   Text4.Text = GetUserLocaleInfo(LCID, LOCALE_S2359)

End Sub
 Comments
Save the program and run. The values displayed should correspond to the Codepage and Regional Settings for your system.
While the GetSystemDefaultLCID function retrieves the system default locale identifier, this is often inappropriate or insufficient in a networked environment or under an operating system where multiple locales have been installed. For example, it is possible for a network admin rolling out a standard image to have the user's default locale set to one differing from the base OS installation, and thus the system default locale.

In this situation Windows' provides an alternate API you can use to obtain the LCID for the current user ... GetUserDefaultLCID. Defined identically to GetSystemDefaultLCID, GetUserDefaultLCID function retrieves the user default–locale identifier and is therefore the most appropriate API to use when it is the user's locale you are interested in, rather than that of the system.


 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter