Offsetting timezones

A

Alex

Hi

My website is hosted in the States (EST), but the website itself is
targeted for UK users (GMT).

How can I offset the time so that the server reports it as GMT when my
ASP.NET app needs to find out the current time?
..
Can I do this in web.config? If so how exactly?

Please note I am using ASP.NET 2.0 and VB.NET, and I can't set the
system clock of the server as it is owned by the web hosting provider.

Many thanks!

Alex
 
K

Kevin Frey

In your case it is relatively simple. Since UK is GMT, just convert the
localtime that the US server provides you into GMT using the
DateTime.ToUniversalTime( ) function.

For the more general case (ie. user is located anywhere in the world) you
will have to perform your own DateTime biasing to compute the local time for
the user's location. You will also need your own mechanism to know what
location the user is in. Generally this would also be associated with other
Culture-related behaviour as well (eg. how numbers are formatted, how dates
are formatted etc), so it's really just an extension of that.

..NET has a TimeZone class, which I [at first] mistakenly thought it might
behave somewhat like Cultures (ie. get a TimeZone class for a particular
timezone). But it doesn't do that, so it doesn't seem particularly useful.
 
G

Guest

Hi Alex,

Since your site will be deployed in US and is targetted for UK (GMT) users,
your case is relatively simple. But, its always better to develope for the
most general case (To and From any Time Zone so that your site does not break
when the location of your hosting provider is changed).

Unfortunately DateTime object gives us the luxury of only converting to and
from Universal Date Time (Formerly GMT) and Local Time (System Time Zone).

// expects DateTimeObject to contain Local Time and converts it to Universal
// Date Time value
DateTimeObject.ToUniversalTime()

// expects DateTimeObject to contain Universal Time and converts it to Local
// Date Time value
DateTimeObject.ToLocalTime()

So apparently there is no way to convert to and from a specific time zone.
But there are certain techinques to acheive what you need.

You can go along the following ways.


1) Use Javascript to pass the clients time zone offset to the server in a
hidden field and compute the British time on the server by first converting
the Server Local Time to Universal time and then adding the client's Time
Zone Offset to the Universal Time.

//javascript on client
dt = new Date();
locOffset = dt.getTimezoneOffset(); // get client's time zone offset
//Now pass this in a hidden feild to the server

//C# on Server
DateTime dt = DateTime.Now;
DateTime desiredTime = dt.ToUniversalTime().AddHours(offset);
//Note: offset is the client's time zone offset passed in the hidden field



2) If you want to perform all the computations on the server side then you
can probably use a class written by a gentleman which makes use of windows
APIs and can convert to any time zone.
http://staceyw.spaces.live.com/blog/cns!F4A38E96E598161E!931.entry



Regards,
Hameer Saleem
 
A

Alex

Thanks for the quick response guys! Useful.

The ASP.NET app I'm using is actually DotNetNuke 4.3.7, I logged a post
here about it here:
(http://www.dotnetnuke.com/Community...d/108/threadid/88651/scope/posts/Default.aspx)

The app appears to support timezones as far as individual users and
website portals (if different portal were based in different
countries), but appears to fall down hill when the server is based in a
different timezone, like in my scenario (I'm not the only DNN user with
this problem it seems).

I was trying to find a way of conning the whole app into accepting a
GMT time, maybe overriding the datetime class or something. Then
everything would work beautifully (although the SQL server backend for
datestamps would be the next problem).

It's a pity ASP.NET doesn't have a setting in web.config that would
adjust the app timezone, wouldn't you think this would be a sensible
feature?

Cheers!

Alex
 
J

Juan T. Llibre

re:
It's a pity ASP.NET doesn't have a setting in web.config that would
adjust the app timezone, wouldn't you think this would be a sensible
feature?

Hi, Alex.

If you download the source for Community Server
( http://communityserver.org/files/default.aspx )
you'll see a helper class named GeographicTimeZone.cs

That class, along with TimezoneDropDownList.cs and timezones.xml
allows users to adjust their timezone setting.

It shouldn't be too difficult to adapt that code for your purposes.



Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
 
A

Alex

Thanks Juan!

Alex

re:

Hi, Alex.

If you download the source for Community Server
( http://communityserver.org/files/default.aspx )
you'll see a helper class named GeographicTimeZone.cs

That class, along with TimezoneDropDownList.cs and timezones.xml
allows users to adjust their timezone setting.

It shouldn't be too difficult to adapt that code for your purposes.



Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top