コード例 #1
0
        /// <summary>
        /// Convert to a string.
        /// </summary>
        /// <remarks>
        /// <para>If the format parameter consists of the single character "G" or "g", the string will consist of the value of the <see cref="AbsoluteSeconds"/> property.</para>
        /// <para>Otherwise, the following character sequences in the format parameter will be replaced as follows:</para>
        /// <list type="table">
        /// <listheader>
        /// <term>Format character</term>
        /// <description>Replacement</description>
        /// </listheader>
        /// <item><term>f</term><description>If the seconds component of the time is 30 or greater, a half symbol; otherwise an empty string.</description></item>
        /// <item><term>h</term><description>Hours from 1-12, leading zeros omitted.</description></item>
        /// <item><term>hh</term><description>Hours from 01-12, leading zero preserved.</description></item>
        /// <item><term>H</term><description>Hours from 0-23, leading zeros omitted.</description></item>
        /// <item><term>HH</term><description>Hours from 00-23, leading zero preserved.</description></item>
        /// <item><term>m</term><descrption>Minutes from 0-59, leading zero omitted.</descrption></item>
        /// <item><term>mm</term><description>Minutes from 00-59, leading zero preserved.</description></item>
        /// <item><term>s</term><description>Seconds from 0-59, leading zero omitted.</description></item>
        /// <item><term>ss</term><description>Seconds from 00-59, leading zero preserved.</description></item>
        /// <item><term>t</term><description>The first letter of the AM/PM designator for the relevant culture.</description></item>
        /// <item><term>tt</term><description>The AM/PM designator for the relevant culture.</description></item>
        /// </list>
        /// </remarks>
        /// <param name="format">Format string</param>
        /// <param name="formatProvider">Culture-specific information.</param>
        /// <returns>A string containing the value of the object formatted as per the format string and culture.</returns>
        public string ToString(string format, IFormatProvider formatProvider)
        {
            if (format is null)
            {
                throw new ArgumentNullException(nameof(format));
            }

            if (format == "g" || format == "G")
            {
                return(AbsoluteSeconds.ToString(formatProvider));
            }

            format = format.Replace("hh", Hours12.ToString("d2", formatProvider));
            format = format.Replace("h", Hours12.ToString(formatProvider));
            format = format.Replace("HH", Hours24.ToString("d2", formatProvider));
            format = format.Replace("H", Hours24.ToString(formatProvider));
            format = format.Replace("mm", Minutes.ToString("d2", formatProvider));
            format = format.Replace("m", Minutes.ToString(formatProvider));
            format = format.Replace("ss", Seconds.ToString("d2", formatProvider));
            format = format.Replace("s", Seconds.ToString(formatProvider));
            format = format.Replace("f", Seconds >= 30 ? "½" : string.Empty);
            if (format.Contains("t"))
            {
                string desig = GetDesignator(formatProvider, false);
                format = format.Replace("tt", desig);
                if (!desig.Contains("t"))
                {
                    format = format.Replace("t", desig.Substring(0, 1));
                }
            }
            if (format.Contains("T"))
            {
                string desig = GetDesignator(formatProvider, true);
                format = format.Replace("TT", desig);
                if (!desig.Contains("T"))
                {
                    format = format.Replace("T", desig.Substring(0, 1));
                }
            }

            return(format);
        }