From: Ronny on
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
news:C818939E-387E-4E0B-AFBF-FA94CA7C148B(a)microsoft.com...

Previous Posts In This Thread:

On Wednesday, December 03, 2008 1:21 PM
RFlaughe wrote:

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;
}
}
}

On Wednesday, December 03, 2008 1:31 PM
Paul G. Tobey [eMVP] wrote:

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.

"RFlaugher" <RFlaugher(a)discussions.microsoft.com> wrote in message
news:6AA3CB29-19C4-49AC-903D-2E0FB099A0EE(a)microsoft.com...

On Wednesday, December 03, 2008 1:47 PM
Chris Tacke, eMVP wrote:

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


"RFlaugher" <RFlaugher(a)discussions.microsoft.com> wrote in message
news:6AA3CB29-19C4-49AC-903D-2E0FB099A0EE(a)microsoft.com...

On Thursday, December 04, 2008 4:13 AM
DateTime convert Function wrote:

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.

On Thursday, December 04, 2008 9:43 AM
Peter Foot [MVP] wrote:

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
news:C818939E-387E-4E0B-AFBF-FA94CA7C148B(a)microsoft.com...


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/tutorials/aspnet/7390a840-dd39-4c35-9940-c7354940d878/a-framework-to-animate-wp.aspx
From: Paul G. Tobey [eMVP] paultobey _at_ earthlink _dot_ on
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" wrote:

> 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
> news:C818939E-387E-4E0B-AFBF-FA94CA7C148B(a)microsoft.com...
>
> Previous Posts In This Thread:
>
> On Wednesday, December 03, 2008 1:21 PM
> RFlaughe wrote:
>
> 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;
> }
> }
> }
>
> On Wednesday, December 03, 2008 1:31 PM
> Paul G. Tobey [eMVP] wrote:
>
> 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.
>
> "RFlaugher" <RFlaugher(a)discussions.microsoft.com> wrote in message
> news:6AA3CB29-19C4-49AC-903D-2E0FB099A0EE(a)microsoft.com...
>
> On Wednesday, December 03, 2008 1:47 PM
> Chris Tacke, eMVP wrote:
>
> 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
>
>
> "RFlaugher" <RFlaugher(a)discussions.microsoft.com> wrote in message
> news:6AA3CB29-19C4-49AC-903D-2E0FB099A0EE(a)microsoft.com...
>
> On Thursday, December 04, 2008 4:13 AM
> DateTime convert Function wrote:
>
> 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.
>
> On Thursday, December 04, 2008 9:43 AM
> Peter Foot [MVP] wrote:
>
> 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
> news:C818939E-387E-4E0B-AFBF-FA94CA7C148B(a)microsoft.com...
>
>
> 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/tutorials/aspnet/7390a840-dd39-4c35-9940-c7354940d878/a-framework-to-animate-wp.aspx
> .
>