Du bist hier: Snippet-Verzeichnis » Visual Basic (78)
Sprache:

Convert Local Time to UTC (GMT) time

Sprache: English
Programmiersprache: Visual Basic
Veröffentlicht von: echten [nicht registriert]
Letzte Änderung: 15.05.2006
Aufrufe: 1597


Beschreibung

I was looking for an API/function/snippet that could convert a given local time to UTC (formerly known as GMT), but could only find an API that was only available in Windows XP. Therefore I looked into combining some other APIs. I created the following function. Just call the function with the time you want to convert. The result is a boolean telling you if the conversion was successful. The converted date is returned in the original variable. Feel free to modify this to return the converted date or pass the converted date in another parameter.

Code

1 Option Explicit 2 3 Private Type SYSTEMTIME 4 wYear As Integer 5 wMonth As Integer 6 wDayOfWeek As Integer 7 wDay As Integer 8 wHour As Integer 9 wMinute As Integer 10 wSecond As Integer 11 wMilliseconds As Integer 12 End Type 13 14 Private Type FILETIME 15 dwLowDateTime As Long 16 dwHighDateTime As Long 17 End Type 18 19 Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long 20 Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long 21 Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long 22 23 Private Function ConvertLocalDateTimeToSystemDateTime(ByRef dteDateTime As Date) As Boolean 24 25 Dim structSTLocal As SYSTEMTIME 26 Dim structFTLocal As FILETIME 27 Dim structSTSystem As SYSTEMTIME 28 Dim structFTSystem As FILETIME 29 Dim blnResults As Boolean 30 31 'First convert the date to a SYSTEMTIME structure 32 With structSTLocal 33 .wDay = DatePart("d", dteDateTime, vbMonday) 34 .wDayOfWeek = DatePart("w", dteDateTime, vbMonday) 35 .wHour = DatePart("h", dteDateTime, vbMonday) 36 .wMilliseconds = 0 37 .wMinute = DatePart("n", dteDateTime, vbMonday) 38 .wMonth = DatePart("m", dteDateTime, vbMonday) 39 .wSecond = DatePart("s", dteDateTime, vbMonday) 40 .wYear = DatePart("yyyy", dteDateTime, vbMonday) 41 End With 42 43 'Now convert this SYSTEMTIME structure to a FILESYSTEM structure 44 blnResults = (SystemTimeToFileTime(structSTLocal, structFTLocal) <> 0) 45 46 'Now convert the (local) File Time to UTC File Time 47 blnResults = blnResults And _ 48 (LocalFileTimeToFileTime(structFTLocal, structFTSystem) <> 0) 49 50 'Now convert the File time back to system time 51 blnResults = blnResults And _ 52 (FileTimeToSystemTime(structFTSystem, structSTSystem) <> 0) 53 54 If blnResults Then 55 'Convert the SYSTEMTIME structure back to a date 56 With structSTSystem 57 dteDateTime = CDate(.wMonth & "/" & .wDay & "/" & .wYear & " " & _ 58 .wHour & ":" & .wMinute & ":" & .wSecond) 59 End With 60 End If 61 62 ConvertLocalDateTimeToSystemDateTime = blnResults 63 End Function

2 Kommentare

1

Does it take into consideration Daylight Savings Time?

Montag, 1. Januar 0001 00:00:00 von Luvy
2

Very interesting code.

Freitag, 22. August 2008 07:39:53 von ilas

Dieses Snippet kommentieren

Name *  

E-Mail (wird nicht angezeigt) *    

Website  

Kommentar *  

Sicherheitscode Sicherheitscode *    

RSS