roadie/Roadie.Api.Library/Utility/TimeInfo.cs
2018-12-26 13:39:13 -06:00

114 lines
No EOL
3 KiB
C#

using Roadie.Library.Extensions;
using System;
namespace Roadie.Library.Utility
{
public sealed class TimeInfo
{
public decimal Days { get; set; }
public string DaysFormatted
{
get
{
if (this.Days > -1)
{
return this.Days.ToString("000");
}
return "--";
}
}
public decimal Hours { get; set; }
public string HoursFormatted
{
get
{
if (this.Hours > -1)
{
return this.Hours.ToString("00");
}
return "--";
}
}
public decimal Minutes { get; set; }
public string MinutesFormatted
{
get
{
if (this.Minutes > -1)
{
return this.Minutes.ToString("00");
}
return "--";
}
}
public decimal Seconds { get; set; }
public string SecondsFormatted
{
get
{
if (this.Seconds > -1)
{
return this.Seconds.ToString("00");
}
return "--";
}
}
public decimal? Years { get; set; }
public string YearsFormatted
{
get
{
if (this.Years.HasValue && this.Years.Value > 0)
{
return this.Years.Value.ToString("0");
}
return null;
}
}
public TimeInfo(decimal milliseconds)
{
var secondsTotal = milliseconds / 1000;
var minutesTotal = Math.Floor(secondsTotal / 60);
var hoursTotal = Math.Floor(minutesTotal / 60);
var daysTotal = Math.Floor(hoursTotal / 24);
var yearsTotal = Math.Floor(daysTotal / 365);
this.Seconds = Math.Floor(secondsTotal - (minutesTotal * 60));
this.Minutes = minutesTotal - (hoursTotal * 60);
this.Hours = hoursTotal - (daysTotal * 24);
this.Days = daysTotal - (yearsTotal * 365);
this.Years = Math.Floor(daysTotal / 365);
}
public string ToFullFormattedString()
{
var yearsFormatted = this.YearsFormatted;
return $"{ (string.IsNullOrEmpty(yearsFormatted) ? string.Empty : yearsFormatted + ":") }{ this.ToString() }";
}
public string ToShortFormattedString()
{
return $"{ MinutesFormatted }:{ SecondsFormatted }";
}
public override string ToString()
{
if(this.Days > 0)
{
return $"{ DaysFormatted}:{ HoursFormatted}:{ MinutesFormatted}:{ SecondsFormatted }";
}
return $"{ HoursFormatted}:{ MinutesFormatted }:{ SecondsFormatted }";
}
}
}