Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Setting the timezone

103 views
Skip to first unread message

Jeff Samuels

unread,
Jun 27, 2003, 3:28:25 PM6/27/03
to
I am working on a PocketPC (version 3.0) project. I am
developing in C# using the Visual Studio .NET 2003 and
using the compact framework.

I have a need to be able to set the time and the timezone
on the device. I can set the time but have not been able
to set the timezone. Is there a easy way to this.

When I PInvoke SetTimeZoneInformation I get an Unsupported
exception. GetTimeZoneInformation seems to work OK.

On a related note: I have observed that I can launch the
clock.exe (using ShellExecuteEx) from my app and I am able
to set the time and timezone there, but my app only picks
up the time change. The timezone is not updated until I
stop and restart my application.

Is this a known thing and is there a work around without
having to stop and restart the application.

Alex Feinman [MVP]

unread,
Jun 27, 2003, 3:44:26 PM6/27/03
to
Setting timezone is supported by OpenNETCF library:
http://www.opennetcf.org/WinAPI.asp
That would help you with the NotSupportedException which always a result of
incorrect P/Invoke definition.
As for making your program aware of the Time Zone changes, I suspect you
might need to monitor WM_SETTINGCHANGE message...

"Jeff Samuels" <samuels...@emeryworld.com> wrote in message
news:637d01c33ce2$477792c0$3101...@phx.gbl...

Jeff Samuels

unread,
Jun 27, 2003, 4:25:00 PM6/27/03
to
Alex,

Thanks for the response.

Just had a look at the OpenNETCF library. I looked at the
Core.TIME_ZONE_INFORMATION class and the Core.TX_REG class
and I did not see a SetTimeZoneInformation (or similar)
methods for either of these. Am I missing something?

As for the other... It is not that my app needs to be
aware of a change to the timezone. Rather it is that when
the timezone does change the DateTime returned by
DateTime.Now (which returns the local time) does not show
the effect of the change. For example say it is 12:00pm
PDT and the tz is set to "Pacific". If I start my app and
subquently launch "clock" and change the tz to "Eastern".
I would expect DateTime.Now to return 3:00pm EDT. What I
get is 12:00pm PDT until I stop my App then restart. Then
I get the 3:00 pm EDT.

What I was wondering is there a way to get DateTime.Now to
show the correct local time without having to stop/start
my appplication.

Thanks again.

>.
>

Paul G. Tobey [eMVP]

unread,
Jun 27, 2003, 4:54:52 PM6/27/03
to
Unless someone else removed it, it's in there (I wrote that code). Look at

OpenNETCF.WinAPI.Core.SetTimeZoneInformation();

Paul T.

"Jeff Samuels" <samuels...@emeryworld.com> wrote in message

news:104001c33cea$2eebe050$a401...@phx.gbl...

Chris Tacke, eMVP

unread,
Jun 27, 2003, 5:11:36 PM6/27/03
to
That's annoying. Using GetLocalTime instead of Now() would probably work
around that. It's also in WinAPI

--
Chris Tacke, eMVP
Advisory Board Member
www.OpenNETCF.org


"Jeff Samuels" <samuels...@emeryworld.com> wrote in message

news:104001c33cea$2eebe050$a401...@phx.gbl...

Alex Feinman [MVP]

unread,
Jun 27, 2003, 5:29:20 PM6/27/03
to

"Jeff Samuels" <samuels...@emeryworld.com> wrote in message
news:104001c33cea$2eebe050$a401...@phx.gbl...

> Alex,
>
> Thanks for the response.
>
> Just had a look at the OpenNETCF library. I looked at the
> Core.TIME_ZONE_INFORMATION class and the Core.TX_REG class
> and I did not see a SetTimeZoneInformation (or similar)
> methods for either of these. Am I missing something?

It's a member of WinAPI.Core class

>
> As for the other... It is not that my app needs to be
> aware of a change to the timezone. Rather it is that when
> the timezone does change the DateTime returned by
> DateTime.Now (which returns the local time) does not show
> the effect of the change. For example say it is 12:00pm
> PDT and the tz is set to "Pacific". If I start my app and
> subquently launch "clock" and change the tz to "Eastern".
> I would expect DateTime.Now to return 3:00pm EDT. What I
> get is 12:00pm PDT until I stop my App then restart. Then
> I get the 3:00 pm EDT.
>

The current timezone information is kept in a private static member of the
TimeZone class called currentTimeZone. It is initialized once and then
reused. The way to force its refresh is to set it to null:

using System.Reflection;
FieldInfo fi = typeof(TimeZone).GetField("currentTimeZone",
BindingFlags.NonPublic|BindingFlags.Static|BindingFlags.Instance);

fi.SetValue(null, null);

In VB:

Imports System.Reflection

Dim tz as TimeZone = TimeZone.CurrentTimeZone

Dim fi as FieldInfo = tz.GetType().GetField("currentTimeZone",
BindingFlags.NonPublic OR BindingFlags.Static OR BindingFlags.Instance)

fi.SetValue(Nothing, Nothing)

Next time any time-formatting (or other timezone-dependent call is made), CF
will re-read current TZ info from OS

0 new messages