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

Discussion in 'ASP .Net' started by deepak, Feb 6, 2008.

  1. deepak

    deepak Guest

    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

    deepak, Feb 6, 2008
    #1
    1. Advertising

  2. 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

    "deepak" <> wrote in message
    news:...
    > 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
    >
    >
    >
    >
    Kevin Spencer, Feb 6, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?aGVuaw==?=

    Covert UTC String to UTC Datetime

    =?Utf-8?B?aGVuaw==?=, May 7, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    18,729
    Egbert Nierop \(MVP for IIS\)
    May 7, 2006
  2. Replies:
    1
    Views:
    794
    Paul Boddie
    Mar 11, 2007
  3. Mark B
    Replies:
    1
    Views:
    868
    Alexey Smirnov
    Sep 24, 2009
  4. Jonathan Wood
    Replies:
    1
    Views:
    2,196
    Jonathan Wood
    Nov 1, 2009
  5. Convert UTC to Local Time

    , Mar 8, 2007, in forum: Javascript
    Replies:
    6
    Views:
    195
Loading...

Share This Page