views:

112

answers:

5

Hi,

We are making an application with the details below.

1) Web/Desktop App on C#.Net

2) Stores users activity timestamp as a datetime field.

We need to store this timestamp in UTC format.

Now, while displaying it will be converted to user's timezone.

Problem

The user can float from one office to another. (multiple offices all in different timezones).

Now, lets assume we want to display user's activity for one month. (It might include different timezones).

How can we store/track users timezone information?

So that it converts the time at runtime and displays proper time.

e.g.

UTC time

day1 : time1

day2: time 2

UTC +1:30

day 3: time3

etc..

Can you suggest how this issue can be dealt with.

Thanks in advance.

A: 
System.Threading.Thread.CurrentThread.CurrentCulture

returns you Current Culture Info

Serkan Hekimoglu
actually, users also login using VPN.. so, in that case the actual timezone will not be tracked..and i want the app design.. how should we store these and relate this.. so that we can get actual time in any one timezone
Chitresh
A: 

You can save the current time zone (TimeZoneInfo.Local or TimeZone.CurrentTimeZone, they are the same but the first one is preferred) along with your timestamps.

I think the user should change the current timezone from the windows clock properties panel.

munissor
+5  A: 

If you use DateTime.UtcNow when you record the activity timestamp, you will always get the correct time. And usually it's the server which will store the time stamp, so it's relative to the server anyways.

If the user inputs it, then you have to convert the input time to UTC before saving it. This can be picked up from the users Culture/TimeZone settings.

When displaying it to the user, you set the Culture and TimeZone information on the thread according to where in the world the user is.

If you don't store the "UTC" part of the date in your database (even though it is in UTC format) you can convert it to UTC like this:

DateTime date = ..from db..;
date = DateTime.SpecifyKind(date, DateTimeKind.Utc);

For converting between timezones check out MSDN.

Mikael Svenson
actually, users also login using VPN.. so, in that case the actual timezone will not be tracked.. and i want the app design.. how should we store these and relate this.. so that we can get actual time in any one timezone –
Chitresh
As long as the timezone is correct on the users desktop (citrix or not) you can use UtcNow for a desktop application. For a webapp check out http://articles.techrepublic.com.com/5100-10878_11-6016329.html on how to use javascript to get the current time and timzone (and convert to UTC) and copy the value to a field which you post to the server.
Mikael Svenson
thanks mikael...
Chitresh
A: 

It seems to me that the application itself should force the user to input the timezone from which they are currently working in order to a reliable timestamp by any method. My guess is asking for this information upon log-in. Though this might become an annoyance for the user, expecting the users to update their time zone through some sort of optional settings dialog is a fantasy.

If you have the resources and are pretty daring, you could always get the time zone based on the IP address of the computer they are accessing the VPN tunnel from, though I couldn't even begin to help you do that.

wtfsven
+1  A: 
  1. Save all records to the database using myDate.ToUniversalTime()
  2. When loading from database using myDate.ToLocalTime()

Then your users can move as much as they like.

jgauffin
If the server gets the date in a specified timezone, then the save will work. If the server is executing the ToLocalTime(), then the date will be converted to the servers local time, not necessarily the clients. Unless you pass on the timezone settings from the client and set on the running thread.
Mikael Svenson