convert UTC to local time(system time) and vice vesra in VB.NET

D

deepak

Hi All,

I want to convert the UTC time to local time zone(system time zone) and vice
versa in ASP.NET using vb.Net

currenlty i know this function which i took from internet site but this
function only converts from UTC to local and not vice versa. I need a
function which converts from utc to local and loac to utc time format for all
dates which is recevied by the ASP.NET
interface in UTC.
this interface will recieve the time in UTC and will convert using a vb.net
function intrenally into LOCAL and finally while again sending back it will
convert to UTC again and will send back to interfce.
So kindlt let me know a function which return UTC to Local(regisrty time of
machine based on time zone) and another function for local to UTC.

My current function for UTC -> LOCAL IS
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Imports Microsoft.Win32
Imports System.Runtime.InteropServices

Public Class timezone_converter
Private Structure TZI
Dim bias As Integer
Dim standardBias As Integer
Dim daylightBias As Integer
Dim standardDate As SYSTEMTIME
Dim daylightDate As SYSTEMTIME
End Structure

Public Structure TIME_ZONE_INFORMATION
Dim Bias As Integer
<VBFixedArray(32)> Dim StandardName() As Short
Dim StandardDate As SYSTEMTIME
Dim StandardBias As Integer
<VBFixedArray(32)> Dim DaylightName() As Short
Dim DaylightDate As SYSTEMTIME
Dim DaylightBias As Integer

Public Sub Initialize()
ReDim StandardName(32)
ReDim DaylightName(32)
End Sub
End Structure
Public Structure SYSTEMTIME
Dim wYear As Integer
Dim wMonth As Integer
Dim wDayOfWeek As Integer
Dim wDay As Integer
Dim wHour As Integer
Dim wMinute As Integer
Dim wSecond As Integer
Dim wMilliseconds As Integer
End Structure

Public Function UTCtoLocal(ByVal serverdate As DateTime, ByVal tzone As
String) As DateTime
Dim timezoneinfo As TIME_ZONE_INFORMATION
timezoneinfo.Initialize()
Dim servertime As SYSTEMTIME
Dim serverfiletime As New FILETIME
Dim Localfiletime As New FILETIME
Dim Localtime As SYSTEMTIME
Dim custTZI As TZI
Dim strTZI() As Byte
Dim temp1 As Long
Dim temp2 As Integer


Dim strPath As String = "Software\Microsoft\Windows
NT\CurrentVersion\Time Zones\" & tzone
Dim oReg As RegistryKey = Registry.LocalMachine.OpenSubKey(strPath)
strTZI = oReg.GetValue("TZI")
custTZI = BulidStr(strTZI)

timezoneinfo.Bias = custTZI.bias
timezoneinfo.StandardBias = custTZI.standardBias
timezoneinfo.StandardDate = custTZI.standardDate
timezoneinfo.DaylightBias = custTZI.daylightBias
timezoneinfo.DaylightDate = custTZI.daylightDate

temp1 = serverdate.Ticks

serverfiletime.dwHighDateTime = (temp1 >> 32)
temp2 = temp1 And &HFFFFFFFFL
serverfiletime.dwLowDateTime = Convert.ToInt32(temp2.ToString)

FileTimeToSystemTime(serverfiletime, servertime)
' TzSpecificLocalTimeToSystemTime(timezoneinfo, servertime, Localtime)
SystemTimeToTzSpecificLocalTime(timezoneinfo, servertime, Localtime)
SystemTimeToFileTime(Localtime, Localfiletime)

temp1 = Convert.ToInt64(Localfiletime.dwHighDateTime) << 32 Or
Convert.ToInt64(BitConverter.ToUInt32((BitConverter.GetBytes(Localfiletime.dwLowDateTime)), 0))
Dim Localdate As New DateTime(temp1)

Return Localdate

End Function


' Declare Sub TzSpecificLocalTimeToSystemTime Lib "kernel32.dll" (ByRef
lpTimeZone As TIME_ZONE_INFORMATION, ByRef lpLocalTime As SYSTEMTIME, ByRef
lpUniversalTime As SYSTEMTIME)
Declare Sub SystemTimeToFileTime Lib "kernel32.dll" (ByRef lpSystemTime
As SYSTEMTIME, ByRef lpFileTime As FILETIME)
Declare Sub FileTimeToSystemTime Lib "kernel32.dll" (ByRef lpFileTime As
FILETIME, ByRef lpSystemTime As SYSTEMTIME)
Declare Sub SystemTimeToTzSpecificLocalTime Lib "kernel32.dll" (ByRef
lpTimeZone As TIME_ZONE_INFORMATION, ByRef lpUniversalTime As SYSTEMTIME,
ByRef lpLocalTime As SYSTEMTIME)


Private Function BulidStr(ByVal Buff() As Byte) As Object
Dim MyGC As GCHandle = GCHandle.Alloc(Buff, GCHandleType.Pinned)
Dim temp As TZI

Dim Obj As Object = Marshal.PtrToStructure(MyGC.AddrOfPinnedObject,
temp.GetType)
Return Obj
'Free GChandle to avoid memory leaks
MyGC.Free()
End Function

End Class

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""


Any help would be greatly appreciated.

Thanks in advance
Deepak
(e-mail address removed)
(e-mail address removed)
 
K

Kevin Spencer

Good heavens, dude, all you need is the DateTime class. Everything you need
to convert it is there:

http://msdn2.microsoft.com/en-us/library/system.datetime.aspx

Specifically, look at the following properties and methods:

DateTime.Kind (indicates whether it is a UTC or Local DateTime instance)
DateTime.UtcNow (provides the current DateTime's UTC DateTime value)
DateTime.ToUniversalTime (converts a local DateTime instance to UTC)
DateTime.ToLocalTime (converts a UTC DateTime to local)

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP
 

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

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top