P/Invoking SetTimezoneInformation() causes DateTime.Now to return an incorrect local time

Author:

Christopher Tacke MVP

COMMUNITY SOLUTIONS CONTENT DISCLAIMER

MICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES.

SUMMARY

Modifying the device's Timezone Bias through Platform Invocation makes all subsequent calls to DateTime.Now within the same process incorrect.

SYMPTOMS

After setting the TImezone bias by P/Invoking SetTimezoneInformation, DateTime.Now returns a different result than P/Invoking the GetLocalTime() API

CAUSE

The compact framework execution engine (EE) loads the TimeZoneInformation into the AppDomain during creation.  P/Invoking SetTimezoneInformation() modifies the devices clock, but the EE cache is unaware of the change and does not take modifications into account.

 

RESOLUTION

Do not use DateTime.Now inside any application or library that either modifies the timezone bias or that may be affected by other processes modifying the timezone bias. 
 
For reliability P/Invoke either GetSystemTime() or GetLocalTime() as follows:
 
[ C# ]

[DllImport("coredll", EntryPoint="GetSystemTime", SetLastError=true)]
internal static extern void GetSystemTime(out SYSTEMTIME st);
 
[DllImport("coredll", EntryPoint="GetLocalTime", SetLastError=true)]
internal static extern void GetLocalTime(out SYSTEMTIME st);
 
[StructLayout(LayoutKind.Sequential)]
public class SYSTEMTIME
{
    public short wYear;
    public short wMonth;
    public short wDayOfWeek;
    public short wDay;
    public short wHour;
    public short wMinute;
    public short wSecond;
    public short wMilliseconds;
}

 
[ VB ]

    Private Declare Sub GetLocalTime Lib "coredll.dll" (<Out()> ByVal st As SYSTEMTIME)

    Private Declare Sub GetSystemTime Lib "coredll.dll" (<Out()> ByVal st As SYSTEMTIME)

    <StructLayout(LayoutKind.Sequential)> _
    Public Class SYSTEMTIME
        Public wYear As Short
        Public wMonth As Short
        Public wDayOfWeek As Short
        Public wDay As Short
        Public wHour As Short
        Public wMinute As Short
        Public wSecond As Short
        Public wMilliseconds As Short
    End Class

Properties

Article ID: 555035 - Last Review: 14 Feb 2017 - Revision: 1

Microsoft .NET Compact Framework

Feedback