views:

78

answers:

5

The custom date strings in .NET allow you to parse a date with seven fractional parts of a second using fffffff I need to parse data exported from Oracle SQL Developer which looks like:

15-OCT-08 15.36.16.280000000

I count nine fractional parts but fffffffff is not a valid date format. In fact:

? DateTime.Now.ToString("dd-MMM-yy HH.mm.ss.fffffffff")

throws an exception with message "Input string was not in a correct format".

How can I elegantly parse my dates?

A: 

I would just right trim the string when parsing it so that there are only 4 decimal places. It looks like the additional data are not significant digits anyway. Then when you write it out, use:

.ToString("dd-MMM-yy HH.mm.ss.ffff")
RedFilter
A: 

If you always get a fixed size string, you could:

var trimmedDate = oracleDate.Substring(0, oracleDate.Length - 2);
var dateFormat = "dd-MMM-yy HH.mm.ss.fffffffff";
DateTime.ParseExact(trimmedDate, dateFormat,  CultureInfo.InvariantCulture)

That could go into a method - ParseOracleDate(oracleDateString)

eglasius
+1  A: 

Why not DateTime.Now.ToString("dd-MMM-yy HH.mm.ss.fffffff00")? The last 100 billionth of the second will not be meaningful anyway.

To parse, use DateTime.ParseExact(s.Substring(0, s.Length - 2));

erikkallen
+1  A: 

This is not elegant, but I bet you can't get anything better. (Well, until Jon or Mark show us)

using System.Globalization;

/* ... */

string d = "15-OCT-08 15.36.16.280000000";
DateTime dt = DateTime.ParseExact(d.Substring(0,d.Length-2),
              "dd-MMM-yy HH.mm.ss.fffffff",CultureInfo.InvariantCulture);
Vinko Vrsalovic
+1 for the CultureInfo.InvariantCulture. It should always be used.
erikkallen
A: 

A DateTime is implemented internally as the count of 100-nanosecond intervals from DateTime.MinValue. That's the highest level of precision possible with this class, so parsing additional digits past this would have no effect.

http://www.eggheadcafe.com/articles/20021111.asp

Steven Sudit