Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2008

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
deepak - 06 Feb 2008 10:00 GMT
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
kr_deepak123@yahoo.co.in
kr_deepak123@hotmail.com
Kevin Spencer - 06 Feb 2008 14:34 GMT
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)

Signature

HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

> Hi All,
>
[quoted text clipped - 138 lines]
> kr_deepak123@yahoo.co.in
> kr_deepak123@hotmail.com

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.