UTC to Local Time

Category: Time/Timers

Date: 02-16-2022

Return to Index


'Time zone offset � the difference of the time given to UTC/GMT in HourMinute
'preceded by + if the time is ahead (east) of UTC and preceded by - if the time is behind (west) of UTC: -2359 to +2359
 
'How Can I Convert the Date and Time to My Time Zone?
'To convert the date and time to your time zone, do the following:
 
'1. Subtract any + time zone offset from the time or add any - time zone offset to the time
'2. Do pay attention to the date: if your result is greater than 23:59, add a day and subtract 24 hours from the result; if the result is less than 0, subtract a day and add 24 hours to the resulting time.
'3. Add or subtract your current time zone offset from UTC.
'4. Repeat the data calculation from step 2.
 
 
'Compilable Example:  (Jose Includes)
#Compiler PBWin 10
#Compile Exe
#Dim All
%Unicode = 1
#Include "Win32API.inc"
 
Enum Equates Singular
   IDC_Button = 500
   IDC_TextBoxIn
   IDC_TextBoxOut
End Enum
 
Global hDlg As Dword
 
Function PBMain() As Long
   Dialog Default Font "Tahoma",12,1
   Dialog New Pixels, 0, "UTC to Local Time Converter",300,300,400,130, %WS_OverlappedWindow To hDlg
   Control Add Button, hDlg, %IDC_Button,"Convert", 50,10,100,25
   Control Add TextBox, hDlg, %IDC_TextBoxIn,"Date: Tue, 3 Jan 2017 22:27:31 +0400 (GMT)",20,40,360,30
   Control Add TextBox, hDlg, %IDC_TextBoxOut,"",20,80,360,30
   Dialog Show Modal hDlg Call DlgProc
End Function
 
CallBack Function DlgProc() As Long
   Local temp$
   Select Case Cb.Msg
      Case %WM_Command
         Select Case Cb.Ctl
            Case %IDC_Button
            Control Get Text hDlg, %IDC_TextBoxIn To temp$
               ConvertUTC(temp$)
         End Select
   End Select
End Function
 
Sub ConvertUTC(EmailString$)
   Local MyTime As IPowerTime, LocalTimeString$, temp$
   Local iHour, iMinute, iSecond, iDay, iMonth, iYear, iZoneMinutes, iPos As Long
   Let MyTime = Class "PowerTime"
 
   iPos = InStr(EmailString$,"(")
   If iPos Then EmailString$ = Left$(EmailString$,iPos-1)
 
   iDay    = Val(Parse$(EmailString$,$Spc,3))
   iMonth  = InStr("JanFebMarAprMayJunJulAugSepOctNovDec",Parse$(EmailString$,$Spc,4))/3+1
   iYear   = Val(Parse$(EmailString$,$Spc,5))
   temp$   = Parse$(EmailString$,$Spc,6)        '22:27:31  hr:min:sec
   iHour   = Val(Parse$(temp$,":",1))
   iMinute = Val(Parse$(temp$,":",2))
   iSecond = Val(Parse$(temp$,":",3))
   temp$   = Parse$(EmailString$,$Spc,7)        '+0400
   iZoneMinutes = Sgn(Val(temp$)) * (Val(Mid$(temp$,2,2)) * 60 + Val(Right$(temp$,2)))
 
   MyTime.NewDate(iYear, iMonth, iDay)      'UTC time
   MyTime.NewTime(iHour, iMinute, iSecond)  'UTC time
   MyTime.AddMinutes(-iZoneMinutes)         'Time Zone adjustment
   MyTime.ToLocalTime                       'Convert UTC to Local time
   LocalTimeString$ = Left$(MyTime.DayOfWeekString,3) + "," + _
                      Str$(MyTime.Day) + $Spc + _
                      Left$(MyTime.MonthString,3) + _
                      Str$(MyTime.Year) + $Spc + _
                      Left$(MyTime.TimeStringFull,8)
   Control Set Text hDlg, %IDC_TextBoxOut, LocalTimeString$
End Sub


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm