<?xml version="1.0" encoding="UTF-8"?>
<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>Why Can&#039;t You Code? &#187; workaround</title>
	<atom:link href="http://www.whycantyoucode.com/tag/workaround/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.whycantyoucode.com</link>
	<description>The highest form of ignorance is when you reject something you don&#039;t know anything about.</description>
	<lastBuildDate>Thu, 25 Feb 2010 14:39:31 +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>Workaround for Silverlight RIA Services DateTime discrepancy between server and client</title>
		<link>http://www.whycantyoucode.com/2009/11/workaround-for-silverlight-ria-services-datetime-discrepancy-between-server-and-client/</link>
		<comments>http://www.whycantyoucode.com/2009/11/workaround-for-silverlight-ria-services-datetime-discrepancy-between-server-and-client/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 12:07:32 +0000</pubDate>
		<dc:creator>Andrei Alecu</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[ria services]]></category>
		<category><![CDATA[workaround]]></category>

		<guid isPermaLink="false">http://www.whycantyoucode.com/?p=125</guid>
		<description><![CDATA[While working on a Silverlight application. I discovered that all the dates displayed in my DataGrid were skewed by a few hours from the real date stored in the database. Upon a closer look I noticed this skew was closely correlated to the time difference between the server’s time zone and the UTC date.
I needed [...]]]></description>
			<content:encoded><![CDATA[<p>While working on a Silverlight application. I discovered that all the dates displayed in my DataGrid were skewed by a few hours from the real date stored in the database. Upon a closer look I noticed this skew was closely correlated to the time difference between the server’s time zone and the <a href="http://en.wikipedia.org/wiki/Coordinated_Universal_Time" target="_blank">UTC date.</a></p>
<p><strong>I needed my Silverlight application to display and use the exact same dates as the database regardless of the time zone my users were in.</strong></p>
<p>Apparently, there’s a little known <span style="text-decoration: line-through">bug</span> “<a href="http://smehrozalam.wordpress.com/2009/08/07/ria-services-how-dates-are-handled-and-sent-across-the-wire/" target="_blank">feature</a>” in the latest <a href="http://silverlight.net/getstarted/riaservices/" target="_blank">RIA Services Beta</a> for Silverlight that involves dates:</p>
<p>RIA uses <strong>JSON</strong> to send data across the wire, and the Microsoft JSON serializers <a href="http://www.west-wind.com/weblog/posts/471402.aspx" target="_blank">always serialize dates as UTC</a>.</p>
<p>Dates retrieved from the database always seem to come up as <code>DateTimeKind.Unspecified</code>, meaning that the serializer will take a guess and treat them as local dates (in the <strong>server’s </strong>local time zone) and convert them to UTC time before sending out on the wire. </p>
<p>There’s an overridable property in the <code>DomainService</code> class called <code>UsesUtcDateTimes</code> which should in theory make the serializer leave your dates alone by treating them as UTC &#8211; which is still not ideal, but it means it won’t change them further &#8211; the property, however, <em>currently </em>doesn’t do anything.</p>
<p><font color="#cc0000" size="4">The gotcha here is that the <strong>original</strong> time zone of the date is <strong>lost</strong> on the wire. It’s not sent along with the date so there’s no way you could determine, on the client, what time zone the date was in originally!</font></p>
<p><strong>Example:</strong> if the server’s timezone is GMT+1, a date of December 21, 2012 <strong><u>12:21 PM</u></strong> will be converted to the UTC (GMT+0) date of December 21, 2012 <strong><u>11:21 AM</u></strong> before being sent out!</p>
<p>The Silverlight application will now display the UTC date of December 21, 2012 11:21 AM instead of the expected date from the database.</p>
<h4><strong>The easy workaround…</strong></h4>
<p>… is to mimic the behavior of the currently un-implemented <code>UsesUtcDateTimes</code> property and fool the serializer into thinking all of your dates are <strong>already</strong> in UTC so it doesn’t mess with them. Thanks to partial classes, you can do this.</p>
<p>Create new partial classes for each of your RIA Data Classes that hold <code>DateTime</code> values and override the <code>On(DateTimePropertyName)Changed()</code> methods like the following:</p>
<pre class="brush: csharp; ruler: true; smart-tabs: false;">public partial class MyDataClass
{
    partial void OnDateChanged()
    {
        _Date = DateTime.SpecifyKind(_Date, DateTimeKind.Utc);
    }

    partial void OnOtherDateChanged()
    {
        _OtherDate = DateTime.SpecifyKind(_OtherDate, DateTimeKind.Utc);
    }
}</pre>
<p>(Notice the <code>partial</code> keyword on the methods as well, not just on the class.)</p>
<h4>The technically correct workaround…</h4>
<p>.. is to pass the original time zone, as a property, along with the <a href="http://en.wikipedia.org/wiki/Coordinated_Universal_Time" target="_blank">UTC</a> DateTime across the wire, and then re-apply the time zone to it on the client before using it.</p>
<p>I’m hoping for a future RIA Services release to address this issue better, and perhaps pass the time zone across by itself.</p>
<h4></h4>
<h3><font color="#cc0000"><strong>UPDATE:</strong> This appears to be fixed in the latest release of </font><a href="http://silverlight.net/riaservices/" target="_blank"><font color="#cc0000">WCF RIA Services</font></a><font color="#cc0000">. This workaround is no longer necessary unless your application still uses the older .NET RIA Services. </font></h3>
<h3><font color="#cc0000">I have written a verbose guide on </font><a href="http://www.whycantyoucode.com/2009/11/verbose-steps-to-migrate-a-silverlight-application-from-net-ria-services-july-09-to-wcf-ria-services-pdc-09/" target="_blank"><font color="#cc0000">upgrading an application to WCF RIA Services</font></a><font color="#cc0000">. Check it out </font><a href="http://www.whycantyoucode.com/2009/11/verbose-steps-to-migrate-a-silverlight-application-from-net-ria-services-july-09-to-wcf-ria-services-pdc-09/" target="_blank"><font color="#cc0000">here</font></a><font color="#cc0000">.</font></h3>
]]></content:encoded>
			<wfw:commentRss>http://www.whycantyoucode.com/2009/11/workaround-for-silverlight-ria-services-datetime-discrepancy-between-server-and-client/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
