Sometime 'D' Is Not a 'D'

Converting from date to string in C# is really easy. Basic components are y M d H m s and you go from there:

var date = new DateTime(2008, 3, 1);
Console.WriteLine(date.ToString("yyyy-MM-dd")); //2008-03-01
Console.WriteLine(date.ToString("yyyy")); //2008
Console.WriteLine(date.ToString("MM")); //03
Console.WriteLine(date.ToString("dd")); //01

Of course you can also use format without leading zeros:

var date = new DateTime(2008, 3, 1);
Console.WriteLine(date.ToString("yyyy-M-d")); //2008-3-1
Console.WriteLine(date.ToString("M")); //March 1 (or something similar) - WTF?
Console.WriteLine(date.ToString("d")); //2008-11-01 (or something similar) - WTF?

Looking at first line one would expect d and M to give day and month without leading zero. In .NET these characters lead double life. For example, if d is given alone it really means standard date and time format specifier. Similar goes for M.

Solution is quite simple. If you really need to use them alone, just prepend percent character (%):

var date = new DateTime(2008, 3, 1);
Console.WriteLine(date.ToString("yyyy-M-d")); //2008-3-1
Console.WriteLine(date.ToString("%M")); //3
Console.WriteLine(date.ToString("%d")); //1

VisualHg in Visual Studio 2012

Illustration

I am big fan of Mercurial and I keep all my private projects under its control. Quite a big portion of them is done in Visual Studio so I needed something a bit more integrated than TortoiseHg. Best solution I have found was VisualHg. And I was happy user until Visual Studio 2012 came out.

No matter how many times I would install and reinstall, it would not appear under Plug-in Selection. It seems that VisualHg installation is broken a bit so it does not signal Visual Studio to refresh list of source control plugins.

Solution is actually easy. Install VisualHg as you would usually do. When you start Visual Studio 2012 just go to Tools -> Extensions and Updates. In online section there you can find HgSccPackage. I actually don’t like this one but installing it does refresh plugin list. After HgSccPackage install is done, VisualHg is once more available for selection.

How Long Is a Month

Most of car rentals have a contract with other companies where companies get various discounts and addons in exchange for continuous business. It is good deal for everybody involved. But sometime curious things happen.

For example company I work for has benefit of “Monthly charge up to 31 days”. Simplified it means that your rental comes at cheaper price a bit earlier. For me that has an even better benefit of getting reimbursed on month basis. In ideal world, if I rent car on 1st, I get an receipt on the same date next month. Except it isn’t so.

For some reasons National has also an policy of closing each contract every 30 days. If you imagine a world where some months have a day longer you will see a problem. Let’s assume my rental starts on September 1st. First bill is due on October 1th. Next bill after that is October 31st. One after that is November 30th. Congratulations, you have just lost a day.

In reality this is not really an cost issue. Although you have monthly rate actual calculations are still done per day so everything evens out. However, person has an interesting choice between sending to accounting two bills for all months that have 31 day or sending them “monthly” bills that end on funny dates.

It only shows that for system design you must consider edge cases (and test for them) or users might get confused…

DotPeek

Illustration

Almost two years ago .NET Reflector went into freeware history. First it was $35 but price has gone since to $95 these days. For something that was freeware once this is quite an increase.

Strictly speaking I was not really affected. Microsoft released source code for basic .NET libraries quite a long ago so most of my snooping needs were covered. But setting that up was pain-in-the-ass compared to the pure simplicity of .NET Reflector.

JetBrains, creators of ReSharper tool, created new tool called dotPeek. For all practical purposes we can view it as long lost cousin of .NET Reflector. Best of all it is completely free (at least in version 1.0).

Product is not without fault. On Windows 8 I could not get document search to work; source produced has extra spaces here and there; it does not support Visual Basic; there is no option to save a file; project export is not possible; it sometime breaks Alt-Tab behavior… Those are just biggest complaints that show it is definitely not without fault.

But I like it nevertheless!

It worked perfectly on all code that I gave it and has always produced acceptable code. Stack trace browsing allows to track function calls across assemblies with ease and it is feature that I never thought of before but it seems so logical now.

Browsing though code is probably as good as it gets once you get used to keyboard shortcuts. And editor support for code folding is fantastic. Since that is what you actually need 95% of time, all bugs / forgotten features are easily forgotten.

It is tool that is worth its place in toolbox of every C# programmer.

P.S. No, it does not have stupid time restrictions that drove me crazy even in freeware .NET Reflector.

Windows 8 Media Center Pack for Free

Illustration

Windows 8 came with some multimedia features missing. Mind you, it was by design. Missing features were made available as separate product named Windows 8 Media Center Pack. For a small cost ($10) you can make your Windows 8 work as good as Windows 7. Nice touch.

Good news is that you can get it for free. There are two conditions: you must have Windows 8 Professional (MSDN works also) and you must activate it before January 31st 2013. If that is true, just go to Microsoft feature pack pages and give your e-mail address. In few minutes you should get a key.

With that key just search for Add Features in Settings and enter key when asked to. Feature will be downloaded in background. Just take care not to do anything else on system since installation will restart your computer without any warning (!). After restart you will have one more restart (?) and then Windows Media Center will magically appear.