为什么outlook.timezones.converttime错误地更改了夏令时



我在Excel中使用以下VBA代码:

Function tzAtotzB(srcDT As Date, srcTZ As String, dstTZ As String) As Date
Dim OutlookApp As Object
Dim TZones As TimeZones
Dim convertedTime As Date
Dim sourceTZ As TimeZone
Dim destTZ As TimeZone
Dim secNum As Integer

Set OutlookApp = CreateObject("Outlook.Application")
Set TZones = OutlookApp.TimeZones
Set destTZ = TZones.Item(dstTZ)
Set sourceTZ = TZones.Item(srcTZ)

tzAtotzB = TZones.ConvertTime(srcDT, sourceTZ, destTZ)
End Function
Function CETtoUTC(srcDT As Date) As Date
CETtoUTC = tzAtotzB(srcDT, "Central Europe Standard Time", "UTC")
End Function
Function UTCtoCET(srcDT As Date) As Date
UTCtoCET = tzAtotzB(srcDT, "UTC", "Central Europe Standard Time")
End Function

由于某些原因,结果不是人们所期望的:

Debug.Print UTCtoCET("26/03/2022 23:00:00")
27/03/2022
Debug.Print UTCtoCET("27/03/2022 00:00:00")
27/03/2022 02:00:00 
Debug.Print CETtoUTC("27/03/2022 02:00:00")
27/03/2022 
Debug.Print UTCtoCET("28/03/2021 00:00:00")
28/03/2021 02:00:00 

CET应该从3月最后一个星期日的01:00 UTC开始从UTC+1切换到UTC+2(来源(,但上面的输出显示在00:00 UTC从UTC+1切换到UTC+2,这是完全不正确的。

我没有发现我的代码有任何错误。这是微软代码库中已知的错误吗?

这似乎是Outlook VBA对象模型中TimeZones.ConvertTime方法的底层实现中的一个错误。

使用您问题中的代码,我能够在引用";Microsoft Outlook 16.0对象库";。OutlookApplication.Version16.0.0.14931

正确的结果在同一台Windows计算机上运行的.NET应用程序中使用.NETTimeZoneInfo对象上的转换方法得出。这使用了Windows注册表中的时区数据,据我所知,这与Outlook VBA库使用的数据相同。

以下是展示正确结果的.NET C#代码:

var zone = TimeZoneInfo.FindSystemTimeZoneById("Central Europe Standard Time");
var input1 = DateTimeOffset.Parse("2022-03-27T00:00:00Z");
var output1 = TimeZoneInfo.ConvertTime(input1, zone);
Console.WriteLine($"{input1.UtcDateTime:yyyy-MM-dd'T'HH:mm:ssK} => {output1:yyyy-MM-dd'T'HH:mm:sszzz}");
var input2 = DateTimeOffset.Parse("2022-03-27T01:00:00Z");
var output2 = TimeZoneInfo.ConvertTime(input2, zone);
Console.WriteLine($"{input2.UtcDateTime:yyyy-MM-dd'T'HH:mm:ssK} => {output2:yyyy-MM-dd'T'HH:mm:sszzz}");

哪个输出:

2022-03-27T00:00:00Z => 2022-03-27T01:00:00+01:00
2022-03-27T01:00:00Z => 2022-03-27T03:00:00+02:00

这显示了正确的转换,因此数据是正确的。因此,问题必须是Outlook VBA实现特有的。如果这对你很重要,我建议你向微软提出支持问题。你可以参考这个答案。

最新更新