Exemplo n.º 1
0
        /// <summary>
        /// Gets the culture name associated with a specified <see cref="CultureKind"/>.
        /// </summary>
        /// <remarks>
        /// Dictionary entries generated via unit test.
        /// </remarks>
        /// <param name="cultureKind">The culture kind.</param>
        /// <returns>
        /// The culture name associated with a specified <see cref="CultureKind"/>.
        /// </returns>
        public static string ToCultureName(
            this CultureKind cultureKind)
        {
            if (cultureKind == CultureKind.Unknown)
            {
                throw new ArgumentOutOfRangeException(nameof(cultureKind), Invariant($"{nameof(cultureKind)} is {nameof(CultureKind)}.{nameof(CultureKind.Unknown)}"));
            }

            if (!CultureKindToCultureNameMap.ContainsKey(cultureKind))
            {
                throw new NotSupportedException(Invariant($"This {nameof(CultureKind)} is not supported: {cultureKind}."));
            }

            var result = CultureKindToCultureNameMap[cultureKind];

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the <see cref="CultureInfo"/> associated with a specified <see cref="CultureKind"/>.
        /// </summary>
        /// <param name="cultureKind">The culture kind.</param>
        /// <returns>
        /// The <see cref="CultureInfo"/> associated with a specified <see cref="CultureKind"/>.
        /// </returns>
        public static CultureInfo ToCultureInfo(
            this CultureKind cultureKind)
        {
            if (cultureKind == CultureKind.Unknown)
            {
                throw new ArgumentOutOfRangeException(nameof(cultureKind), Invariant($"{nameof(cultureKind)} is {nameof(CultureKind)}.{nameof(CultureKind.Unknown)}"));
            }

            if (CachedCultureKindToCultureInfoMap.ContainsKey(cultureKind))
            {
                return(CachedCultureKindToCultureInfoMap[cultureKind]);
            }

            var cultureName = cultureKind.ToCultureName();

            var result = new CultureInfo(cultureName);

            CachedCultureKindToCultureInfoMap.TryAdd(cultureKind, result);

            return(result);
        }
        /// <summary>
        /// Converts the value of the current <see cref="DateTime"/> object to its equivalent
        /// string representation using the specified format and culture-specific format information.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="formatKind">The kind of formatting to apply.</param>
        /// <param name="cultureKind">OPTIONAL kind of culture to use.  DEFAULT is to use the invariant culture.</param>
        /// <returns>
        /// A string representation of the specified <see cref="DateTime"/> value as specified by format and culture.
        /// </returns>
        public static string ToString(
            this DateTime value,
            DateTimeFormatKind formatKind,
            CultureKind cultureKind = CultureKind.Invariant)
        {
            if (formatKind == DateTimeFormatKind.Unknown)
            {
                throw new ArgumentOutOfRangeException(nameof(formatKind), Invariant($"{nameof(formatKind)} is {nameof(DateTimeFormatKind)}.{nameof(DateTimeFormatKind.Unknown)}"));
            }

            if (cultureKind == CultureKind.Unknown)
            {
                throw new ArgumentOutOfRangeException(nameof(cultureKind), Invariant($"{nameof(cultureKind)} is {nameof(CultureKind)}.{nameof(CultureKind.Unknown)}"));
            }

            var formatString = formatKind.ToFormatString();

            var cultureInfo = cultureKind.ToCultureInfo();

            var result = value.ToString(formatString, cultureInfo);

            return(result);
        }