p/invoke GetSystemTime() method is giving wrong output
I used p/invoke GetSystemTime() method in my application to get the current system date time but it is giving wrong values any solution for this..
Ah, time and the CF and WinCE. What fun! Along with all the other fine answers you've received there are other things to know:
- The OS stores LocalTime, not UTC so GetSystemTime ends up getting LocalTime and them adjusting that backwards based on your timezone and DST, so if the local time is right but SystemTime is not, then you have a TZ or DST setting wrong.
- DST may or may not be right due to congress changing it, so a QFE may be required by the OEM
- DST may be on or off in the registry
- The CF caches timezone bias as startup, so any adjustment of the timezone renders DateTime.Now incorrect until you restart your app
- Not all devices can persist time across a power loss (or even a reset)
- Time will "float" throughout the day. how badly (milliseconds to seconds) depends on the actual hardware implementation
Why don't you use DateTime.Now ?
What is the problem?
Is your p/invoke signature correct?
Is you struct laid out correctly?
How are you dealing with the struct pointer being 'returned' ?
If the time returned is off by one hour, then you're running into a Daylight Savings Time bug (which can be fixed with a hotfix).
GetSystemTime returns the Coordinated Universal Time (UTC). You may be looking for just the local time, in which case you want to call GetLocalTime instead (or just use DateTime.Now or DateTime.UtcNow, and skip the PInvoke stuff).
What do you mean by wrong values? Since you are asking about Windows CE, it might be that your system does not save the RTC and does not sync on boot resulting in not having the correct time at all. This is platform specific. Is the time and date correct in the taskbar (assuming you have that in the image)?