0

I have an application that reads 'timed' data out of a file; currently, my input is something like this:

-- EDIT -- I have found an actual case where this fails.

0:0:1:934 > >> GOOD MORNING.<br>

But I seem to be having some trouble parsing this into a valid TimeSpan. I would appreciate if someone could point me in the right direction, as not many offerings that I've found thus far provided much in the way of solving the problem.

As my code stands;

String StoredTime = ArchiveLine.Split('>')[0].TrimEnd();
String StoredFrame = ArchiveLine.Substring(ArchiveLine.IndexOf('>')+1).TrimStart();

TimeSpan FrameTime = TimeSpan.Parse(StoredTime, DateTimeFormatInfo.InvariantInfo);

And it throws a format exception.

Thanks.

DigitalJedi805
  • 1,486
  • 4
  • 16
  • 41
  • Is it you can't parse your target numbers or you can't get a clean set of numbers? – StarPilot Sep 12 '12 at 22:14
  • 3
    What have you tried, and what trouble are you having? That string (without the `>` and whitespace) should be parsed successfully by [TimeSpan.TryParse()](http://msdn.microsoft.com/en-us/library/3z48198e.aspx). – Dan J Sep 12 '12 at 22:15
  • Along with the code you've added, could you give a precise value for `StoredFrame` when you attempt to parse it? Given the first example in your question, I can't tell what's actually being parsed. – Dan J Sep 12 '12 at 22:21
  • Well I must apologize, I placed a '' placeholder in the line when I wrote it, and it has apparently been absorbed. I'll go back and correct it, but that is really not the issue I'm having, as I stated. – DigitalJedi805 Sep 12 '12 at 22:23
  • Fair enough, but I'm still not sure we have enough information to answer the question. The string `00:00:25.1634393` should be valid, according to the TimeSpan.Parse documentation, but notice that the fractional-second portion is limited to seven digits. If you aren't exceeding that, I've no idea what the problem is. – Dan J Sep 12 '12 at 22:30
  • See answer below. Don't be surprised if this gets deleted... it would probably be appopriate. – DigitalJedi805 Sep 12 '12 at 22:35
  • See edits above, I found a use case where this has failed. – DigitalJedi805 Sep 12 '12 at 22:50

2 Answers2

3

It may be a localisation issue. Some cultures use a comma instead of a period as the decimal point. Try:

TimeSpan FrameTime = TimeSpan.Parse(StoredTime, DateTimeFormatInfo.InvariantInfo); 
Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
  • If this doesn't get deleted for the simple fact that it is primarily a typo, this is likely the answer I was looking for. I have implemented it in this fashion and it is working, whether or not it wasn't actually working before, we'll never know. – DigitalJedi805 Sep 12 '12 at 22:43
0

The problem here is that (I) referenced the wrong portion of my line in the Parse method...

DigitalJedi805
  • 1,486
  • 4
  • 16
  • 41