Scott Hanselman

"Parsing a culture-aware DateTime" or "Avoiding InStr"

June 11, 2003 Comment on this post [2] Posted in Web Services | Internationalization
Sponsored By

Say you've a black-box application that you can't change that is culture-ignorant and returns a date as a string that looks like this:

6/11/2003 1:56:31 PM

but your client wants it output like this

11-Jun-03 01:56:31 PM

Sometimes folks ask me "What's the best way to parse a Date string in .NET?"  Often they are old school VBA-type folk and they are really saying "I'm going to use InStr() to parse this date unless you stop me."

So, here's a better way, IMHO.  I believe in avoiding Mid$, InStr, strstr, etc...it's just not my business to parse strings.  This is, of course, not the only way, but a possible way.  (Certainly the CultureInfo should be cached, things could be better localized, etc):

System.IFormatProvider format = new System.Globalization.CultureInfo("en-US", true);
DateTime lastLogin = DateTime.Parse(Session["lastSignOn"].ToString(),format);
lblLastLogin.Text = String.Format("Your last login was: {0}", lastLogin.ToString("dd-MMM-yy hh:mm:ss tt"));

Note that we know we are getting a the string as a U.S. formatted date time, we pass in our CultureInfo object that implements IFormatProvider to DateTime.Parse, then pass it our custom Format String to the new DateTime object's ToString(). 

This is the kind of higher level layer of abstraction that allows me to focus on business and avoid work.  Parsing DateTimes is administrivia...it's lame-o work.  The Marketects at Microsoft say "Do more with less" but I prefer my own saying "Do more business, do less work."

Put it down sir...step away from the manual string parsing code...no need for strstr() here...

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Bitter XML Quote of the Day

June 11, 2003 Comment on this post [2] Posted in Web Services | XML
Sponsored By

"Without an XML Schema, you might as well replace all those < and > signs with quotes and commas, 'cuz that's what you've got - just less-than/greater-than-delimited text."

I'll be presenting this rant and many other as a PADNUG Web Services SIG meeting sometime this year in a presentation loosely titled: "Mailing a Boot: Exactly how NOT to use XML and Web Services"

Certainly schema isn't required, but when you spend the date passing DateTimes and decimals around in XML, you get used to clients of your XML using the associated schema for simple data typing. 

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

My Graduation Cake

June 10, 2003 Comment on this post [1] Posted in Web Services
Sponsored By

A picture named UPDATED P0006191 (Medium).jpgMy wife used the C# Wallpaper from IrritatedVowel.com on the top of my Graduation cake...the results are pretty snazzy!  Everyone was quite impressed with code on a cake, although lots of not-so-funny-people insisted there were some syntax errors...:)

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Schweet: Free .NET Hosting for Developers

June 10, 2003 Comment on this post [2] Posted in Web Services | ASP.NET | TechEd | Speaking
Sponsored By

Here's a useful and exciting thing to discover on my return from TechEd:  EasyStreeet is offering Free .NET Hosting for Developers.   It's Six Months Free with the .NET Framework 1.1, with ASP.NET, all the BCL (of course), and support for Web Services.

I've been looking around at inexpensive but powerful ASP.NET Hosting to get some of my other small projects off the ground (in addition to www.diabeticbooks.com and www.pioneercourthousesquare.com among others) and this looks like a deal that's too sweet to pass up...might be a nice way to start testing out some blogging and RSS aggregation thoughts, as well as some No-Touch-Deploy WinForms.

All you have to do is email them to get setup:

To submit your request: email your name, company name, email and phone number to NETPlayground@easystreet.com.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

WSDL Cleanliness is next to Godliness (actually Cleavage is next to Cleanliness and Goggles is next to Godliness...)

June 10, 2003 Comment on this post [6] Posted in Web Services | ASP.NET | TechEd | Speaking | XML
Sponsored By

So let me start my own little possee here and cry: Say No to inline schema definitions in WSDL! Import is your friend.... [Commonality]

I'm with Tomas on this one. Schemas should be written as standalone documents and then be imported into the WSDL document. Not only is this easier to manage for the developer writing the schema and WSDL, but it's also easier for those who have to grok the two. Besides, the service you're writing the WSDL for probably isn't the only thing that's going to need access to the types you define in the schemas. [Drew Marsh]

Totally...not only WSDL First! and <xs:import> but (maybe this is obvious) I'm getting in the habit of putting the Domain Object-y definitions that we use elsewhere and the SOAP-y GetDomainObjectResult/Response definitions into separate XSDs.  I preached this at TechEd last week and spoke briefly to Tomas about it although we didn't have enough time to really dig in.

 <wsdl:types>
    <xs:schema>
   <xs:import namespace="
http://banking.corillian.com/Account.xsd" schemaLocation="Account.xsd"/>
   <xs:import namespace="
http://banking.corillian.com/Banking/GetAccounts" schemaLocation="GetAccounts.xsd"/>
  </xs:schema>
 </wsdl:types>

In this small example, the Account.xsd schema contains all sorts of Account-specific definitions that we may pass around internally to our app (on Queues, to functions, as objects, without SOAP) and GetAccounts.xsd contains definitions for GetAccountsResult and GetAccountsResponse and other useful "container" definitions in a separate namespace.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.