-4

It's a simple task that turned into a confusing situation.

2:20 min when placed in like this TimeSpan(0,2,20) and then TimeSpan.TotallSeconds gives you 140 seconds

But when you do the same calculation TimeSpan.FromMinutes(2.20).TotalSeconds it gives 132.

Google and other converters also give 132. So I would assume it is correct answer.

But when you calculate it like this 2 min = 120 sek + 20 sek it is 140 sek.

Is this a bug ? So which one is correct?

  • 4
    "2.20 minutes" means "2.2 * 60 seconds", _not_ "2* 60 + 20 seconds". – Franz Gleichmann Jul 17 '20 at 16:27
  • Two point two minutes (two and one fifth, a minute being 60, that's 60 + 60 + one fifth of 60, i.e. 12) really is 132 seconds. Perhaps you meant to TImeSpan.Parse("2:20") ? Do you live in a country where times are separated by `.`. instead of `:` (if they ever are) ? That might lead you to make the mistake because twelve thirtyfour and fifty six seconds is written as 12.34.56 ?? – Caius Jard Jul 17 '20 at 16:27
  • @CaiusJard Well, the problem occurred when I try to convert video duration to time in seconds. So I get a string form Youtube 2:20, my logic was split(':') it and construct a TimeSpan(0,2,20) and get the total number of seconds from that. But when I did the same on Google it says 132 seconds. Rather than 140 value that I got from the variable earlier. And when I use TimeSpan.FromMinutes(2.20) it is 132. –  Jul 17 '20 at 16:35
  • If youtube is telling you a video is 2:20 long, and you've asked google to convert it and it said 132 then i guess it really depends on what you asked google. 2:20 is surely 140 seconds, 2.2 minutes is surely 132 seconds. I asked bing and it said 140 https://i.stack.imgur.com/1xwPy.png but I was quite explicit about how i wanted it to do the conversion. 2:20 is 2 minutes and 20/60ths of a minute i.e. 2.333333 minutes i.e. 140 seconds. 2.2 is 2 and one fifth of 60s, i,e, 132 seconds. A video that is 2:20 is definitely not 2.2 minutes long. Check how you ask google to confirm your logic. – Caius Jard Jul 17 '20 at 16:38
  • Oh, and try TimeSpan.Parse or ParseExact - for Parse it will assume hh:mm, so either parse 00:02:00 or parseexact and supply a format - https://dotnetfiddle.net/kiAOAU – Caius Jard Jul 17 '20 at 16:42
  • @CaiusJard Thank you for all tips, TimeSpan.Parse will help and work nice in this situation. My mistake I misundersanding of what 2.20 means in minutes –  Jul 17 '20 at 16:48

1 Answers1

1

"2:20 min when placed in like this TimeSpan(0,2,20) and then TimeSpan.TotallSeconds gives you 140 seconds"

Correct. You're passing 0 hours, 2 minutes, and 20 seconds to the constructor. Since a minute is 60 seconds, the total seconds in the span is 60 + 60 + 20 = 140.

"But when you do the same calculation TimeSpan.FromMinutes(2.20).TotalSeconds it gives 132"

2.20 means "two and two tenths" (trailing zeros can be ignored after a decimal, just as leading zeros are ignored before a decimal). So, how many seconds are in a tenth of a minute? Well, since there are 60 seconds in a minute, one-tenth of that is 6 seconds; therefore 2 tenths of 60 seconds is 12 seconds.

So now we have a span of 60 + 60 + 12 = 132 seconds.


There is no bug here, just a misundersanding of what 2.20 means when applied to minutes.

Rufus L
  • 36,127
  • 5
  • 30
  • 43