Archive for the ‘C#’ Category

Safe way to convert UTC time to local time when databinding

Monday, October 29th, 2007

After reading Scott Mitchell’s article Using Coordinated Universal Time (UTC) to Store Date/Time Values, I decided to do as he suggest and use UTC for a web application I’m building at work. All in all it is fairly easy to deal with UTC in the .NET Framwork. There is a DateTime.UtcNow you can use instead of the regular DateTime.Now property, and there is a DateTime.ToLocalTime method that will convert your UTC DateTime value to the local server time zone, even taking daylight savings time in to consideration. When converting the other way, the DateTime.ToUniversalTime method is provided.

With MsSQL’s getutcdate() function (instead of getdate()), just about everything is smooth sailing. The only problem I have encountered is when binding a to a ASP.NET server control. Scott suggests doing like this:

((DateTime) Eval("DateUpdated")).ToLocalTime()

This only works when DateUpdated is not null, which is not always the case in my application. To get around this, I created the following simple class and stuck it in my App_Code folder.

public static class Extensions
{
    /// <summary>
    /// If object is a DateTime, it will convert
    /// the DateTime to local time.
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public static object ToLocalTime(this object obj)
    {
        if (obj is DateTime)
            return ((DateTime)obj).ToLocalTime();
        else
            return obj;
    }
}

The method ToLocalTime(this object obj)uses the extension method feature in .NET 3.0, that allows you to extend the functionality of other class. Here I extend upon Object, adding a .ToLocalTime() method to it. This allows me to use it in ASP.NET server controls like this:

Eval("DateUpdated").ToLocalTime()

This works because Eval() returns a object, which is also the reason why I did not extend a more specific class.

Getting excited about LINQ

Thursday, February 8th, 2007

Today I attended a very fascinating two hour lecture at DIKU where Mads Torgersen (Program Manager at Microsoft) talked about LINQ and how it works. He especially focused on the different ideas and technologies that Microsoft uses under the hood to make it less of a pain for the rest of us when working with data. One of the things that he focused a great deal on was how principles from functional programming are being introduced into the C# and the .NET framework.

A funny side note: I was sitting next to a professors who teaches a course on functional programming that they make every first year student take, and he said numerous times that “this is a great advertisement for my course”, and I do understand why he is was excited. I remember back when I was dragged kicking and screaming through that course, wondering why I needed to learn this weird kind of programming that wasn’t even used in general practice, but now, all of a sudden, this guy from Microsoft is typing in lambda expressions in Visual Studio on the projector, God knows I never thought I would see a lambda expression again after that course :) .

Back home from the university I goggled around a bit and found two interesting videos featuring another Dane, Anders Hejlsberg (Chief Architect of C# at Microsoft). In the first video Anders gives an introduction to benefits LINQ will give you as a programmer using easy to understand code examples, and in the second video he talks more theoretically about the problem that LINQ solves and also gets into what capabilities functional programming will bring to the table down the road. Both videos are well worth the time.

More information can be found over at The LINQ Project page on the MSDN network.