/// <summary> /// Convert the duration instance to a text description with the specified maximum unit of time /// </summary> /// <param name="maxUnit">The maximum unit of time that should be represented in the returned string</param> /// <returns>Returns the duration description</returns> /// <remarks>The conversion to string breaks the duration down by largest to smallest unit (years down to /// seconds). This version allows you to specify the maximum unit of time that should be represented. /// Any time in excess of the maximum time unit will be returned in the maximum time unit (i.e. PT1D will /// be returned as 24 hours for a maximum time unit of hours, P1M2W5D will be returned as 7 weeks for a /// maximum time unit of weeks if a month is defined as 30 days).</remarks> public string ToDescription(MaxUnit maxUnit) { StringBuilder sb = new StringBuilder(50); Duration d = new Duration(this.Ticks); int years, months, weeks, days, hours, minutes, seconds; if (d.Ticks < 0) { d.TimeSpan = d.TimeSpan.Negate(); } switch (maxUnit) { case MaxUnit.Months: years = 0; months = (int)(d.TimeSpan.Days / Duration.DaysInOneMonth); weeks = d.TimeSpan.Days - (int)(months * Duration.DaysInOneMonth); days = d.TimeSpan.Days - (int)(months * Duration.DaysInOneMonth) - (int)(weeks * 7); hours = d.TimeSpan.Hours; minutes = d.TimeSpan.Minutes; seconds = d.TimeSpan.Seconds; break; case MaxUnit.Weeks: years = months = 0; weeks = (int)(d.TimeSpan.Days / 7); days = d.TimeSpan.Days - (weeks * 7); hours = d.TimeSpan.Hours; minutes = d.TimeSpan.Minutes; seconds = d.TimeSpan.Seconds; break; case MaxUnit.Days: years = months = weeks = 0; days = d.TimeSpan.Days; hours = d.TimeSpan.Hours; minutes = d.TimeSpan.Minutes; seconds = d.TimeSpan.Seconds; break; case MaxUnit.Hours: years = months = weeks = days = 0; hours = (int)d.TimeSpan.TotalHours; minutes = d.TimeSpan.Minutes; seconds = d.TimeSpan.Seconds; break; case MaxUnit.Minutes: years = months = weeks = days = hours = 0; minutes = (int)d.TimeSpan.TotalMinutes; seconds = d.TimeSpan.Seconds; break; case MaxUnit.Seconds: years = months = weeks = days = hours = minutes = 0; seconds = (int)d.TimeSpan.TotalSeconds; break; default: // Max unit = Years, all parts specified years = d.Years; months = d.Months; weeks = d.Weeks; days = d.Days; hours = d.TimeSpan.Hours; minutes = d.TimeSpan.Minutes; seconds = d.TimeSpan.Seconds; break; } if (years != 0) { sb.Append(years); sb.Append(LR.GetString((years == 1) ? "DurYear" : "DurYears")); } if (months != 0) { sb.Append(", "); sb.Append(months); sb.Append(LR.GetString((months == 1) ? "DurMonth" : "DurMonths")); } if (weeks != 0) { sb.Append(", "); sb.Append(weeks); sb.Append(LR.GetString((weeks == 1) ? "DurWeek" : "DurWeeks")); } if (days != 0) { sb.Append(", "); sb.Append(days); sb.Append(LR.GetString((days == 1) ? "DurDay" : "DurDays")); } // Append time if needed if (sb.Length == 0 || hours != 0 || minutes != 0 || seconds != 0) { if (hours != 0) { sb.Append(", "); sb.Append(hours); sb.Append(LR.GetString((hours == 1) ? "DurHour" : "DurHours")); } if (minutes != 0) { sb.Append(", "); sb.Append(minutes); sb.Append(LR.GetString((minutes == 1) ? "DurMinute" : "DurMinutes")); } // Always append seconds if there's nothing else if (seconds != 0 || sb.Length == 0) { sb.Append(", "); sb.Append(seconds); sb.Append(LR.GetString((seconds == 1) ? "DurSecond" : "DurSeconds")); } } // Remove the leading comma separator if it's there if (sb[0] == ',') { sb.Remove(0, 2); } // Negative duration? if (this.Ticks < 0) { sb.Insert(0, ' '); sb.Insert(0, LR.GetString("DurNegative")); } return(sb.ToString()); }
/// <summary> /// Convert the duration instance to a text description with the specified maximum unit of time /// </summary> /// <param name="maxUnit">The maximum unit of time that should be represented in the returned string</param> /// <returns>Returns the duration description</returns> /// <remarks>The conversion to string breaks the duration down by largest to smallest unit (years down to /// seconds). This version allows you to specify the maximum unit of time that should be represented. /// Any time in excess of the maximum time unit will be returned in the maximum time unit (i.e. PT1D will /// be returned as 24 hours for a maximum time unit of hours, P1M2W5D will be returned as 7 weeks for a /// maximum time unit of weeks if a month is defined as 30 days).</remarks> public string ToDescription(MaxUnit maxUnit) { StringBuilder sb = new StringBuilder(50); Duration d = new Duration(this.Ticks); int years, months, weeks, days, hours, minutes, seconds; if(d.Ticks < 0) d.TimeSpan = d.TimeSpan.Negate(); switch(maxUnit) { case MaxUnit.Months: years = 0; months = (int)(d.TimeSpan.Days / Duration.DaysInOneMonth); weeks = d.TimeSpan.Days - (int)(months * Duration.DaysInOneMonth); days = d.TimeSpan.Days - (int)(months * Duration.DaysInOneMonth) - (int)(weeks * 7); hours = d.TimeSpan.Hours; minutes = d.TimeSpan.Minutes; seconds = d.TimeSpan.Seconds; break; case MaxUnit.Weeks: years = months = 0; weeks = (int)(d.TimeSpan.Days / 7); days = d.TimeSpan.Days - (weeks * 7); hours = d.TimeSpan.Hours; minutes = d.TimeSpan.Minutes; seconds = d.TimeSpan.Seconds; break; case MaxUnit.Days: years = months = weeks = 0; days = d.TimeSpan.Days; hours = d.TimeSpan.Hours; minutes = d.TimeSpan.Minutes; seconds = d.TimeSpan.Seconds; break; case MaxUnit.Hours: years = months = weeks = days = 0; hours = (int)d.TimeSpan.TotalHours; minutes = d.TimeSpan.Minutes; seconds = d.TimeSpan.Seconds; break; case MaxUnit.Minutes: years = months = weeks = days = hours = 0; minutes = (int)d.TimeSpan.TotalMinutes; seconds = d.TimeSpan.Seconds; break; case MaxUnit.Seconds: years = months = weeks = days = hours = minutes = 0; seconds = (int)d.TimeSpan.TotalSeconds; break; default: // Max unit = Years, all parts specified years = d.Years; months = d.Months; weeks = d.Weeks; days = d.Days; hours = d.TimeSpan.Hours; minutes = d.TimeSpan.Minutes; seconds = d.TimeSpan.Seconds; break; } if(years != 0) { sb.Append(years); sb.Append(LR.GetString((years == 1) ? "DurYear" : "DurYears")); } if(months != 0) { sb.Append(", "); sb.Append(months); sb.Append(LR.GetString((months == 1) ? "DurMonth" : "DurMonths")); } if(weeks != 0) { sb.Append(", "); sb.Append(weeks); sb.Append(LR.GetString((weeks == 1) ? "DurWeek" : "DurWeeks")); } if(days != 0) { sb.Append(", "); sb.Append(days); sb.Append(LR.GetString((days == 1) ? "DurDay" : "DurDays")); } // Append time if needed if(sb.Length == 0 || hours != 0 || minutes != 0 || seconds != 0) { if(hours != 0) { sb.Append(", "); sb.Append(hours); sb.Append(LR.GetString((hours == 1) ? "DurHour" : "DurHours")); } if(minutes != 0) { sb.Append(", "); sb.Append(minutes); sb.Append(LR.GetString((minutes == 1) ? "DurMinute" : "DurMinutes")); } // Always append seconds if there's nothing else if(seconds != 0 || sb.Length == 0) { sb.Append(", "); sb.Append(seconds); sb.Append(LR.GetString((seconds == 1) ? "DurSecond" : "DurSeconds")); } } // Remove the leading comma separator if it's there if(sb[0] == ',') sb.Remove(0, 2); // Negative duration? if(this.Ticks < 0) { sb.Insert(0, ' '); sb.Insert(0, LR.GetString("DurNegative")); } return sb.ToString(); }
/// <summary> /// Convert the duration instance to its ISO 8601 string form with the specified maximum unit of time /// </summary> /// <param name="maxUnit">The maximum unit of time that should be represented in the returned string</param> /// <returns>Returns the duration in ISO 8601 format</returns> /// <remarks>The conversion to string breaks the duration down by largest to smallest unit (years down to /// seconds). This version allows you to specify the maximum unit of time that should be represented. /// Any time in excess of the maximum time unit will be returned in the maximum time unit (i.e. PT1D will /// be returned as P24H for a maximum time unit of hours, P1M2W5D will be returned as P7W for a maximum /// time unit of weeks if a month is defined as 30 days).</remarks> public string ToString(MaxUnit maxUnit) { StringBuilder sb = new StringBuilder("P", 50); Duration d = new Duration(this.Ticks); int years, months, weeks, days, hours, minutes, seconds; if (d.Ticks < 0) { d.TimeSpan = d.TimeSpan.Negate(); } switch (maxUnit) { case MaxUnit.Months: years = 0; months = (int)(d.TimeSpan.Days / Duration.DaysInOneMonth); weeks = d.TimeSpan.Days - (int)(months * Duration.DaysInOneMonth); days = d.TimeSpan.Days - (int)(months * Duration.DaysInOneMonth) - (int)(weeks * 7); hours = d.TimeSpan.Hours; minutes = d.TimeSpan.Minutes; seconds = d.TimeSpan.Seconds; break; case MaxUnit.Weeks: years = months = 0; weeks = (int)(d.TimeSpan.Days / 7); days = d.TimeSpan.Days - (weeks * 7); hours = d.TimeSpan.Hours; minutes = d.TimeSpan.Minutes; seconds = d.TimeSpan.Seconds; break; case MaxUnit.Days: years = months = weeks = 0; days = d.TimeSpan.Days; hours = d.TimeSpan.Hours; minutes = d.TimeSpan.Minutes; seconds = d.TimeSpan.Seconds; break; case MaxUnit.Hours: years = months = weeks = days = 0; hours = (int)d.TimeSpan.TotalHours; minutes = d.TimeSpan.Minutes; seconds = d.TimeSpan.Seconds; break; case MaxUnit.Minutes: years = months = weeks = days = hours = 0; minutes = (int)d.TimeSpan.TotalMinutes; seconds = d.TimeSpan.Seconds; break; case MaxUnit.Seconds: years = months = weeks = days = hours = minutes = 0; seconds = (int)d.TimeSpan.TotalSeconds; break; default: // Max unit = Years, all parts specified years = d.Years; months = d.Months; weeks = d.Weeks; days = d.Days; hours = d.TimeSpan.Hours; minutes = d.TimeSpan.Minutes; seconds = d.TimeSpan.Seconds; break; } if (years != 0) { sb.AppendFormat("{0}Y", years); } if (months != 0) { sb.AppendFormat("{0}M", months); } if (weeks != 0) { sb.AppendFormat("{0}W", weeks); } if (days != 0) { sb.AppendFormat("{0}D", days); } // Append time if needed if (sb.Length == 1 || hours != 0 || minutes != 0 || seconds != 0) { sb.Append('T'); if (hours != 0) { sb.AppendFormat("{0}H", hours); } if (minutes != 0) { sb.AppendFormat("{0}M", minutes); } // Always append seconds if there's nothing else if (seconds != 0 || sb.Length == 2) { sb.AppendFormat("{0}S", seconds); } } // Negative duration? if (this.Ticks < 0) { sb.Insert(0, '-'); } return(sb.ToString()); }
/// <summary> /// Convert the duration instance to its ISO 8601 string form with the specified maximum unit of time /// </summary> /// <param name="maxUnit">The maximum unit of time that should be represented in the returned string</param> /// <returns>Returns the duration in ISO 8601 format</returns> /// <remarks>The conversion to string breaks the duration down by largest to smallest unit (years down to /// seconds). This version allows you to specify the maximum unit of time that should be represented. /// Any time in excess of the maximum time unit will be returned in the maximum time unit (i.e. PT1D will /// be returned as P24H for a maximum time unit of hours, P1M2W5D will be returned as P7W for a maximum /// time unit of weeks if a month is defined as 30 days).</remarks> public string ToString(MaxUnit maxUnit) { StringBuilder sb = new StringBuilder("P", 50); Duration d = new Duration(this.Ticks); int years, months, weeks, days, hours, minutes, seconds; if(d.Ticks < 0) d.TimeSpan = d.TimeSpan.Negate(); switch(maxUnit) { case MaxUnit.Months: years = 0; months = (int)(d.TimeSpan.Days / Duration.DaysInOneMonth); weeks = d.TimeSpan.Days - (int)(months * Duration.DaysInOneMonth); days = d.TimeSpan.Days - (int)(months * Duration.DaysInOneMonth) - (int)(weeks * 7); hours = d.TimeSpan.Hours; minutes = d.TimeSpan.Minutes; seconds = d.TimeSpan.Seconds; break; case MaxUnit.Weeks: years = months = 0; weeks = (int)(d.TimeSpan.Days / 7); days = d.TimeSpan.Days - (weeks * 7); hours = d.TimeSpan.Hours; minutes = d.TimeSpan.Minutes; seconds = d.TimeSpan.Seconds; break; case MaxUnit.Days: years = months = weeks = 0; days = d.TimeSpan.Days; hours = d.TimeSpan.Hours; minutes = d.TimeSpan.Minutes; seconds = d.TimeSpan.Seconds; break; case MaxUnit.Hours: years = months = weeks = days = 0; hours = (int)d.TimeSpan.TotalHours; minutes = d.TimeSpan.Minutes; seconds = d.TimeSpan.Seconds; break; case MaxUnit.Minutes: years = months = weeks = days = hours = 0; minutes = (int)d.TimeSpan.TotalMinutes; seconds = d.TimeSpan.Seconds; break; case MaxUnit.Seconds: years = months = weeks = days = hours = minutes = 0; seconds = (int)d.TimeSpan.TotalSeconds; break; default: // Max unit = Years, all parts specified years = d.Years; months = d.Months; weeks = d.Weeks; days = d.Days; hours = d.TimeSpan.Hours; minutes = d.TimeSpan.Minutes; seconds = d.TimeSpan.Seconds; break; } if(years != 0) sb.AppendFormat("{0}Y", years); if(months != 0) sb.AppendFormat("{0}M", months); if(weeks != 0) sb.AppendFormat("{0}W", weeks); if(days != 0) sb.AppendFormat("{0}D", days); // Append time if needed if(sb.Length == 1 || hours != 0 || minutes != 0 || seconds != 0) { sb.Append('T'); if(hours != 0) sb.AppendFormat("{0}H", hours); if(minutes != 0) sb.AppendFormat("{0}M", minutes); // Always append seconds if there's nothing else if(seconds != 0 || sb.Length == 2) sb.AppendFormat("{0}S", seconds); } // Negative duration? if(this.Ticks < 0) sb.Insert(0, '-'); return sb.ToString(); }