Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,52 @@
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) ) ;
}
}
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 ) ;
}
}
}
}

View file

@ -0,0 +1,14 @@
private static string ConvertToCompoundDuration(int seconds)
{
if (seconds < 0) throw new ArgumentOutOfRangeException(nameof(seconds));
if (seconds == 0) return "0 sec";
TimeSpan span = TimeSpan.FromSeconds(seconds);
int[] parts = {span.Days / 7, span.Days % 7, span.Hours, span.Minutes, span.Seconds};
string[] units = {" wk", " d", " hr", " min", " sec"};
return string.Join(", ",
from index in Enumerable.Range(0, units.Length)
where parts[index] > 0
select parts[index] + units[index]);
}