In my previous post I bemoaned my lack of Regex skills and the fact that the VS2010 Training Kit startdates were fixed to use DateTime.Parse() rather than new DateTime(). I always use new DateTime() because it removes the mm/dd versus dd/mm problem. I've revisited the problem, in part to try and improve my Regex skills, and here is my solution.
Pattern:
DateTime\.Parse\("(\d{1,2})/(\d{1,2})/(\d{4})"\)
Replace:
new DateTime($3, $1, $2)
Good news / bad news time. That will work programmatically using the .NET Framework's Regex.Replace() method, BUT NOT in find / replace. Curses! The curlies get read as a "tag" by the find/replace tool, by which they mean capture group. Also, "\d" doesn't appear to be recognised and needs to be replaced with ":d". So that means I need to ask it to find one or two digits, then one or two digits, then four digits. I can use a hat "^" to specify a count. Finally, rather than using the "$" to place a capture group in the replace, it wants a "\". So that leaves my pattern looking like this:
DateTime\.Parse\("{(:d|:d^2)}/{(:d|:d^2)}/{:d^4}"\)
and leaves my replace looking like this:
new DateTime(\3, \1, \2)
Phew! I'm going for a lie down...
Showing posts with label US date format. Show all posts
Showing posts with label US date format. Show all posts
Tuesday, 8 September 2009
Thursday, 16 July 2009
VS2010 Training Kit - US-centric hardcoded dates
Finally got my virtual machine with Windows 7 and VS2010 beta sorted so I started working through the Training Kit, wanting to mess with the Parallel stuff. Ran the first code snippet and BANG! I'm getting a parse exception. Turns out the startdates for the Employees are being created using (e.g.):
DateTime.Parse("5/16/2001")
Which is fine in the States but no use on my en-GB machine. So my first instinct was to run a regular expression to turn them all into (e.g.):
new DateTime(2001, 5, 16)
But then I realised that my RegEx skills are not good enough and I wanted to get the job done before my train home finally arrived. After umming and ahhing for a few moments I remembered that the problem was my culture settings. So I thought I have two options - either change my culture settings (BOO!) or change the constructor's culture settings.
So I stuck this at the start of the EmployeeList constructor:
var ct = Thread.CurrentThread;
var origCulture = ct.CurrentCulture;
ct.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
And this at the end:
ct.CurrentCulture = origCulture;
Yay!
DateTime.Parse("5/16/2001")
Which is fine in the States but no use on my en-GB machine. So my first instinct was to run a regular expression to turn them all into (e.g.):
new DateTime(2001, 5, 16)
But then I realised that my RegEx skills are not good enough and I wanted to get the job done before my train home finally arrived. After umming and ahhing for a few moments I remembered that the problem was my culture settings. So I thought I have two options - either change my culture settings (BOO!) or change the constructor's culture settings.
So I stuck this at the start of the EmployeeList constructor:
var ct = Thread.CurrentThread;
var origCulture = ct.CurrentCulture;
ct.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
And this at the end:
ct.CurrentCulture = origCulture;
Yay!
Subscribe to:
Posts (Atom)