/EnhancedReminders/EnhancedReminders/Code/EnhancedReminders/DispatcherService/ITimespanFormatter.cs

# · C# · 226 lines · 85 code · 22 blank · 119 comment · 1 complexity · 392ed3cb962bcfc6228d1a13e07cfbcb MD5 · raw file

  1. //++
  2. //
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. //
  5. // Module Name:
  6. //
  7. // ITimeSpanFormatter.cs
  8. //
  9. // Abstract:
  10. //
  11. // Interface describing a class intended for formatting DateTimes for printing
  12. // in reminders.
  13. //--
  14. using System;
  15. using System.Globalization;
  16. namespace Microsoft.EnhancedReminders.Dispatcher
  17. {
  18. /// <summary>
  19. /// Interface describing a class intended for formatting DateTimes for printing
  20. /// in reminders.
  21. /// </summary>
  22. public interface ITimeSpanFormatter
  23. {
  24. /// <summary>
  25. /// Adds friendly text for a time span for an overdue appointment or meeting. This typically
  26. /// has a form similar to "you have a meeting that started at 12:00 PM"
  27. /// </summary>
  28. /// <param name="cultureInfo">The culture to print the timespan in</param>
  29. /// <param name="startTime">The time the meeting/appointment occurs at</param>
  30. string OverdueTimeSpan(CultureInfo cultureInfo, DateTime startTime);
  31. /// <summary>
  32. /// Adds friendly text for a time span for an appointment or meeting that starts in less than an hour.
  33. /// This typically has a form similar to "you have a meeting in 20 minutes."
  34. /// </summary>
  35. /// <param name="cultureInfo">The culture to print the timespan in</param>
  36. /// <param name="span">A TimeSpan representing the lead time to the meeting or appointment</param>
  37. string ImmediateTimeSpan(CultureInfo cultureInfo, TimeSpan span);
  38. /// <summary>
  39. /// Adds friendly text for a time span for an appointment or meeting that starts sometime today, but later
  40. /// than an hour. This typically has a form similar to "you have a meeting at 12:00 PM"
  41. /// </summary>
  42. /// <param name="cultureInfo">The culture to print the timespan in</param>
  43. /// <param name="startTime">The time the meeting/appointment occurs at</param>
  44. string TimeSpanToday(CultureInfo cultureInfo, DateTime startTime);
  45. /// <summary>
  46. /// Adds friendly text for a time span for an appointment or meeting that starts sometime tomorrow.
  47. /// This typically has a form similar to "you have a meeting tomorrow at 12:00 PM"
  48. /// </summary>
  49. /// <param name="cultureInfo">The culture to print the timespan in</param>
  50. /// <param name="startTime">The time the meeting/appointment occurs at</param>
  51. string TimeSpanTomorrow(CultureInfo cultureInfo, DateTime startTime);
  52. /// <summary>
  53. /// Adds friendly text for a time span for an appointment or meeting that starts sometime later than tomorrow.
  54. /// This typically has a form similar to "you have a meeting on Monday, October 12 at 12:00 PM"
  55. /// </summary>
  56. /// <param name="cultureInfo">The culture to print the time span in</param>
  57. /// <param name="startTime">The time the meeting/appointment occurs at</param>
  58. string DistantTimeSpan(CultureInfo cultureInfo, DateTime startTime);
  59. }
  60. /// <summary>
  61. /// ITimespanFormatter implementation for printing time spans for Instant Message Reminders. This
  62. /// class is also intended to be a base class for other ITimeSpanFormatters that may only need
  63. /// to override a handful of the timespan strings that this class generates.
  64. /// </summary>
  65. public class InstantMessageTimeSpanFormatter : ITimeSpanFormatter
  66. {
  67. #region ITimespanFormatter Members
  68. /// <summary>
  69. /// Adds friendly text for a time span for an overdue appointment or meeting. This typically
  70. /// has a form similar to "you have a meeting that started at 12:00 PM"
  71. /// </summary>
  72. /// <param name="cultureInfo">The culture to print the timespan in</param>
  73. /// <param name="startTime">The time the meeting/appointment occurs at</param>
  74. public virtual string OverdueTimeSpan(CultureInfo cultureInfo, DateTime startTime)
  75. {
  76. return string.Format(cultureInfo, IMResources.LeadTime_Present, startTime.ToString("h:mm tt", cultureInfo));
  77. }
  78. /// <summary>
  79. /// Adds friendly text for a time span for an appointment or meeting that starts in less than an hour.
  80. /// This typically has a form similar to "you have a meeting in 20 minutes."
  81. /// </summary>
  82. /// <param name="cultureInfo">The culture to print the timespan in</param>
  83. /// <param name="span">A TimeSpan representing the lead time to the meeting or appointment</param>
  84. public virtual string ImmediateTimeSpan(CultureInfo cultureInfo, TimeSpan span)
  85. {
  86. return string.Format(cultureInfo, IMResources.LeadTime_Future,
  87. string.Format(cultureInfo, Math.Abs(span.Minutes) == 1 ?
  88. IMResources.Minutes_Singular :
  89. IMResources.Minutes_Plural, Math.Abs(span.Minutes)));
  90. }
  91. /// <summary>
  92. /// Adds friendly text for a time span for an appointment or meeting that starts sometime today, but later
  93. /// than an hour. This typically has a form similar to "you have a meeting at 12:00 PM"
  94. /// </summary>
  95. /// <param name="cultureInfo">The culture to print the timespan in</param>
  96. /// <param name="startTime">The time the meeting/appointment occurs at</param>
  97. public virtual string TimeSpanToday(CultureInfo cultureInfo, DateTime startTime)
  98. {
  99. return string.Format(cultureInfo, IMResources.LeadTime_Today, startTime.ToString("h:mm tt", cultureInfo));
  100. }
  101. /// <summary>
  102. /// Adds friendly text for a time span for an appointment or meeting that starts sometime tomorrow.
  103. /// This typically has a form similar to "you have a meeting tomorrow at 12:00 PM"
  104. /// </summary>
  105. /// <param name="cultureInfo">The culture to print the timespan in</param>
  106. /// <param name="startTime">The time the meeting/appointment occurs at</param>
  107. public virtual string TimeSpanTomorrow(CultureInfo cultureInfo, DateTime startTime)
  108. {
  109. return string.Format(cultureInfo, IMResources.LeadTime_Tomorrow, startTime.ToShortTimeString());
  110. }
  111. /// <summary>
  112. /// Adds friendly text for a time span for an appointment or meeting that starts sometime later than tomorrow.
  113. /// This typically has a form similar to "you have a meeting on Monday, October 12 at 12:00 PM"
  114. /// </summary>
  115. /// <param name="cultureInfo">The culture to print the timespan in</param>
  116. /// <param name="startTime">The time the meeting/appointment occurs at</param>
  117. public virtual string DistantTimeSpan(CultureInfo cultureInfo, DateTime startTime)
  118. {
  119. return string.Format(cultureInfo, IMResources.LeadTime_Distant,
  120. startTime.ToString("dddd, MMMM dd", cultureInfo),
  121. startTime.ToString("h:mm tt", cultureInfo));
  122. }
  123. #endregion
  124. }
  125. /// <summary>
  126. /// ITimespanFormatter implementation for printing time spans for Voice Reminders that
  127. /// are delivered via TTS.
  128. /// </summary>
  129. public class SsmlTimeSpanFormatter : InstantMessageTimeSpanFormatter
  130. {
  131. #region ITimeSpanFormatter Members
  132. /// <summary>
  133. /// Adds friendly text for a time span for an overdue appointment or meeting. This typically
  134. /// has a form similar to "you have a meeting that started at 12:00 PM"
  135. /// </summary>
  136. /// <param name="cultureInfo">The culture to print the timespan in</param>
  137. /// <param name="startTime">The time the meeting/appointment occurs at</param>
  138. public override string OverdueTimeSpan(CultureInfo cultureInfo, DateTime startTime)
  139. {
  140. return string.Format(cultureInfo, IMResources.LeadTime_Present,
  141. string.Format(CultureInfo.InvariantCulture, "<say-as type=\"time\">{0}</say-as> {1}",
  142. startTime.ToString("hh:mm", cultureInfo), startTime.ToString("tt", cultureInfo)));
  143. }
  144. /// <summary>
  145. /// Adds friendly text for a time span for an appointment or meeting that starts sometime today, but later
  146. /// than an hour. This typically has a form similar to "you have a meeting at 12:00 PM"
  147. /// </summary>
  148. /// <param name="cultureInfo">The culture to print the timespan in</param>
  149. /// <param name="startTime">The time the meeting/appointment occurs at</param>
  150. public override string TimeSpanToday(CultureInfo cultureInfo, DateTime startTime)
  151. {
  152. return string.Format(cultureInfo, IMResources.LeadTime_Today,
  153. string.Format(CultureInfo.InvariantCulture, "<say-as type=\"time\">{0}</say-as> {1}",
  154. startTime.ToString("hh:mm", cultureInfo), startTime.ToString("tt", cultureInfo)));
  155. }
  156. /// <summary>
  157. /// Adds friendly text for a time span for an appointment or meeting that starts sometime tomorrow.
  158. /// This typically has a form similar to "you have a meeting tomorrow at 12:00 PM"
  159. /// </summary>
  160. /// <param name="cultureInfo">The culture to print the timespan in</param>
  161. /// <param name="startTime">The time the meeting/appointment occurs at</param>
  162. public override string TimeSpanTomorrow(CultureInfo cultureInfo, DateTime startTime)
  163. {
  164. return string.Format(cultureInfo, IMResources.LeadTime_Tomorrow,
  165. string.Format(CultureInfo.InvariantCulture, "<say-as type=\"time\">{0}</say-as> {1}",
  166. startTime.ToString("hh:mm", cultureInfo), startTime.ToString("tt", cultureInfo)));
  167. }
  168. /// <summary>
  169. /// Adds friendly text for a time span for an appointment or meeting that starts sometime later than tomorrow.
  170. /// This typically has a form similar to "you have a meeting on Monday, October 12 at 12:00 PM"
  171. /// </summary>
  172. /// <param name="cultureInfo">The culture to print the timespan in</param>
  173. /// <param name="startTime">The time the meeting/appointment occurs at</param>
  174. public override string DistantTimeSpan(CultureInfo cultureInfo, DateTime startTime)
  175. {
  176. return string.Format(cultureInfo, IMResources.LeadTime_Distant,
  177. string.Format(CultureInfo.InvariantCulture, "{1} <say-as type=\"date:md\">{0}</say-as>",
  178. startTime.ToString("MM-dd", cultureInfo), startTime.ToString("dddd", cultureInfo)),
  179. string.Format(CultureInfo.InvariantCulture, "<say-as type=\"time\">{0}</say-as> {1}",
  180. startTime.ToString("hh:mm", cultureInfo), startTime.ToString("tt", cultureInfo)));
  181. }
  182. #endregion
  183. }
  184. /// <summary>
  185. /// ITimespanFormatter implementation for printing time spans for SMS Reminders
  186. /// </summary>
  187. public class SmsTimeSpanFormatter : InstantMessageTimeSpanFormatter
  188. {
  189. #region ITimeSpanFormatter Members
  190. /// <summary>
  191. /// Adds friendly text for a time span for an appointment or meeting that starts sometime later than tomorrow.
  192. /// This typically has a form similar to "you have a meeting on Monday, October 12 at 12:00 PM"
  193. /// </summary>
  194. /// <param name="cultureInfo">The culture to print the timespan in</param>
  195. /// <param name="startTime">The time the meeting/appointment occurs at</param>
  196. public override string DistantTimeSpan(CultureInfo cultureInfo, DateTime startTime)
  197. {
  198. return string.Format(cultureInfo, IMResources.LeadTime_Distant,
  199. startTime.ToString("ddd, MMM dd", cultureInfo),
  200. startTime.ToString("h:mm tt", cultureInfo));
  201. }
  202. #endregion
  203. }
  204. }