Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

I need to parse a specific date format, as fast as possible,

since there are around a million of them I need to parse in a go.

 

The date format looks like this:

"7/30 17:15:39.140"

"7/30 10:07:03.156"

 

After searching for an example on the MSDN I found this:

http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

 

[CSharp]

string dateString, format;

DateTime result;

CultureInfo provider = CultureInfo.InvariantCulture;

 

// Parse date and time with custom specifier.

dateString = "Sun 15 Jun 2008 8:30 AM -06:00";

format = "ddd dd MMM yyyy h:mm tt zzz";

try {

result = DateTime.ParseExact(dateString, format, provider);

Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());

} catch(FormatException) {

Console.WriteLine("{0} is not in the correct format.", dateString);

}

 

[/CSharp]

 

So I tried to adapt to my needs but don't seem to get it right.

I always get an 'System.FormatException'

 

Here is what I tried:

[CSharp]

string dateString, format;

DateTime result;

CultureInfo provider = CultureInfo.InvariantCulture;

 

// Parse date and time with custom specifier.

dateString = "2008/7/30 13:52:15.765";

format = "yyyy/MM/DD HH:mm:ss.FFF";

try {

result = DateTime.ParseExact(dateString, format, provider);

Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());

} catch(FormatException) {

Console.WriteLine("{0} is not in the correct format.", dateString);

}

[/CSharp]

 

On another note.

Is this faster than:

[CSharp]

string[] TimeStampParts = logEntry.Split(new Char[] { '/', ' ', ':', '.' });

TimeStamp = new DateTime(

(long.Parse(TimeStampParts[5]) +

(long.Parse(TimeStampParts[4]) +

(long.Parse(TimeStampParts[3]) +

(long.Parse(TimeStampParts[2]) +

(long.Parse(TimeStampParts[1]) +

long.Parse(TimeStampParts[0]) * 30) * 24) * 60) * 60) * 1000) * 10000);[/CSharp]

?

 

If not, are there any methods/tricks I can use to make it faster?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...