33 lines
854 B
C#
33 lines
854 B
C#
using System;
|
|
using System.Linq;
|
|
|
|
namespace DeukBot4.Utilities
|
|
{
|
|
public static class TimespanParser
|
|
{
|
|
public static TimeSpan? Parse(string s)
|
|
{
|
|
var timeIndicator = s.Last();
|
|
var numberStr = s.Remove(s.Length - 1, 1);
|
|
if (!int.TryParse(numberStr, out var number))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
switch (timeIndicator)
|
|
{
|
|
case 's':
|
|
return TimeSpan.FromSeconds(number);
|
|
case 'm':
|
|
return TimeSpan.FromMinutes(number);
|
|
case 'h':
|
|
return TimeSpan.FromHours(number);
|
|
case 'd':
|
|
return TimeSpan.FromDays(number);
|
|
default:
|
|
return null;
|
|
}
|
|
|
|
}
|
|
}
|
|
} |