views:

161

answers:

1

Hi All,

I'm getting to a point with my app where I'm about to try to roll out utc support.

I've already got it all working and have written myself two utility classes, called convertToUtc and convertFromUtc. I think you can guess what they do.

What I was thinking though, could I build these into the getter and setter methods for my date property on the linq-to-sql created object model, or should I just go round the app everywhere re assigning how the value is saved (adding an extra line to the controller like

task.taskDeadline = Utility.ConvertToUtc(aspnet_Repository.GetUserGuid(User.Identity.Name), task.taskDeadline.Value);

If anyone can tell me I'm doing something horrible by going back to the db each time I need the user Guid, that would be cool. I guess down the line somewhere I would cache it, but this would require holding it in session or something I presume.

Thanks all.

+1  A: 

You should always store datetimes in your database as UTC values (if you know the true UTC time for the object) and NEVER as local time values.

The conversion from UTC time to localtime is [mostly] a rendering issue that should be dealt with in your UI layer.

One exception to this latter rule is when you are trying to GROUP by datetime for the user (e.g. what happened yesterday or last month) in which case the UI needs to pass the timezone down to the business layer, and maybe even to the database in order to execute an efficient query grouped by a local representation of the datetime.

Hightechrider
Your exception case can be handled by storing the users timezone with the user as an offset from GMT ( i.e. as number between -12 and +13 ). Update the users timezone each time they log on ( easy to do with javascript if its a web app ).
Neal
Yes, you need to pass the timezone from UI to business layer (as I described) but it's not an integer: timezones can be decimal values. Also, strictly speaking, you need the full timezone information not just the offset to be able to group correctly around the times of year when DST changes (most people don't do this because it's very hard).
Hightechrider