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
.