/// <summary>
        /// Formats the specified DateTime to a time string representation.
        /// </summary>
        /// <param name="value">The DateTime that should be formatted.</param>
        /// <param name="timeFormat">The time format used to describe how value
        /// should be formatted.</param>
        /// <param name="timeCharacters">The allowed characters in the format.
        /// Leave empty to indicate that all characters are allowed. See remarks.</param>
        /// <returns>
        /// A string that represents the time part of a DateTime.
        /// </returns>
        /// <remarks>The TimeFormat will contain TimeCharacters in a certain
        /// order, like hh:mm:ss. By passing specific TimeCharacters, these
        /// will get filtered and the method only returns part of the formatted
        /// string. Example: pass 'h', 't', 'H' to get back 4 AM, if the culture
        /// was set to en-US.</remarks>
        public virtual string FormatTime(DateTime?value, ITimeFormat timeFormat, params char[] timeCharacters)
        {
            if (value.HasValue)
            {
                if (timeFormat == null)
                {
                    throw new ArgumentNullException("timeFormat");
                }

                if (timeCharacters.Count() > 0)
                {
                    // if timeCharacters is used, only allow those characters.
                    string filtered = new string(timeFormat.GetTimeDisplayFormat(ActualCulture)
                                                 .ToCharArray()
                                                 .Where(c => timeCharacters.Contains(c))
                                                 .Select(c => c)
                                                 .ToArray());
                    // empty timeformat defaults to long datestring,
                    // not filtering is a better default.
                    if (!string.IsNullOrEmpty(filtered))
                    {
                        timeFormat = new CustomTimeFormat(filtered);
                    }
                }

                string formatted = value.Value.ToString(GetTransformedFormat(timeFormat.GetTimeDisplayFormat(ActualCulture)), ActualCulture);
                // after formatting, allow globalization step to map digits.
                return(new string(formatted.ToCharArray()
                                  .Select(c => Char.IsDigit(c) ? MapDigitToCharacter(Int32.Parse(c.ToString(CultureInfo.InvariantCulture), NumberStyles.Number, CultureInfo.InvariantCulture)) : c)
                                  .ToArray())
                       .Trim());
            }
            return(string.Empty);
        }
        public override object ConvertTo(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                if (value is ShortTimeFormat)
                {
                    return("Short");
                }

                if (value is LongTimeFormat)
                {
                    return("Long");
                }

                CustomTimeFormat customTimeFormat = value as CustomTimeFormat;
                if (customTimeFormat != null)
                {
                    return(customTimeFormat.Format);
                }
            }

            return(TypeConverters.ConvertTo(this, value, destinationType));
        }
        /// <summary>
        /// Formats the specified DateTime to a time string representation.
        /// </summary>
        /// <param name="value">The DateTime that should be formatted.</param>
        /// <param name="timeFormat">The time format used to describe how value
        /// should be formatted.</param>
        /// <param name="timeCharacters">The allowed characters in the format. 
        /// Leave empty to indicate that all characters are allowed. See remarks.</param>
        /// <returns>
        /// A string that represents the time part of a DateTime.
        /// </returns>
        /// <remarks>The TimeFormat will contain TimeCharacters in a certain 
        /// order, like hh:mm:ss. By passing specific TimeCharacters, these
        /// will get filtered and the method only returns part of the formatted
        /// string. Example: pass 'h', 't', 'H' to get back 4 AM, if the culture
        /// was set to en-US.</remarks>
        public virtual string FormatTime(DateTime? value, ITimeFormat timeFormat, params char[] timeCharacters)
        {
            if (value.HasValue)
            {
                if (timeFormat == null)
                {
                    throw new ArgumentNullException("timeFormat");
                }

                if (timeCharacters.Count() > 0)
                {
                    // if timeCharacters is used, only allow those characters.
                    string filtered = new string(timeFormat.GetTimeDisplayFormat(ActualCulture)
                                                     .ToCharArray()
                                                     .Where(c => timeCharacters.Contains(c))
                                                     .Select(c => c)
                                                     .ToArray());
                    // empty timeformat defaults to long datestring, 
                    // not filtering is a better default.
                    if (!string.IsNullOrEmpty(filtered))
                    {
                        timeFormat = new CustomTimeFormat(filtered);
                    }
                }

                string formatted = value.Value.ToString(GetTransformedFormat(timeFormat.GetTimeDisplayFormat(ActualCulture)), ActualCulture);
                // after formatting, allow globalization step to map digits.
                return new string(formatted.ToCharArray()
                    .Select(c => Char.IsDigit(c) ? MapDigitToCharacter(Int32.Parse(c.ToString(CultureInfo.InvariantCulture), NumberStyles.Number, CultureInfo.InvariantCulture)) : c)
                    .ToArray())
                    .Trim();
            }
            return string.Empty;
        }
示例#4
0
        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is
        /// equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare
        /// with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// True if the specified <see cref="T:System.Object"/> is equal
        /// to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">
        /// The <paramref name="obj"/> parameter is null.
        /// </exception>
        public override bool Equals(object obj)
        {
            CustomTimeFormat comparison = obj as CustomTimeFormat;

            return(comparison != null && comparison == this);
        }