[Roam-commit] SF.net SVN: roam: [126] Trunk/Source/Roam.Common/Utils/TimeUtils.cs
Status: Alpha
Brought to you by:
jonfroehlich
|
From: <jon...@us...> - 2008-03-14 19:31:28
|
Revision: 126
http://roam.svn.sourceforge.net/roam/?rev=126&view=rev
Author: jonfroehlich
Date: 2008-03-14 12:31:26 -0700 (Fri, 14 Mar 2008)
Log Message:
-----------
added GetApproximateAge related methods in TimeUtils
Modified Paths:
--------------
Trunk/Source/Roam.Common/Utils/TimeUtils.cs
Modified: Trunk/Source/Roam.Common/Utils/TimeUtils.cs
===================================================================
--- Trunk/Source/Roam.Common/Utils/TimeUtils.cs 2008-03-11 19:19:54 UTC (rev 125)
+++ Trunk/Source/Roam.Common/Utils/TimeUtils.cs 2008-03-14 19:31:26 UTC (rev 126)
@@ -29,6 +29,152 @@
{
public static readonly TimeSpan TimeSpanInfinite = new TimeSpan(0, 0, 0, 0, -1);
+ private static readonly int DaysInYear = 365;
+ private static readonly int DaysInMonth = 30;
+ private static readonly int DaysInWeek = 7;
+ private static readonly int HoursInDay = 24;
+ private static readonly int MinutesInHour = 60;
+ private static readonly int SecondsInMinute = 60;
+ private static readonly int MillisecondsInSecond = 1000;
+
+ public static string GetApproximateAge(TimeSpan timeSpan)
+ {
+ return GetApproximateAge(timeSpan, 1);
+ }
+
+ /// <summary>
+ /// Very approximate age (doesn't calculate leap year, assumes all months are 30 days long, etc.)
+ /// </summary>
+ /// <param name="timeSpan"></param>
+ /// <param name="maxUnits"></param>
+ /// <returns></returns>
+ public static string GetApproximateAge(TimeSpan timeSpan, int maxUnits)
+ {
+ if (timeSpan.TotalDays > DaysInYear)
+ {
+ double years = timeSpan.TotalDays / DaysInYear;
+ if (maxUnits == 1)
+ {
+ return string.Format("{0:0.0} years", years);
+ }
+ else
+ {
+ int yearsInteger = (int)Math.Floor(years);
+ double fraction = years - yearsInteger;
+ double daysLeft = fraction * DaysInYear;
+ return string.Format("{0} year{1}, {2}",
+ yearsInteger, (yearsInteger == 1) ? "" : "s",
+ GetApproximateAge(TimeSpan.FromDays(daysLeft), maxUnits - 1));
+ }
+ }
+ else if (timeSpan.TotalDays > DaysInMonth)
+ {
+ double months = timeSpan.TotalDays / DaysInMonth;
+ if (maxUnits == 1)
+ {
+ return string.Format("{0:0.0} months", months);
+ }
+ else
+ {
+ int monthsInteger = (int)Math.Floor(months);
+ double fraction = months - monthsInteger;
+ double daysLeft = fraction * DaysInMonth;
+ return string.Format("{0} month{1}, {2}",
+ monthsInteger, (monthsInteger == 1) ? "" : "s",
+ GetApproximateAge(TimeSpan.FromDays(daysLeft), maxUnits - 1));
+ }
+ }
+ else if (timeSpan.TotalDays > DaysInWeek)
+ {
+ double weeks = timeSpan.TotalDays / DaysInWeek;
+ if (maxUnits == 1)
+ {
+ return string.Format("{0:0.0} weeks", weeks);
+ }
+ else
+ {
+ int weeksInteger = (int)Math.Floor(weeks);
+ double fraction = weeks - weeksInteger;
+ double daysLeft = fraction * DaysInWeek;
+ return string.Format("{0} week{1}, {2}",
+ weeksInteger, (weeksInteger == 1) ? "" : "s",
+ GetApproximateAge(TimeSpan.FromDays(daysLeft), maxUnits - 1));
+ }
+ }
+ else if (timeSpan.TotalDays > 1)
+ {
+ double days = timeSpan.TotalDays;
+ if (maxUnits == 1)
+ {
+ return string.Format("{0:0.0} days", days);
+ }
+ else
+ {
+ int daysInteger = (int)Math.Floor(days);
+ double fraction = days - daysInteger;
+ double hoursLeft = fraction;
+ return string.Format("{0} day{1}, {2}",
+ daysInteger, (daysInteger == 1) ? "" : "s",
+ GetApproximateAge(TimeSpan.FromHours(hoursLeft), maxUnits - 1));
+ }
+ }
+ else if (timeSpan.TotalHours > 1)
+ {
+ double hours = timeSpan.TotalHours;
+ if (maxUnits == 1)
+ {
+ return string.Format("{0:0.0} hours", hours);
+ }
+ else
+ {
+ int hoursInteger = (int)Math.Floor(hours);
+ double fraction = hours - hoursInteger;
+ double minutesLeft = fraction;
+ return string.Format("{0} hour{1}, {2}",
+ hoursInteger, (hoursInteger == 1) ? "" : "s",
+ GetApproximateAge(TimeSpan.FromMinutes(minutesLeft), maxUnits - 1));
+ }
+ }
+ else if (timeSpan.TotalMinutes > 1)
+ {
+ double minutes = timeSpan.TotalMinutes;
+ if (maxUnits == 1)
+ {
+ return string.Format("{0:0.0} minutes", minutes);
+ }
+ else
+ {
+ int minutesInteger = (int)Math.Floor(minutes);
+ double fraction = minutes - minutesInteger;
+ double secondsLeft = fraction;
+ return string.Format("{0} minute{1}, {2}",
+ minutesInteger, (minutesInteger == 1) ? "" : "s",
+ GetApproximateAge(TimeSpan.FromSeconds(secondsLeft), maxUnits - 1));
+ }
+ }
+ else if (timeSpan.TotalSeconds > 1)
+ {
+ double seconds = timeSpan.TotalSeconds;
+ if (maxUnits == 1)
+ {
+ return string.Format("{0:0.0} seconds", seconds);
+ }
+ else
+ {
+ int secondsInteger = (int)Math.Floor(seconds);
+ double fraction = seconds - secondsInteger;
+ double millisecondsLeft = fraction;
+ return string.Format("{0} second{1}, {2}",
+ secondsInteger, (secondsInteger == 1) ? "" : "s",
+ GetApproximateAge(TimeSpan.FromMilliseconds(millisecondsLeft), maxUnits - 1));
+ }
+ }
+ else
+ {
+ return string.Format("{0:0.0} milliseconds", timeSpan.TotalMilliseconds);
+ }
+ }
+
public static TimeSpan Interval(DateTime dt1, DateTime dt2)
{
return (TimeSpan)(dt2 - dt1);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|