<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Egil Hansen &#187; C#</title>
	<atom:link href="http://egilhansen.com/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://egilhansen.com</link>
	<description>.NET, Windows, ASP.Net, scripts</description>
	<lastBuildDate>Thu, 04 Feb 2010 13:32:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Safe way to convert UTC time to local time when databinding</title>
		<link>http://egilhansen.com/2007/10/29/safe-way-to-convert-utc-time-to-local-time-when-databinding/</link>
		<comments>http://egilhansen.com/2007/10/29/safe-way-to-convert-utc-time-to-local-time-when-databinding/#comments</comments>
		<pubDate>Mon, 29 Oct 2007 17:43:18 +0000</pubDate>
		<dc:creator>Egil Hansen</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.egil.dk/2007/10/29/safe-way-to-convert-utc-time-to-local-time-when-databinding/</guid>
		<description><![CDATA[After reading Scott Mitchell&#8217;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&#8217;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 [...]]]></description>
			<content:encoded><![CDATA[<p>After reading Scott Mitchell&#8217;s article <a href="http://aspnet.4guysfromrolla.com/articles/081507-1.aspx">Using Coordinated Universal Time (UTC) to Store Date/Time Values</a>, I decided to do as he suggest and use UTC for a web application I&#8217;m building at work. All in all it is fairly easy to deal with UTC in the .NET Framwork. There is a <a href="http://msdn2.microsoft.com/en-us/library/system.datetime.utcnow.aspx">DateTime.UtcNow</a> you can use instead of the regular DateTime.Now property, and there is a <a href="http://msdn2.microsoft.com/en-us/library/system.datetime.tolocaltime.aspx">DateTime.ToLocalTime</a> 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 <a href="http://msdn2.microsoft.com/en-us/library/system.datetime.touniversaltime.aspx">DateTime.ToUniversalTime</a> method is provided.</p>
<p>With MsSQL&#8217;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:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>DateTime<span style="color: #000000;">&#41;</span> Eval<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;DateUpdated&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToLocalTime</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>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.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> Extensions
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// If object is a DateTime, it will convert</span>
    <span style="color: #008080; font-style: italic;">/// the DateTime to local time.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;obj&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">object</span> ToLocalTime<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> <span style="color: #FF0000;">object</span> obj<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>obj <span style="color: #008000;">is</span> DateTime<span style="color: #000000;">&#41;</span>
            <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>DateTime<span style="color: #000000;">&#41;</span>obj<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToLocalTime</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">else</span>
            <span style="color: #0600FF;">return</span> obj<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The method <span style="font-family: Courier New">ToLocalTime(this object obj)</span>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:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Eval<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;DateUpdated&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToLocalTime</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>This works because Eval() returns a object, which is also the reason why I did not extend a more specific class.</p>
]]></content:encoded>
			<wfw:commentRss>http://egilhansen.com/2007/10/29/safe-way-to-convert-utc-time-to-local-time-when-databinding/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Getting excited about LINQ</title>
		<link>http://egilhansen.com/2007/02/08/getting-excited-about-linq/</link>
		<comments>http://egilhansen.com/2007/02/08/getting-excited-about-linq/#comments</comments>
		<pubDate>Thu, 08 Feb 2007 20:48:02 +0000</pubDate>
		<dc:creator>Egil Hansen</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.egil.dk/2007/02/08/getting-excited-about-linq/</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>Today I attended a very fascinating two hour lecture at <abbr title="The Department of Computer Science, University of Copenhagen">DIKU</abbr> where Mads Torgersen (Program Manager at Microsoft) talked about <abbr title="Language Integrated Query">LINQ</abbr> 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 <a href="http://en.wikipedia.org/wiki/Functional_programming" title="More about functional programming over at Wikipedia">functional programming</a> are being introduced into the C# and the .NET framework.</p>
<p>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 <span style="font-style: italic">&#8220;this is a great advertisement for my course&#8221;</span>, 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&#8217;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 <img src='http://egilhansen.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Back home from the university I goggled around a bit and found two interesting videos featuring another Dane, <a href="http://en.wikipedia.org/wiki/Anders_Hejlsberg" title="Anders Hejlsberg at Wikipedia">Anders Hejlsberg</a> (Chief Architect of C# at Microsoft). In <a href="http://channel9.msdn.com/Showpost.aspx?postid=114680" title="Anders Hejlsberg on LINQ at Channel 9">the first video</a> Anders gives an introduction to benefits <abbr title="Language Integrated Query">LINQ</abbr> will give you as a programmer using easy to understand code examples, and in <a href="http://blogs.msdn.com/charlie/archive/2007/01/26/anders-hejlsberg-on-linq-and-functional-programming.aspx" title="Anders Hejlsberg on LINQ and Functional Programming">the second video</a> he talks more theoretically about the problem that <abbr title="Language Integrated Query">LINQ</abbr> solves and also gets into what capabilities functional programming will bring to the table down the road. Both videos are well worth the time.</p>
<p>More information can be found over at <a href="http://msdn2.microsoft.com/en-us/netframework/aa904594.aspx" title="The LINQ Project">The LINQ Project page</a> on the MSDN network.</p>
]]></content:encoded>
			<wfw:commentRss>http://egilhansen.com/2007/02/08/getting-excited-about-linq/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
