<?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>Ad hoc Geek &#187; formatting</title>
	<atom:link href="http://www.adhocgeek.com/tag/formatting/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.adhocgeek.com</link>
	<description>Approaching geekery in an ad hoc and improvisational manner.</description>
	<lastBuildDate>Fri, 30 Sep 2011 09:43:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Actionscript Date Lunacy</title>
		<link>http://www.adhocgeek.com/2009/08/actionscript-date-lunacy/</link>
		<comments>http://www.adhocgeek.com/2009/08/actionscript-date-lunacy/#comments</comments>
		<pubDate>Thu, 20 Aug 2009 14:41:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Geek]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[dates]]></category>
		<category><![CDATA[formatting]]></category>

		<guid isPermaLink="false">http://www.adhocgeek.com/?p=100</guid>
		<description><![CDATA[I really hate the Actionscript Date class. Mostly because of the way it deals with local time offsets. When I create a new date instance, I generally don&#8217;t care about the local time zone settings. I&#8217;ll care about them when I have to combine it with other dates, sure, but at the time I set [...]]]></description>
			<content:encoded><![CDATA[<p>I <em>really</em> hate the <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Date.html">Actionscript Date class</a>. Mostly because of the way it deals with local time offsets. When I create a new date instance, I generally don&#8217;t care about the local time zone settings. I&#8217;ll care about them when I have to combine it with other dates, sure, but at the time I set that value I want it to <em>stay</em> that value. The AS date class thinks otherwise.</p>
<p>Say I have a number of milliseconds from epoch (1 Jan 1970) which I want to use to create a specific date so I can use a DateFormatter to output the result, then you&#8217;d think that the following would be fine :</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> myDate:<span style="color: #0066CC;">Date</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Date</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1250766979000</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> df:DateFormatter = <span style="color: #000000; font-weight: bold;">new</span> DateFormatter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
df.<span style="color: #006600;">formatString</span> = <span style="color: #ff0000;">&quot;YYYY-MM-DD JJ:NN:SS&quot;</span>;
<span style="color: #000000; font-weight: bold;">var</span> stringDate:<span style="color: #0066CC;">String</span> = df.<span style="color: #006600;">format</span><span style="color: #66cc66;">&#40;</span>myDate<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p><small>(Let&#8217;s gloss over the batshit-insane need for a DateFormatter class and the actual formatString itself &#8211; JJ for 0-23 hour time, really?)</small></p>
<p>But no. In my area, we&#8217;re currently in daylight savings mode, so the Flash player decides that this date must be modified accordingly, and if you format it with the code above you&#8217;ll get &#8220;2009-08-20 12:16:19&#8243;, whereas it should be &#8220;2009-08-20 11:16:19&#8243; (you can try and calculate this yourself if you&#8217;re feeling particularly bored). This sounds fairly trivial to deal with until you realise that, short of writing your own DateFormatter, there&#8217;s no way to output the actual, formatted UTC date and time (ignoring the unformattable toUTCString() method).</p>
<p>To do that, you have to take this approach :</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> myDate:<span style="color: #0066CC;">Date</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Date</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1250766979000</span><span style="color: #66cc66;">&#41;</span>;
myDate.<span style="color: #0066CC;">setTime</span><span style="color: #66cc66;">&#40;</span>myDate.<span style="color: #0066CC;">getTime</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + myDate.<span style="color: #006600;">timezoneOffset</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">60</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">1000</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> df:DateFormatter = <span style="color: #000000; font-weight: bold;">new</span> DateFormatter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
df.<span style="color: #006600;">formatString</span> = <span style="color: #ff0000;">&quot;YYYY-MM-DD JJ:NN:SS&quot;</span>;
<span style="color: #000000; font-weight: bold;">var</span> stringDate:<span style="color: #0066CC;">String</span> = df.<span style="color: #006600;">format</span><span style="color: #66cc66;">&#40;</span>myDate<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Which, to me at least, seems mental, because we haven&#8217;t really created a UTC date there &#8211; we&#8217;ve actually had to modify the milliseconds since epoch due to the local timezone settings. If I change the time settings on my computer, the output of this will still change! It&#8217;s possible I&#8217;ve lived too long in Microsoft-land, but the equivalent code in C# seems a million times better and far more predictable :</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">DateTime dat <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DateTime<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1970</span>, <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> TimeSpan<span style="color: #008000;">&#40;</span>12507669790000000L<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">string</span> stringDate <span style="color: #008000;">=</span> dat<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;yyyy-MM-dd HH:mm:ss&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Yes, miraculously, dates can format themselves in C#! There&#8217;s no need for a superfluous DateFormatter class. The only reason this code might look a litte overinflated is that dates in C# have a wider range, so I&#8217;ve had to convert the milliseconds since Unix epoch to 10&#8242;s of nanoseconds since 0001-1-1 00:00:00.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adhocgeek.com/2009/08/actionscript-date-lunacy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

