1

What are the API provided in the Compact Framework to read the time (NOT in UTC!!) on a windows mobile based device?

I was able to get the device time in UTC with this code:

DateTime dt = DateLib.Now;            
SystemTime sysTime = new SystemTime();
Windows.SystemTime.GetSystemTime(out sysTime);
Windows.SystemTime.ConvertTimeStructure(ref sysTime, out dt);
Windows.SystemTime.SetSystemTime(dt);

But I want the time as per the device's time zone. Thanks

Neeraj
  • 592
  • 4
  • 6
  • 21

2 Answers2

1

Why aren't you simply using the built-in, supported DateTime.Now property?

EDIT

The Compact Framework caches timezone information on load, so any call to change the time zone after an app has loaded will not be reflected in a call to DateTime.Now.

Right now I assume you're P/Invoking GetSystemTime, instead you should be P/Invoking GetLocalTime.

ctacke
  • 66,480
  • 18
  • 94
  • 155
  • So, what is happening is this: Suppose the device is on Pacific Standard Time (PST) and I launch my app and login. I get the correct time. Now I logout and change the Time Zone to say Central Standard Time. I login and hope that the time (DateTime.Now) would give the time as per the time in the clock & alarm application on the device (which is CST) but the time I get is still in PST. So, I decided to update the time on device before loging into my app. So, I'd first read the device time and then set it. But I do not know the right API. – Neeraj Jul 28 '11 at 21:59
  • DateLib.Now is just a wrapper around DateTime.Now and it chops the seconds. – Neeraj Jul 28 '11 at 22:04
1

You can use DateTime.UtcNow to get the UTC Time. Get the current utc offset TimeZone

GetUtcOffset in order to get a TimeSpan.

http://msdn.microsoft.com/en-us/library/system.timezone.getutcoffset.aspx

Then you can get LocalTime = DateTime.UtcNow + TimeZone.GetUtcOffset

R Quijano
  • 1,301
  • 9
  • 10