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 ti开发者_JAVA百科mezones).
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.
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.
- Save all records to the database using myDate.ToUniversalTime()
- When loading from database using myDate.ToLocalTime()
Then your users can move as much as they like.
System.Threading.Thread.CurrentThread.CurrentCulture
returns you Current Culture Info
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.
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.
精彩评论