Exemple #1
0
        /// <summary>
        /// Gets the time from a
        /// <see cref="T:System.DateTime"/>
        /// object in short Metro format.
        /// </summary>
        /// <remarks>
        /// Metro design guidelines normalize strings to lowercase.
        /// </remarks>
        /// <param name="dt">Time information.</param>
        /// <returns>"10:20p" for 10:20 p.m. when en-US.</returns>
        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());
        }
Exemple #2
0
 /// <summary>
 /// Gets the abbreviated day from a
 /// <see cref="T:System.DateTime"/>
 /// object.
 /// </summary>
 /// <param name="dt">Date information.</param>
 /// <returns>e.g. "Mon" for Monday when en-US.</returns>
 public static string GetAbbreviatedDay(DateTime dt)
 {
     if (DateTimeFormatHelper.IsCurrentCultureJapanese() || DateTimeFormatHelper.IsCurrentCultureKorean())
     {
         return("(" + dt.ToString("ddd", CultureInfo.CurrentCulture) + ")");
     }
     else
     {
         return(dt.ToString("ddd", CultureInfo.CurrentCulture));
     }
 }
Exemple #3
0
        /// <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 culture)
        {
            // Target value must be a System.DateTime object.
            if (!(value is DateTime))
            {
                throw new ArgumentException(TimeResources.GetString("InvalidDateTimeArgument"));
            }

            string result;

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

            DateTime current = DateTime.Now;

            TimeSpan difference = current - given;

            SetLocalizationCulture(PreferredCulture);

            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 = TimeResources.GetString("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 = TimeResources.GetString("AboutAMonthAgo");
            }
            else if (difference.TotalSeconds >= Week)
            {
                int nWeeks = (int)(difference.TotalSeconds / Week);
                if (nWeeks > 1)
                {
                    // "x weeks ago"
                    result = string.Format(PreferredCulture, TimeResources.GetString("XWeeksAgo_2To4"), nWeeks.ToString(PreferredCulture));
                }
                else
                {
                    // "about a week ago"
                    result = TimeResources.GetString("AboutAWeekAgo");
                }
            }
            else if (difference.TotalSeconds >= (5 * Day))
            {
                // "last <dayofweek>"
                result = string.Format(PreferredCulture, TimeResources.GetString("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 = TimeResources.GetString("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 = TimeResources.GetString("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);
        }