PDA

Vollständige Version anzeigen : Wie ermittle ich Zeitzonendaten (Sommerzeit etc.)?


Stefan Kulpa
01.02.2003, 06:03
<div><link href="http://www.ms-office-forum.de/ubb/codeconv.css" rel="stylesheet"><pre><span class="TOKEN">Option</span> <span class="TOKEN">Explicit</span>
&nbsp;
<span class="REM">'// -------------------------------------------------------------------------------------</span>
<span class="REM">'// Dieser Codeausschnitt ermittelt verschiedene Zeitzoneninformationen und bedient sich</span>
<span class="REM">'// dabei des Win32-API. Folgende Funktionen stehen zur Verf&uuml;gung:</span>
<span class="REM">'// -------------------------------------------------------------------------------------</span>
<span class="REM">'// Funktion: Beschreibung:</span>
<span class="REM">'// -------------------------------------------------------------------------------------</span>
<span class="REM">'// DaylightSavingExists Ermittelt, ob die Zeitzone (des Systems) eine Sommerzeit hat</span>
<span class="REM">'// DaylightSaving Ermittelt, ob Sommerzeit besteht</span>
<span class="REM">'// StandardBias Ermittelt die Sommerzeit-Zeitverschiebung gegen&uuml;ber GMT-Uhrzeit in Minuten</span>
<span class="REM">'// DaylightBias Ermittelt die die Standardzeit-Zeitverschiebung gegen&uuml;ber GMT-Uhrzeit in Minuten</span>
<span class="REM">'// CurrentBias Ermittelt die aktuelle Zeitverschiebung gegen&uuml;ber GMT-Uhrzeit in Minuten</span>
<span class="REM">'// DaylightName Ermittelt den Klartextnamen der Sommerzeit-Zeitzone</span>
<span class="REM">'// StandardName Ermittelt den Klartextnamen der Standardzeit-Zeitzone</span>
<span class="REM">'// GMTTime Ermittelt die aktuelle GMT-Uhrzeit (inkl. Datum)</span>
<span class="REM">'// FirstDateDaylight Ermittelt das Startdatum der Sommerzeit</span>
<span class="REM">'// FirstDateStandard Ermittelt das Startdatum der Standardzeit</span>
<span class="REM">'// -------------------------------------------------------------------------------------</span>
&nbsp;
<span class="TOKEN">Const</span> TIME_ZONE_ID_DAYLIGHT <span class="TOKEN">As</span> <span class="TOKEN">Long</span> = 2
&nbsp;
<span class="TOKEN">Type</span> SYSTEMTIME
wYear <span class="TOKEN">As</span> <span class="TOKEN">Integer</span>
wMonth <span class="TOKEN">As</span> <span class="TOKEN">Integer</span>
wDayOfWeek <span class="TOKEN">As</span> <span class="TOKEN">Integer</span>
wDay <span class="TOKEN">As</span> <span class="TOKEN">Integer</span>
wHour <span class="TOKEN">As</span> <span class="TOKEN">Integer</span>
wMinute <span class="TOKEN">As</span> <span class="TOKEN">Integer</span>
wSecond <span class="TOKEN">As</span> <span class="TOKEN">Integer</span>
wMilliseconds <span class="TOKEN">As</span> <span class="TOKEN">Integer</span>
<span class="TOKEN">End</span> <span class="TOKEN">Type</span>
&nbsp;
<span class="TOKEN">Type</span> TIME_ZONE_INFORMATION
Bias <span class="TOKEN">As</span> <span class="TOKEN">Long</span> <span class="REM">' Basis-Zeitverschiebung in Minuten</span>
StandardName(1 <span class="TOKEN">To</span> 64) <span class="TOKEN">As</span> <span class="TOKEN">Byte</span> <span class="REM">' Name der Sommerzeit-Zeitzone</span>
StandardDate <span class="TOKEN">As</span> SYSTEMTIME <span class="REM">' Beginn der Standardzeit</span>
StandardBias <span class="TOKEN">As</span> <span class="TOKEN">Long</span> <span class="REM">' Zus&auml;tzliche Zeitverschiebung der Standardzeit</span>
DaylightName(1 <span class="TOKEN">To</span> 64) <span class="TOKEN">As</span> <span class="TOKEN">Byte</span> <span class="REM">' Name der Sommerzeit-Zeitzone</span>
DaylightDate <span class="TOKEN">As</span> SYSTEMTIME <span class="REM">' Beginn der Sommerzeit</span>
DaylightBias <span class="TOKEN">As</span> <span class="TOKEN">Long</span> <span class="REM">' Zus&auml;tzliche Zeitverschiebung der Sommerzeit</span>
<span class="TOKEN">End</span> <span class="TOKEN">Type</span>
&nbsp;
<span class="TOKEN">Declare</span> <span class="TOKEN">Function</span> GetTimeZoneInformation <span class="TOKEN">Lib</span> &quot;kernel32&quot; _
(lpTimeZoneInformation <span class="TOKEN">As</span> TIME_ZONE_INFORMATION) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
&nbsp;
<span class="TOKEN">Sub</span> Beispiel()
&nbsp;
<span class="TOKEN">Debug.Print</span> &quot;Aktuelle Zeit&quot;
<span class="TOKEN">Debug.Print</span> &quot;-------------&quot;
<span class="TOKEN">Debug.Print</span> &quot;Aktuelle Lokalzeit: &quot;; Format$(Now(), &quot;dd.mm.yyyy, hh:nn:ss U\hr&quot;)
<span class="TOKEN">Debug.Print</span> &quot;Aktuelle GMT-Zeit: &quot;; Format$(GMTTime(), &quot;dd.mm.yyyy, hh:nn:ss U\hr&quot;)
<span class="TOKEN">Debug.Print</span> &quot;Sommerzeit: &quot;; IIf(DaylightSaving(), &quot;Ja&quot;, &quot;Nein&quot;)
<span class="TOKEN">Debug.Print</span>
<span class="TOKEN">Debug.Print</span> &quot;Standardzeit&quot;
<span class="TOKEN">Debug.Print</span> &quot;------------&quot;
<span class="TOKEN">Debug.Print</span> &quot;Name der Standardzeit: &quot;; StandardName()
<span class="TOKEN">Debug.Print</span> &quot;Beginn der Standardzeit: &quot;; Format$(FirstDateStandard(), &quot;dd.mm.yyyy, hh:nn:ss U\hr&quot;)
<span class="TOKEN">Debug.Print</span> &quot;Zeitverschiebung: &quot;; &quot;GMT&quot; &amp; IIf(StandardBias &lt; 0, &quot;&quot;, &quot;+&quot;) &amp; StandardBias &amp; &quot; Minuten&quot;
<span class="TOKEN">Debug.Print</span>
<span class="TOKEN">Debug.Print</span> &quot;Sommerzeit&quot;
<span class="TOKEN">Debug.Print</span> &quot;----------&quot;
<span class="TOKEN">If</span> DaylightSavingExists = <span class="TOKEN">True</span> <span class="TOKEN">Then</span>
<span class="TOKEN">Debug.Print</span> &quot;Name der Sommerzeit: &quot;; DaylightName()
<span class="TOKEN">Debug.Print</span> &quot;Beginn der Sommerzeit: &quot;; Format$(FirstDateDaylight(), &quot;dd.mm.yyyy, hh:nn:ss U\hr&quot;)
<span class="TOKEN">Debug.Print</span> &quot;Zeitverschiebung: &quot;; &quot;GMT&quot; &amp; IIf(DaylightBias &lt; 0, &quot;&quot;, &quot;+&quot;) &amp; DaylightBias &amp; &quot; Minuten&quot;
<span class="TOKEN">Else</span>
<span class="TOKEN">Debug.Print</span> vbTab; &quot;Zeitzone hat keine Sommerzeit!&quot;
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
<span class="REM">'// -------------------------------------------------------------------------------------</span>
<span class="REM">'// Ausgabe:</span>
<span class="REM">'//</span>
<span class="REM">'// Aktuelle Zeit</span>
<span class="REM">'// -------------</span>
<span class="REM">'// Aktuelle Lokalzeit: 01.02.2003, 06:01:20 Uhr</span>
<span class="REM">'// Aktuelle GMT-Zeit: 01.02.2003, 05:01:20 Uhr</span>
<span class="REM">'// Sommerzeit: Nein</span>
<span class="REM">'//</span>
<span class="REM">'// Standardzeit</span>
<span class="REM">'// ------------</span>
<span class="REM">'// Name der Standardzeit: Westeurop&auml;ische Normalzeit</span>
<span class="REM">'// Beginn der Standardzeit: 26.10.2003, 03:00:00 Uhr</span>
<span class="REM">'// Zeitverschiebung: GMT+60 Minuten</span>
<span class="REM">'//</span>
<span class="REM">'// Sommerzeit</span>
<span class="REM">'// ----------</span>
<span class="REM">'// Name der Sommerzeit: Westeurop&auml;ische Sommerzeit</span>
<span class="REM">'// Beginn der Sommerzeit: 30.03.2003, 02:00:00 Uhr</span>
<span class="REM">'// Zeitverschiebung: GMT+120 Minuten</span>
<span class="REM">'// -------------------------------------------------------------------------------------</span>
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Sub</span>
&nbsp;
<span class="TOKEN">Function</span> DaylightSavingExists() <span class="TOKEN">As</span> <span class="TOKEN">Boolean</span>
<span class="REM">'// Gibt zur&uuml;ck, ob die Zeitzone eine Sommerzeit hat.</span>
<span class="TOKEN">Dim</span> udtTZI <span class="TOKEN">As</span> TIME_ZONE_INFORMATION
<span class="TOKEN">Dim</span> RetVal <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
RetVal = GetTimeZoneInformation(udtTZI)
DaylightSavingExists = (udtTZI.DaylightDate.wMonth &lt;&gt; 0)
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Function</span>
&nbsp;
<span class="TOKEN">Function</span> DaylightSaving() <span class="TOKEN">As</span> <span class="TOKEN">Boolean</span>
<span class="REM">'// Gibt zur&uuml;ck, ob Sommerzeit besteht.</span>
<span class="TOKEN">Dim</span> udtTZI <span class="TOKEN">As</span> TIME_ZONE_INFORMATION
<span class="TOKEN">Dim</span> RetVal <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
RetVal = GetTimeZoneInformation(udtTZI)
DaylightSaving = (RetVal = TIME_ZONE_ID_DAYLIGHT)
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Function</span>
&nbsp;
<span class="TOKEN">Function</span> StandardBias() <span class="TOKEN">As</span> <span class="TOKEN">Integer</span>
<span class="REM">'// Gibt die Standardzeit-Zeitverschiebung</span>
<span class="REM">'// gegen&uuml;ber GMT-Uhrzeit in Minuten zur&uuml;ck.</span>
<span class="TOKEN">Dim</span> udtTZI <span class="TOKEN">As</span> TIME_ZONE_INFORMATION
GetTimeZoneInformation udtTZI
StandardBias = -(udtTZI.Bias + udtTZI.StandardBias)
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Function</span>
&nbsp;
<span class="TOKEN">Function</span> DaylightBias() <span class="TOKEN">As</span> <span class="TOKEN">Integer</span>
<span class="REM">'// Gibt die Sommerzeit-Zeitverschiebung</span>
<span class="REM">'// gegen&uuml;ber GMT-Uhrzeit in Minuten zur&uuml;ck.</span>
<span class="TOKEN">Dim</span> udtTZI <span class="TOKEN">As</span> TIME_ZONE_INFORMATION
GetTimeZoneInformation udtTZI
DaylightBias = -(udtTZI.Bias + udtTZI.DaylightBias)
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Function</span>
&nbsp;
<span class="TOKEN">Function</span> CurrentBias() <span class="TOKEN">As</span> <span class="TOKEN">Integer</span>
<span class="REM">'// Gibt die aktuelle Zeitverschiebung</span>
<span class="REM">'// gegen&uuml;ber GMT-Uhrzeit in Minuten zur&uuml;ck.</span>
<span class="TOKEN">Dim</span> udtTZI <span class="TOKEN">As</span> TIME_ZONE_INFORMATION
<span class="TOKEN">Dim</span> RetVal <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
RetVal = GetTimeZoneInformation(udtTZI)
<span class="TOKEN">With</span> udtTZI
<span class="TOKEN">If</span> RetVal = TIME_ZONE_ID_DAYLIGHT <span class="TOKEN">Then</span>
CurrentBias = -(.Bias + .DaylightBias)
Else: CurrentBias = -(.Bias + .StandardBias)
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
<span class="TOKEN">End</span> <span class="TOKEN">With</span>
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Function</span>
&nbsp;
<span class="TOKEN">Function</span> DaylightName() <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="REM">'// Gibt den Klartextnamen der Sommerzeit-Zeitzone zur&uuml;ck.</span>
<span class="TOKEN">Dim</span> udtTZI <span class="TOKEN">As</span> TIME_ZONE_INFORMATION
<span class="TOKEN">Dim</span> lNullPos <span class="TOKEN">As</span> <span class="TOKEN">Integer</span>
GetTimeZoneInformation udtTZI
<span class="TOKEN">With</span> udtTZI
<span class="TOKEN">If</span> InStr(.DaylightName, vbNullChar) &gt; 0 <span class="TOKEN">Then</span>
DaylightName = Left$(.DaylightName, InStr(.DaylightName, vbNullChar) - 1)
Else: DaylightName = .DaylightName
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
<span class="TOKEN">End</span> <span class="TOKEN">With</span>
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Function</span>
&nbsp;
<span class="TOKEN">Function</span> StandardName() <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="REM">'// Gibt den Klartextnamen der Standardzeit-Zeitzone zur&uuml;ck.</span>
<span class="TOKEN">Dim</span> udtTZI <span class="TOKEN">As</span> TIME_ZONE_INFORMATION
GetTimeZoneInformation udtTZI
<span class="TOKEN">With</span> udtTZI
<span class="TOKEN">If</span> InStr(.StandardName, vbNullChar) &gt; 0 <span class="TOKEN">Then</span>
StandardName = Left$(.StandardName, InStr(.StandardName, vbNullChar) - 1)
Else: StandardName = .StandardName
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
<span class="TOKEN">End</span> <span class="TOKEN">With</span>
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Function</span>
&nbsp;
<span class="TOKEN">Function</span> GMTTime() <span class="TOKEN">As</span> Date
<span class="REM">'// Gibt die aktuelle GMT-Uhrzeit (inkl. Datum) zur&uuml;ck.</span>
GMTTime = DateAdd(&quot;n&quot;, -CurrentBias(), Now)
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Function</span>
&nbsp;
<span class="TOKEN">Function</span> FirstDateDaylight(<span class="TOKEN">Optional</span> <span class="TOKEN">ByVal</span> InYear <span class="TOKEN">As</span> <span class="TOKEN">Long</span>) <span class="TOKEN">As</span> Date
<span class="REM">'// Gibt das Startdatum der Sommerzeit zur&uuml;ck.</span>
<span class="TOKEN">Dim</span> udtTZI <span class="TOKEN">As</span> TIME_ZONE_INFORMATION
<span class="TOKEN">If</span> InYear = 0 <span class="TOKEN">Then</span> InYear = Year(Now)
GetTimeZoneInformation udtTZI
FirstDateDaylight = GetTimezoneChangeDate(udtTZI.DaylightDate, InYear)
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Function</span>
&nbsp;
<span class="TOKEN">Public Function</span> FirstDateStandard(<span class="TOKEN">Optional</span> <span class="TOKEN">ByVal</span> InYear <span class="TOKEN">As</span> <span class="TOKEN">Long</span>) <span class="TOKEN">As</span> Date
<span class="REM">'// Gibt das Startdatum der Standardzeit zur&uuml;ck.</span>
<span class="TOKEN">Dim</span> udtTZI <span class="TOKEN">As</span> TIME_ZONE_INFORMATION
<span class="TOKEN">If</span> InYear = 0 <span class="TOKEN">Then</span> InYear = Year(Now)
GetTimeZoneInformation udtTZI
FirstDateStandard = GetTimezoneChangeDate(udtTZI.StandardDate, InYear)
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Function</span>
&nbsp;
<span class="TOKEN">Function</span> GetTimezoneChangeDate(Data <span class="TOKEN">As</span> SYSTEMTIME, InYear <span class="TOKEN">As</span> <span class="TOKEN">Long</span>) <span class="TOKEN">As</span> Date
<span class="TOKEN">Dim</span> dtTemp <span class="TOKEN">As</span> Date
<span class="TOKEN">Dim</span> lMonthFirstWeekday <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="REM">'// In Data.wDayOfWeek wird ein Wochentag &uuml;bergeben. Die Information in .wDay</span>
<span class="REM">'// legt fest, in welcher Woche des Monats der betroffene Tag zu ermitteln</span>
<span class="REM">'// ist (1-4) bzw., dass der letzte gleiche Wochentag des Monats gemeint ist.</span>
<span class="TOKEN">With</span> Data
Select Case .wDay <span class="REM">'// Gibt die Woche im Monat an</span>
Case 1 <span class="TOKEN">To</span> 4 <span class="REM">'// Wochentag in 1.-4. Woche im Monat</span>
<span class="REM"> '// Wochentag des ersten Tages im Monat berechnen</span>
lMonthFirstWeekday = Weekday(DateSerial(InYear, .wMonth, 1))
<span class="REM"> '// Den gesuchten Tag ermitteln</span>
GetTimezoneChangeDate = _
DateSerial(InYear, .wMonth, _
.wDayOfWeek - lMonthFirstWeekday + .wDay * 7 + 1) + _
TimeSerial(.wHour, .wMinute, .wSecond)
Case 5 <span class="REM">' letzter Wochentag im Monat</span>
<span class="REM"> '// Letzten Tag des Monats berechnen</span>
dtTemp = DateSerial(InYear, .wMonth + 1, 0)
<span class="REM"> '// Zum letzten passenden Wochentag dieses Monats rechnen</span>
GetTimezoneChangeDate = _
dtTemp + vbSunday - Weekday(dtTemp) + _
TimeSerial(.wHour, .wMinute, .wSecond)
<span class="TOKEN">End</span> Select
<span class="TOKEN">End</span> <span class="TOKEN">With</span>
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Function</span></pre></div>
Code eingefügt mit dem MOF Code Converter (http://www.ms-office-forum.net/forum/codeconverter.php)

eddie
11.12.2006, 13:26
Danke für den Skript.

Ich habe aber noch eine Frage:
Soviel ich hier herauslese kann man nur die Daten für die dzt. eingestellte Zeitzone herauslesen.

Gibt es eine Möglichkeit, die Daten ALLER Zeitzonen auszulesen?

Da man ja die Zeitzone nachträglich ändern kann, müssen die Informationen irgendwo abgespeichert sein. Eventuell kann man diese Auslesen und in eine Tabelle abstellen.