DateTime.Now returns incorrect time after time zone change

R

RFlaugher

I am using CE 5.0 with CF 2.0 SP1. I noticed that the DateTime.Now function
does not seem to work correctly after a time zone change via a call to
SetTimeZoneInformation while the GetLocalTime Win32 function does work
correctly. Not only does the DateTime function change its value after a time
zone change but further calls to SetLocalTime are ignored by it. Is there a
fix for this? Right now I have had to write my own version:

public static class MyDateTime
{
public static DateTime Now
{
get
{
Win32.SYSTEMTIME time;

// Get local time
Win32.GetLocalTime(out time);

// Convert to DateTime
DateTime now = new DateTime(time.wYear, time.wMonth,
time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);

return now;
}
}
}
 
P

Paul G. Tobey [eMVP]

I seem to remember this from years ago. The time zone is only grabbed by
the .NET CF run-time on startup of the program, so, if you change that time
zone, the local time you get from Now() will be wrong (because it's being
calculated from GMT using the time zone that used to be set, when the
program started). You could use GetLocalTime (native), directly, and you
should always get what matches the Date/Time Control Panel applet. I'm sure
there's a lot in the archives about this...

Paul T.
 
C

Chris Tacke, eMVP

That's a well-known behavior of the CF. If you set the timezone, you need
to call GetLocalTime directly to get your time.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
 
D

DateTime convert Function

I am getting 2 error while using this class

Error 1 The type or namespace name 'SYSTEMTIME' does not exist in the
namespace 'Microsoft.Win32' (are you missing an assembly reference?)

Error 2 The name 'Micorsoft' does not exist in the current context

Please let me know how to use this method using win32


other method to written this code

public static class MyDateTime
{

[StructLayout(LayoutKind.Sequential)]
private struct SYSTEMTIME
{
public short Year;
public short Month;
public short DayOfWeek;
public short Day;
public short Hour;
public short Minute;
public short Second;
public short Milliseconds;
}

[DllImport("coredll.dll")]
private static extern bool SetSystemTime(ref SYSTEMTIME time);

[DllImport("coredll.dll", EntryPoint = "GetLocalTime", SetLastError
= true)]
internal static extern void GetLocalTime(out SYSTEMTIME st);

public static DateTime Now
{
get
{
SYSTEMTIME time;
// Microsoft.Win32.SYSTEMTIME time;
SetSystemTime(time);

// Get local time
GetLocalTime(out time);
// Micorsoft.Win32.GetLocalTime(out time);

// Convert to DateTime
DateTime now = new DateTime(time.wYear, time.wMonth,
time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);

return now;
}
}
}

method also written error

Error 1 Inconsistent accessibility: parameter type 'out

Please let us know where i did wrong.
 
P

Peter Foot [MVP]

Not sure why you are calling SetSystemTime - you should just be calling
GetLocalTime and converting the value to a DateTime e.g.

SYSTEMTIME time;
// Get local time
GetLocalTime(out time);
// Convert to DateTime
DateTime now = new DateTime(time.wYear, time.wMonth,
time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);

return now;

Because SYSTEMTIME is declared as private then so should the GetLocalTime
P/Invoke (or they should both be marked as internal).

Peter

--
Peter Foot
Microsoft Device Application Development MVP
peterfoot.net | appamundi.com | inthehand.com
APPA Mundi Ltd - software solutions for a mobile world
In The Hand Ltd - .NET Components for Mobility

"DateTime convert Function" <DateTime convert Function
@discussions.microsoft.com> wrote in message
news:[email protected]...
 
R

Ronny Gydar

I have an application that shows flightinformation, and if the user change timezone on their phone while my application is running (can happen automatically on some newer phones), my app still shows localtimes as they where before the change, and they need to restart my app to get correct localtimes.

All times in my app are in UTC, and recalculated on the fly when they are shown.

Just wanted to add that this issue is also occuring when timezone is changed outside of the app, so it seems all apps needs to consider this bug, and never use DateTime.Now...?

Is there no way to just "refresh" the app around this, so that I do not need to change all calls against DateTime.Now in my code.

I use DateTime.Now, DateTime.UtcNow "somedatetime".ToLocalTime() so forth a lot in my app. Never occured to me that those would not be "safe" to use.



Peter Foot [MVP] wrote:

Not sure why you are calling SetSystemTime - you should just be calling
04-Dec-08

Not sure why you are calling SetSystemTime - you should just be calling
GetLocalTime and converting the value to a DateTime e.g

SYSTEMTIME time
// Get local tim
GetLocalTime(out time)
// Convert to DateTim
DateTime now = new DateTime(time.wYear, time.wMonth
time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds)

return now

Because SYSTEMTIME is declared as private then so should the GetLocalTime
P/Invoke (or they should both be marked as internal)

Pete

--
Peter Foo
Microsoft Device Application Development MV
peterfoot.net | appamundi.com | inthehand.co
APPA Mundi Ltd - software solutions for a mobile worl
In The Hand Ltd - .NET Components for Mobilit

"DateTime convert Function" <DateTime convert Function
@discussions.microsoft.com> wrote in message

Previous Posts In This Thread:

DateTime.Now returns incorrect time after time zone change
I am using CE 5.0 with CF 2.0 SP1. I noticed that the DateTime.Now function
does not seem to work correctly after a time zone change via a call to
SetTimeZoneInformation while the GetLocalTime Win32 function does work
correctly. Not only does the DateTime function change its value after a time
zone change but further calls to SetLocalTime are ignored by it. Is there a
fix for this? Right now I have had to write my own version:

public static class MyDateTim

public static DateTime No

ge

Win32.SYSTEMTIME time

// Get local tim
Win32.GetLocalTime(out time)

// Convert to DateTim
DateTime now = new DateTime(time.wYear, time.wMonth,
time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds)

return now


}

I seem to remember this from years ago. The time zone is only grabbed by the .
I seem to remember this from years ago. The time zone is only grabbed by
the .NET CF run-time on startup of the program, so, if you change that time
zone, the local time you get from Now() will be wrong (because it's being
calculated from GMT using the time zone that used to be set, when the
program started). You could use GetLocalTime (native), directly, and you
should always get what matches the Date/Time Control Panel applet. I'm sure
there's a lot in the archives about this..

Paul T


That's a well-known behavior of the CF.
That's a well-known behavior of the CF. If you set the timezone, you need
to call GetLocalTime directly to get your time

--

Chris Tacke, Embedded MV
OpenNETCF Consultin
Giving back to the embedded communit
http://community.OpenNETCF.co


I am getting 2 error while using this class Error 1 The type or namespace name
I am getting 2 error while using this class

Error 1 The type or namespace name 'SYSTEMTIME' does not exist in the
namespace 'Microsoft.Win32' (are you missing an assembly reference?)

Error 2 The name 'Micorsoft' does not exist in the current context

Please let me know how to use this method using win32


other method to written this code

public static class MyDateTime
{

[StructLayout(LayoutKind.Sequential)]
private struct SYSTEMTIME
{
public short Year;
public short Month;
public short DayOfWeek;
public short Day;
public short Hour;
public short Minute;
public short Second;
public short Milliseconds;
}

[DllImport("coredll.dll")]
private static extern bool SetSystemTime(ref SYSTEMTIME time);

[DllImport("coredll.dll", EntryPoint = "GetLocalTime", SetLastError
= true)]
internal static extern void GetLocalTime(out SYSTEMTIME st);

public static DateTime Now
{
get
{
SYSTEMTIME time;
// Microsoft.Win32.SYSTEMTIME time;
SetSystemTime(time);

// Get local time
GetLocalTime(out time);
// Micorsoft.Win32.GetLocalTime(out time);

// Convert to DateTime
DateTime now = new DateTime(time.wYear, time.wMonth,
time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);

return now;
}
}
}

method also written error

Error 1 Inconsistent accessibility: parameter type 'out

Please let us know where i did wrong.

Not sure why you are calling SetSystemTime - you should just be calling
Not sure why you are calling SetSystemTime - you should just be calling
GetLocalTime and converting the value to a DateTime e.g.

SYSTEMTIME time;
// Get local time
GetLocalTime(out time);
// Convert to DateTime
DateTime now = new DateTime(time.wYear, time.wMonth,
time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);

return now;

Because SYSTEMTIME is declared as private then so should the GetLocalTime
P/Invoke (or they should both be marked as internal).

Peter

--
Peter Foot
Microsoft Device Application Development MVP
peterfoot.net | appamundi.com | inthehand.com
APPA Mundi Ltd - software solutions for a mobile world
In The Hand Ltd - .NET Components for Mobility

"DateTime convert Function" <DateTime convert Function
@discussions.microsoft.com> wrote in message


Submitted via EggHeadCafe - Software Developer Portal of Choice
A Framework to Animate WPF and Silverlight Pages Similar to the PowerPoint Slides
http://www.eggheadcafe.com/tutorial...0-c7354940d878/a-framework-to-animate-wp.aspx
 
R

Ronny Gydar

I have an application that shows flightinformation, and if the user change timezone on their phone while my application is running (can happen automatically on some newer phones), my app still shows localtimes as they where before the change, and they need to restart my app to get correct localtimes.

All times in my app are in UTC, and recalculated on the fly when they are shown.

Just wanted to add that this issue is also occuring when timezone is changed outside of the app, so it seems all apps needs to consider this bug, and never use DateTime.Now...?

Is there no way to just "refresh" the app around this, so that I do not need to change all calls against DateTime.Now in my code.

I use DateTime.Now, DateTime.UtcNow "somedatetime".ToLocalTime() so



Peter Foot [MVP] wrote:

Not sure why you are calling SetSystemTime - you should just be calling
04-Dec-08

Not sure why you are calling SetSystemTime - you should just be calling
GetLocalTime and converting the value to a DateTime e.g

SYSTEMTIME time
// Get local tim
GetLocalTime(out time)
// Convert to DateTim
DateTime now = new DateTime(time.wYear, time.wMonth
time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds)

return now

Because SYSTEMTIME is declared as private then so should the GetLocalTime
P/Invoke (or they should both be marked as internal)

Pete

--
Peter Foo
Microsoft Device Application Development MV
peterfoot.net | appamundi.com | inthehand.co
APPA Mundi Ltd - software solutions for a mobile worl
In The Hand Ltd - .NET Components for Mobilit

"DateTime convert Function" <DateTime convert Function
@discussions.microsoft.com> wrote in message

Previous Posts In This Thread:

DateTime.Now returns incorrect time after time zone change
I am using CE 5.0 with CF 2.0 SP1. I noticed that the DateTime.Now function
does not seem to work correctly after a time zone change via a call to
SetTimeZoneInformation while the GetLocalTime Win32 function does work
correctly. Not only does the DateTime function change its value after a time
zone change but further calls to SetLocalTime are ignored by it. Is there a
fix for this? Right now I have had to write my own version:

public static class MyDateTim

public static DateTime No

ge

Win32.SYSTEMTIME time

// Get local tim
Win32.GetLocalTime(out time)

// Convert to DateTim
DateTime now = new DateTime(time.wYear, time.wMonth,
time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds)

return now


}

I seem to remember this from years ago. The time zone is only grabbed by the .
I seem to remember this from years ago. The time zone is only grabbed by
the .NET CF run-time on startup of the program, so, if you change that time
zone, the local time you get from Now() will be wrong (because it's being
calculated from GMT using the time zone that used to be set, when the
program started). You could use GetLocalTime (native), directly, and you
should always get what matches the Date/Time Control Panel applet. I'm sure
there's a lot in the archives about this..

Paul T


That's a well-known behavior of the CF.
That's a well-known behavior of the CF. If you set the timezone, you need
to call GetLocalTime directly to get your time

--

Chris Tacke, Embedded MV
OpenNETCF Consultin
Giving back to the embedded communit
http://community.OpenNETCF.co


I am getting 2 error while using this class Error 1 The type or namespace name
I am getting 2 error while using this class

Error 1 The type or namespace name 'SYSTEMTIME' does not exist in the
namespace 'Microsoft.Win32' (are you missing an assembly reference?)

Error 2 The name 'Micorsoft' does not exist in the current context

Please let me know how to use this method using win32


other method to written this code

public static class MyDateTime
{

[StructLayout(LayoutKind.Sequential)]
private struct SYSTEMTIME
{
public short Year;
public short Month;
public short DayOfWeek;
public short Day;
public short Hour;
public short Minute;
public short Second;
public short Milliseconds;
}

[DllImport("coredll.dll")]
private static extern bool SetSystemTime(ref SYSTEMTIME time);

[DllImport("coredll.dll", EntryPoint = "GetLocalTime", SetLastError
= true)]
internal static extern void GetLocalTime(out SYSTEMTIME st);

public static DateTime Now
{
get
{
SYSTEMTIME time;
// Microsoft.Win32.SYSTEMTIME time;
SetSystemTime(time);

// Get local time
GetLocalTime(out time);
// Micorsoft.Win32.GetLocalTime(out time);

// Convert to DateTime
DateTime now = new DateTime(time.wYear, time.wMonth,
time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);

return now;
}
}
}

method also written error

Error 1 Inconsistent accessibility: parameter type 'out

Please let us know where i did wrong.

Not sure why you are calling SetSystemTime - you should just be calling
Not sure why you are calling SetSystemTime - you should just be calling
GetLocalTime and converting the value to a DateTime e.g.

SYSTEMTIME time;
// Get local time
GetLocalTime(out time);
// Convert to DateTime
DateTime now = new DateTime(time.wYear, time.wMonth,
time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);

return now;

Because SYSTEMTIME is declared as private then so should the GetLocalTime
P/Invoke (or they should both be marked as internal).

Peter

--
Peter Foot
Microsoft Device Application Development MVP
peterfoot.net | appamundi.com | inthehand.com
APPA Mundi Ltd - software solutions for a mobile world
In The Hand Ltd - .NET Components for Mobility

"DateTime convert Function" <DateTime convert Function
@discussions.microsoft.com> wrote in message

Same problem when timezone is changed outside the application
I have an application that shows flightinformation, and if the user change timezone on their phone while my application is running (can happen automatically on some newer phones), my app still shows localtimes as they where before the change, and they need to restart my app to get correct localtimes.

All times in my app are in UTC, and recalculated on the fly when they are shown.

Just wanted to add that this issue is also occuring when timezone is changed outside of the app, so it seems all apps needs to consider this bug, and never use DateTime.Now...?

Is there no way to just "refresh" the app around this, so that I do not need to change all calls against DateTime.Now in my code.

I use DateTime.Now, DateTime.UtcNow "somedatetime".ToLocalTime() so forth a lot in my app. Never occured to me that those would not be "safe" to use.


Submitted via EggHeadCafe - Software Developer Portal of Choice
Using VSTO Add-In To Automate Frequent Excel 2007 Tasks
http://www.eggheadcafe.com/tutorial...60-39a86ccab5d6/using-vsto-addin-to-auto.aspx
 
R

Ronny Gydar

Sorry about double-post...trying to add account and posting at same time...not always successful for people who type faster than they think! ;)



Ronny Gydar wrote:

Same problem when timezone is changed outside the application
12-Apr-10

I have an application that shows flightinformation, and if the user change timezone on their phone while my application is running (can happen automatically on some newer phones), my app still shows localtimes as they where before the change, and they need to restart my app to get correct localtimes.

All times in my app are in UTC, and recalculated on the fly when they are shown.

Just wanted to add that this issue is also occuring when timezone is changed outside of the app, so it seems all apps needs to consider this bug, and never use DateTime.Now...?

Is there no way to just "refresh" the app around this, so that I do not need to change all calls against DateTime.Now in my code.

I use DateTime.Now, DateTime.UtcNow "somedatetime".ToLocalTime() so

Previous Posts In This Thread:


Submitted via EggHeadCafe - Software Developer Portal of Choice
WPF Reflection Effect
http://www.eggheadcafe.com/tutorial...-beab-49bd76e20b9b/wpf-reflection-effect.aspx
 
P

Paul G. Tobey [eMVP]

This is a known 'feature' of the .NET CF date/time support. You can use
GetTimeZoneInformation(), via P/Invoke, if you need to get
dynamically-updated time zone information. It's wrapped in OpenNETCF Smart
Device Framework.

Of course, you can also arrange to be notified when time zone is changed.
See CeRunAppAtEvent()/NOTIFICATION_EVENT_TZ_CHANGE.

Paul T.

Ronny Gydar said:
I have an application that shows flightinformation, and if the user change timezone on their phone while my application is running (can happen automatically on some newer phones), my app still shows localtimes as they where before the change, and they need to restart my app to get correct localtimes.

All times in my app are in UTC, and recalculated on the fly when they are shown.

Just wanted to add that this issue is also occuring when timezone is changed outside of the app, so it seems all apps needs to consider this bug, and never use DateTime.Now...?

Is there no way to just "refresh" the app around this, so that I do not need to change all calls against DateTime.Now in my code.

I use DateTime.Now, DateTime.UtcNow "somedatetime".ToLocalTime() so forth a lot in my app. Never occured to me that those would not be "safe" to use.



Peter Foot [MVP] wrote:

Not sure why you are calling SetSystemTime - you should just be calling
04-Dec-08

Not sure why you are calling SetSystemTime - you should just be calling
GetLocalTime and converting the value to a DateTime e.g.

SYSTEMTIME time;
// Get local time
GetLocalTime(out time);
// Convert to DateTime
DateTime now = new DateTime(time.wYear, time.wMonth,
time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);

return now;

Because SYSTEMTIME is declared as private then so should the GetLocalTime
P/Invoke (or they should both be marked as internal).

Peter

--
Peter Foot
Microsoft Device Application Development MVP
peterfoot.net | appamundi.com | inthehand.com
APPA Mundi Ltd - software solutions for a mobile world
In The Hand Ltd - .NET Components for Mobility

"DateTime convert Function" <DateTime convert Function
@discussions.microsoft.com> wrote in message

Previous Posts In This Thread:

DateTime.Now returns incorrect time after time zone change
I am using CE 5.0 with CF 2.0 SP1. I noticed that the DateTime.Now function
does not seem to work correctly after a time zone change via a call to
SetTimeZoneInformation while the GetLocalTime Win32 function does work
correctly. Not only does the DateTime function change its value after a time
zone change but further calls to SetLocalTime are ignored by it. Is there a
fix for this? Right now I have had to write my own version:

public static class MyDateTime
{
public static DateTime Now
{
get
{
Win32.SYSTEMTIME time;

// Get local time
Win32.GetLocalTime(out time);

// Convert to DateTime
DateTime now = new DateTime(time.wYear, time.wMonth,
time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);

return now;
}
}
}

I seem to remember this from years ago. The time zone is only grabbed by the .
I seem to remember this from years ago. The time zone is only grabbed by
the .NET CF run-time on startup of the program, so, if you change that time
zone, the local time you get from Now() will be wrong (because it's being
calculated from GMT using the time zone that used to be set, when the
program started). You could use GetLocalTime (native), directly, and you
should always get what matches the Date/Time Control Panel applet. I'm sure
there's a lot in the archives about this...

Paul T.


That's a well-known behavior of the CF.
That's a well-known behavior of the CF. If you set the timezone, you need
to call GetLocalTime directly to get your time.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com



I am getting 2 error while using this class Error 1 The type or namespace name
I am getting 2 error while using this class

Error 1 The type or namespace name 'SYSTEMTIME' does not exist in the
namespace 'Microsoft.Win32' (are you missing an assembly reference?)

Error 2 The name 'Micorsoft' does not exist in the current context

Please let me know how to use this method using win32


other method to written this code

public static class MyDateTime
{

[StructLayout(LayoutKind.Sequential)]
private struct SYSTEMTIME
{
public short Year;
public short Month;
public short DayOfWeek;
public short Day;
public short Hour;
public short Minute;
public short Second;
public short Milliseconds;
}

[DllImport("coredll.dll")]
private static extern bool SetSystemTime(ref SYSTEMTIME time);

[DllImport("coredll.dll", EntryPoint = "GetLocalTime", SetLastError
= true)]
internal static extern void GetLocalTime(out SYSTEMTIME st);

public static DateTime Now
{
get
{
SYSTEMTIME time;
// Microsoft.Win32.SYSTEMTIME time;
SetSystemTime(time);

// Get local time
GetLocalTime(out time);
// Micorsoft.Win32.GetLocalTime(out time);

// Convert to DateTime
DateTime now = new DateTime(time.wYear, time.wMonth,
time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);

return now;
}
}
}

method also written error

Error 1 Inconsistent accessibility: parameter type 'out

Please let us know where i did wrong.

Not sure why you are calling SetSystemTime - you should just be calling
Not sure why you are calling SetSystemTime - you should just be calling
GetLocalTime and converting the value to a DateTime e.g.

SYSTEMTIME time;
// Get local time
GetLocalTime(out time);
// Convert to DateTime
DateTime now = new DateTime(time.wYear, time.wMonth,
time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);

return now;

Because SYSTEMTIME is declared as private then so should the GetLocalTime
P/Invoke (or they should both be marked as internal).

Peter

--
Peter Foot
Microsoft Device Application Development MVP
peterfoot.net | appamundi.com | inthehand.com
APPA Mundi Ltd - software solutions for a mobile world
In The Hand Ltd - .NET Components for Mobility

"DateTime convert Function" <DateTime convert Function
@discussions.microsoft.com> wrote in message


Submitted via EggHeadCafe - Software Developer Portal of Choice
A Framework to Animate WPF and Silverlight Pages Similar to the PowerPoint Slides
http://www.eggheadcafe.com/tutorial...0-c7354940d878/a-framework-to-animate-wp.aspx
.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top