示例#1
0
        public static string GetSuperShortTime(DateTime dt)
        {
            if (formatInfo_GetSuperShortTime == null)
            {
                lock ( lock_GetSuperShortTime )
                {
                    StringBuilder result = new StringBuilder(string.Empty);
                    string        seconds;

                    formatInfo_GetSuperShortTime = ( DateTimeFormatInfo )CultureInfo.CurrentCulture.DateTimeFormat.Clone();

                    result.Append(formatInfo_GetSuperShortTime.LongTimePattern);
                    seconds = rxSeconds.Match(result.ToString()).Value;
                    result.Replace(" ", string.Empty);
                    result.Replace(seconds, string.Empty);
                    if (!(DateTimeFormatHelper.IsCurrentCultureJapanese() ||
                          DateTimeFormatHelper.IsCurrentCultureKorean() ||
                          DateTimeFormatHelper.IsCurrentCultureHungarian()))
                    {
                        result.Replace(DoubleMeridiemDesignator, SingleMeridiemDesignator);
                    }

                    formatInfo_GetSuperShortTime.ShortTimePattern = result.ToString();
                }
            }

            return(dt.ToString("t", formatInfo_GetSuperShortTime).ToLowerInvariant());
        }
        /// <summary>
        /// Converts a
        /// <see cref="T:System.DateTime"/>
        /// object into a string appropriately formatted for daily feeds.
        /// This format can be found in the call history.
        /// </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">(Not used).</param>
        /// <returns>The given date and time as a string.</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Target value must be a System.DateTime object.
            if (!(value is DateTime))
            {
                throw new ArgumentException(Properties.Resources.InvalidDateTimeArgument);
            }

            StringBuilder result = new StringBuilder(string.Empty);

            DateTime given = (DateTime)value;

            DateTime current = DateTime.Now;

            if (DateTimeFormatHelper.IsFutureDateTime(current, given))
            {
                // Future dates and times are not supported.
                throw new NotSupportedException(Properties.Resources.NonSupportedDateTime);
            }

            if (DateTimeFormatHelper.IsAtLeastOneWeekOld(current, given))
            {
                result.Append(DateTimeFormatHelper.GetShortDate(given));
            }
            else
            {
                result.AppendFormat(CultureInfo.CurrentCulture, "{0} {1}",
                                    DateTimeFormatHelper.GetAbbreviatedDay(given),
                                    DateTimeFormatHelper.GetSuperShortTime(given));
            }

            return(result.ToString());
        }
示例#3
0
 public static string GetAbbreviatedDay(DateTime dt)
 {
     if (DateTimeFormatHelper.IsCurrentCultureJapanese() || DateTimeFormatHelper.IsCurrentCultureKorean())
     {
         return("(" + dt.ToString("ddd", CultureInfo.CurrentCulture) + ")");
     }
     else
     {
         return(dt.ToString("ddd", CultureInfo.CurrentCulture));
     }
 }
        /// <summary>
        /// Converts a
        /// <see cref="T:System.DateTime"/>
        /// object into a string appropriately formatted for hourly feeds.
        /// This format can be found in messaging.
        /// </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">(Not used).</param>
        /// <returns>The given date and time as a string.</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Target value must be a System.DateTime object.
            if (!(value is DateTime))
            {
                throw new ArgumentException(Properties.Resources.InvalidDateTimeArgument);
            }

            StringBuilder result = new StringBuilder(string.Empty);

            DateTime given = (DateTime)value;

            DateTime current = DateTime.Now;

            if (DateTimeFormatHelper.IsFutureDateTime(current, given))
            {
                // Future dates and times are not supported.
                throw new NotSupportedException(Properties.Resources.NonSupportedDateTime);
            }

            if (DateTimeFormatHelper.IsAnOlderYear(current, given))
            {
                result.AppendFormat(CultureInfo.CurrentCulture, "{0}, {1}",
                                    DateTimeFormatHelper.GetShortDate(given),
                                    DateTimeFormatHelper.GetSuperShortTime(given));
            }
            else if (DateTimeFormatHelper.IsAnOlderWeek(current, given))
            {
                result.AppendFormat(CultureInfo.CurrentCulture, "{0}, {1}",
                                    DateTimeFormatHelper.GetMonthAndDay(given),
                                    DateTimeFormatHelper.GetSuperShortTime(given));
            }
            else if (DateTimeFormatHelper.IsPastDayOfWeekWithWindow(current, given))
            {
                result.AppendFormat(CultureInfo.CurrentCulture, "{0}, {1}",
                                    DateTimeFormatHelper.GetAbbreviatedDay(given),
                                    DateTimeFormatHelper.GetSuperShortTime(given));
            }
            else
            {
                // Given day time is today.
                result.Append(DateTimeFormatHelper.GetSuperShortTime(given));
            }

            if (DateTimeFormatHelper.IsCurrentUICultureFrench())
            {
                result.Replace(",", string.Empty);
            }

            return(result.ToString());
        }
        /// <summary>
        /// Converts a
        /// <see cref="T:System.DateTime"/>
        /// object into a string appropriately formatted to
        /// display full date and time information.
        /// This format can be found in email.
        /// </summary>
        /// <remarks>
        /// This format never displays the year.
        /// </remarks>
        /// <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">(Not used).</param>
        /// <returns>The given date and time as a string.</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Target value must be a System.DateTime object.
            if (!(value is DateTime))
            {
                throw new ArgumentException(Properties.Resources.InvalidDateTimeArgument);
            }

            StringBuilder result = new StringBuilder(string.Empty);

            DateTime given = (DateTime)value;

            if (DateTimeFormatHelper.IsCurrentCultureJapanese() || DateTimeFormatHelper.IsCurrentCultureKorean())
            {
                result.AppendFormat(CultureInfo.CurrentCulture, "{0} {1} {2}",
                                    DateTimeFormatHelper.GetMonthAndDay(given),
                                    DateTimeFormatHelper.GetAbbreviatedDay(given),
                                    DateTimeFormatHelper.GetShortTime(given));
            }
            else if (DateTimeFormatHelper.IsCurrentCultureTurkish())
            {
                result.AppendFormat(CultureInfo.CurrentCulture, "{0}, {1} {2}",
                                    DateTimeFormatHelper.GetMonthAndDay(given),
                                    DateTimeFormatHelper.GetAbbreviatedDay(given),
                                    DateTimeFormatHelper.GetShortTime(given));
            }
            else
            {
                result.AppendFormat(CultureInfo.CurrentCulture, "{0} {1}, {2}",
                                    DateTimeFormatHelper.GetAbbreviatedDay(given),
                                    DateTimeFormatHelper.GetMonthAndDay(given),
                                    DateTimeFormatHelper.GetShortTime(given));
            }

            if (DateTimeFormatHelper.IsCurrentUICultureFrench())
            {
                result.Replace(",", string.Empty);
            }

            return(result.ToString());
        }
        /// <summary>
        /// Converts a
        /// <see cref="T:System.DateTime"/>
        /// object into a string appropriately formatted for list-views.
        /// This format can be found in email.
        /// </summary>
        /// <remarks>
        /// This format never displays the year.
        /// </remarks>
        /// <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">(Not used).</param>
        /// <returns>The given date and time as a string.</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Target value must be a System.DateTime object.
            if (!(value is DateTime))
            {
                throw new ArgumentException(Properties.Resources.InvalidDateTimeArgument);
            }

            string result;

            DateTime given = (DateTime)value;

            DateTime current = DateTime.Now;

            if (DateTimeFormatHelper.IsFutureDateTime(current, given))
            {
                // Future dates and times are not supported.
                throw new NotSupportedException(Properties.Resources.NonSupportedDateTime);
            }

            if (DateTimeFormatHelper.IsAnOlderWeek(current, given))
            {
                result = DateTimeFormatHelper.GetMonthAndDay(given);
            }
            else if (DateTimeFormatHelper.IsPastDayOfWeek(current, given))
            {
                result = DateTimeFormatHelper.GetAbbreviatedDay(given);
            }
            else
            {
                // Given day time is today.
                result = DateTimeFormatHelper.GetSuperShortTime(given);
            }

            return(result);
        }
        /// <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, CultureInfo culture)
        {
            // Target value must be a System.DateTime object.
            if (!(value is DateTime))
            {
                throw new ArgumentException(Properties.Resources.InvalidDateTimeArgument);
            }

            string result;

            DateTime given = ((DateTime)value).ToLocalTime();

            DateTime current = DateTime.Now;

            TimeSpan difference = current - given;

            SetLocalizationCulture(culture);

            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 = ControlResources.OverAYearAgo;
            }
            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 = ControlResources.AboutAMonthAgo;
            }
            else if (difference.TotalSeconds >= Week)
            {
                int nWeeks = (int)(difference.TotalSeconds / Week);
                if (nWeeks > 1)
                {
                    // "x weeks ago"
                    result = string.Format(CultureInfo.CurrentUICulture, ControlResources.XWeeksAgo_2To4, nWeeks.ToString(ControlResources.Culture));
                }
                else
                {
                    // "about a week ago"
                    result = ControlResources.AboutAWeekAgo;
                }
            }
            else if (difference.TotalSeconds >= (5 * Day))
            {
                // "last <dayofweek>"
                result = string.Format(CultureInfo.CurrentUICulture, ControlResources.LastDayOfWeek, GetDayOfWeek(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 = ControlResources.AboutAnHourAgo;
            }
            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 = ControlResources.AboutAMinuteAgo;
            }
            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);
        }