Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > convert UTC to local time(system time) and vice vesra in VB.NET

Reply
Thread Tools

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

 
 
deepak
Guest
Posts: n/a
 
      02-06-2008
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((BitConverte r.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
http://www.velocityreviews.com/forums/(E-Mail Removed)
(E-Mail Removed)


 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      02-06-2008
Good heavens, dude, all you need is the DateTime class. Everything you need
to convert it is there:

http://msdn2.microsoft.com/en-us/lib....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

"deepak" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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((BitConverte r.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 Removed)
> (E-Mail Removed)
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert UTC DateTime to Local Time (Not My Local Time) Jonathan Wood ASP .Net 1 11-01-2009 12:08 AM
Convert stored UTC time to webuser's local time? Mark B ASP .Net 1 09-24-2009 07:51 AM
convert time string in UTC to time in local time davelist@mac.com Python 1 03-11-2007 12:57 AM
Convert UTC to Local Time vunet.us@gmail.com Javascript 6 03-09-2007 02:12 AM
Covert UTC String to UTC Datetime =?Utf-8?B?aGVuaw==?= ASP .Net 3 05-07-2006 06:25 PM



Advertisments