Convert seconds to compound duration: Difference between revisions
Content added Content deleted
m (→{{header|Perl 6}}: escaping newline is not required anymore) |
|||
Line 409: | Line 409: | ||
Number entered: 6000000 |
Number entered: 6000000 |
||
9 wk, 6 d, 10 hr, 40 min |
9 wk, 6 d, 10 hr, 40 min |
||
</pre> |
|||
=={{header|C sharp}}== |
|||
<lang csharp>using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
namespace ConvertSecondsToCompoundDuration |
|||
{ |
|||
class Program |
|||
{ |
|||
static void Main( string[] args ) |
|||
{ |
|||
foreach ( string arg in args ) |
|||
{ |
|||
int duration ; |
|||
bool isValid = int.TryParse( arg , out duration ) ; |
|||
if ( !isValid ) { Console.Error.WriteLine( "ERROR: Not an integer: {0}" , arg ) ; } |
|||
if ( duration < 0 ) { Console.Error.WriteLine( "ERROR: duration must be non-negative" , arg ) ; } |
|||
Console.WriteLine(); |
|||
Console.WriteLine( "{0:#,##0} seconds ==> {1}" , duration , FormatAsDuration(duration) ) ; |
|||
} |
|||
return ; |
|||
} |
|||
private static string FormatAsDuration( int duration ) |
|||
{ |
|||
if ( duration < 0 ) throw new ArgumentOutOfRangeException("duration") ; |
|||
return string.Join( ", " , GetDurationParts(duration) ) ; |
|||
} |
|||
private static IEnumerable<string> GetDurationParts( int duration ) |
|||
{ |
|||
var parts = new[] |
|||
{ |
|||
new { Name="wk" , Length = 7*24*60*60*1 , } , |
|||
new { Name="d" , Length = 24*60*60*1 , } , |
|||
new { Name="h" , Length = 60*60*1 , } , |
|||
new { Name="m" , Length = 60*1 , } , |
|||
new { Name="s" , Length = 1 , } , |
|||
} ; |
|||
foreach ( var part in parts ) |
|||
{ |
|||
int n = Math.DivRem( duration , part.Length , out duration ) ; |
|||
if ( n > 0 ) yield return string.Format( "{0} {1}" , n , part.Name ) ; |
|||
} |
|||
} |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
7,259 seconds ==> 2 h, 59 s |
|||
86,400 seconds ==> 1 d |
|||
6,000,000 seconds ==> 9 wk, 6 d, 10 h, 40 m |
|||
</pre> |
</pre> |
||