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:

  1. 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.
  2. DST may or may not be right due to congress changing it, so a QFE may be required by the OEM
  3. DST may be on or off in the registry
  4. The CF caches timezone bias as startup, so any adjustment of the timezone renders DateTime.Now incorrect until you restart your app
  5. Not all devices can persist time across a power loss (or even a reset)
  6. 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)?

Need Your Help

Barcode decoder for desktops


I am looking for a barcode scanner for a desktop application. We have over 2000 images with generated per month which need to be processed to decode barcode. Now most of these might or might not co...

Impementing Vertical Scroll with horizontal swipe in Android

android vertical-scrolling swipe-gesture

I am trying for swiping in Android using OnGestureListener.I need vertical Scrolling of text if it exceeds the layout height.I need Verical scroll and horizontal swipe should work together.