/// <summary> /// Converts a /// <see cref="T:System.DateTime"/> /// object into a String the represents the elapsed time /// relatively to the present. /// </summary> /// <param name="value">The given date and time.</param> /// <param name="targetType"> /// The type corresponding to the binding property, which must be of /// <see cref="T:System.String"/>. /// </param> /// <param name="parameter">(Not used).</param> /// <param name="culture"> /// The culture to use in the converter. /// When not specified, the converter uses the current culture /// as specified by the system locale. /// </param> /// <returns>The given date and time as a String.</returns> public Object Convert(Object value, Type targetType, Object parameter, String language) { var timeStamp = (Int32)value; var unixDateTime = new DateTime(1970, 1, 1, 0, 0, 0); unixDateTime = unixDateTime.AddSeconds(timeStamp); unixDateTime = unixDateTime.ToLocalTime(); // Target value must be a System.DateTime object. //if (!(value is DateTime)) //{ // throw new ArgumentException("Invalid type. Argument must be of the type System.DateTime."); //} SetLocalizationCulture(); String result; DateTime given = unixDateTime; DateTime current = DateTime.Now; TimeSpan difference = current - given; if (DateTimeFormatHelper.IsFutureDateTime(current, given)) { // Future dates and times are not supported, but to prevent crashing an app // if the time they receive from a server is slightly ahead of the phone's clock // we'll just default to the minimum, which is "2 seconds ago". result = GetPluralTimeUnits(2, PluralSecondStrings); } if (difference.TotalSeconds > Year) { // "over a year ago" result = "一年多前"; } else if (difference.TotalSeconds > (1.5 * Month)) { // "x months ago" Int32 nMonths = (Int32)((difference.TotalSeconds + Month / 2) / Month); result = GetPluralMonth(nMonths); } else if (difference.TotalSeconds >= (3.5 * Week)) { // "about a month ago" result = "约 1 个月前"; } else if (difference.TotalSeconds >= Week) { Int32 nWeeks = (Int32)(difference.TotalSeconds / Week); if (nWeeks > 1) { // "x weeks ago" result = String.Format(CultureInfo.CurrentUICulture, "{0} 周前", nWeeks.ToString()); } else { // "about a week ago" result = "约 1 周前"; } } else if (difference.TotalSeconds >= (5 * Day)) { // "last <dayofweek>" result = GetLastDayOfWeek(given.DayOfWeek); } else if (difference.TotalSeconds >= Day) { // "on <dayofweek>" result = GetOnDayOfWeek(given.DayOfWeek); } else if (difference.TotalSeconds >= (2 * Hour)) { // "x hours ago" Int32 nHours = (Int32)(difference.TotalSeconds / Hour); result = GetPluralTimeUnits(nHours, PluralHourStrings); } else if (difference.TotalSeconds >= Hour) { // "about an hour ago" result = "约 1 小时前"; } else if (difference.TotalSeconds >= (2 * Minute)) { // "x minutes ago" Int32 nMinutes = (Int32)(difference.TotalSeconds / Minute); result = GetPluralTimeUnits(nMinutes, PluralMinuteStrings); } else if (difference.TotalSeconds >= Minute) { // "about a minute ago" result = "约 1 分钟前"; } else { // "x seconds ago" or default to "2 seconds ago" if less than two seconds. Int32 nSeconds = ((Int32)difference.TotalSeconds > 1.0) ? (Int32)difference.TotalSeconds : 2; result = GetPluralTimeUnits(nSeconds, PluralSecondStrings); } return(result); }
public object Convert(object value, Type targetType, object parameter, string language) { PluralHourStrings = new string[4] { XHoursAgo_2To4, XHoursAgo_EndsIn1Not11, XHoursAgo_EndsIn2To4Not12To14, XHoursAgo_Other }; PluralMinuteStrings = new string[4] { XMinutesAgo_2To4, XMinutesAgo_EndsIn1Not11, XMinutesAgo_EndsIn2To4Not12To14, XMinutesAgo_Other }; PluralSecondStrings = new string[4] { XSecondsAgo_2To4, XSecondsAgo_EndsIn1Not11, XSecondsAgo_EndsIn2To4Not12To14, XSecondsAgo_Other }; string result; DateTime given; DateTime.TryParse((string)value, out given); given = given.ToLocalTime(); DateTime current = DateTime.Now; TimeSpan difference = current - given; if (DateTimeFormatHelper.IsFutureDateTime(current, given)) { // Future dates and times are not supported, but to prevent crashing an app // if the time they receive from a server is slightly ahead of the phone's clock // we'll just default to the minimum, which is "2 seconds ago". result = GetPluralTimeUnits(2, PluralSecondStrings); } if (difference.TotalSeconds > Year) { // "over a year ago" result = "over a year ago"; } else if (difference.TotalSeconds > (1.5 * Month)) { // "x months ago" int nMonths = (int)((difference.TotalSeconds + Month / 2) / Month); result = GetPluralMonth(nMonths); } else if (difference.TotalSeconds >= (3.5 * Week)) { // "about a month ago" result = "about a month ago"; } else if (difference.TotalSeconds >= Week) { int nWeeks = (int)(difference.TotalSeconds / Week); if (nWeeks > 1) { // "x weeks ago" result = string.Format(XWeeksAgo_2To4, nWeeks.ToString()); } else { // "about a week ago" result = "about a week ago"; } } else if (difference.TotalSeconds >= (5 * Day)) { // "last <dayofweek>" result = GetLastDayOfWeek(given.DayOfWeek); } else if (difference.TotalSeconds >= Day) { // "on <dayofweek>" result = GetOnDayOfWeek(given.DayOfWeek); } else if (difference.TotalSeconds >= (2 * Hour)) { // "x hours ago" int nHours = (int)(difference.TotalSeconds / Hour); result = GetPluralTimeUnits(nHours, PluralHourStrings); } else if (difference.TotalSeconds >= Hour) { // "about an hour ago" result = "about an hour ago"; } else if (difference.TotalSeconds >= (2 * Minute)) { // "x minutes ago" int nMinutes = (int)(difference.TotalSeconds / Minute); result = GetPluralTimeUnits(nMinutes, PluralMinuteStrings); } else if (difference.TotalSeconds >= Minute) { // "about a minute ago" result = "about a minute ago"; } else { // "x seconds ago" or default to "2 seconds ago" if less than two seconds. int nSeconds = ((int)difference.TotalSeconds > 1.0) ? (int)difference.TotalSeconds : 2; result = GetPluralTimeUnits(nSeconds, PluralSecondStrings); } return(result); }