簡體   English   中英

指定 DateTime.ToString(“o”) 十進制精度

[英]Specifying DateTime.ToString(“o”) decimal precision

我有一個 DateTime 對象,我試圖將它輸出到一個符合 ISO 8601 格式的 xml 文件中,以便在兩個系統之間傳輸——我們無法控制接收者。 .net 往返格式大部分滿足此要求,但將精度強制為 7dp。

有沒有辦法可以指定這個? 例如像"o:0"這樣的東西完全省略毫秒小數位或"o:3"將其設置為3dp。 我知道我可以自己格式化輸出yyyy'-'MM'-'dd'T'HH':'mm':'ssK (或yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffK分別)作為自定義格式說明符以在必要時控制小數位,但想看看我是否錯過了簡單地將小數精度傳遞給"o"格式說明符的方法。

似乎答案是否定的,沒有任何東西(至少在核心框架中)可以自定義它。 查看DateTime往返格式源代碼,這是硬編碼為 7dp:

// Omitted for brevity
...
AppendHHmmssTimeOfDay(result, dateTime);
result.Append('.');

long fraction = dateTime.Ticks % TimeSpan.TicksPerSecond;
AppendNumber(result, fraction, 7);

如果有人感興趣,我的解決方案是實現自定義ToFormattedString()擴展方法來處理此問題,如有必要,它將用自定義字符串替換格式字符串,並使用該方法調用ToString

using System.Globalization; // System.Globalization needed for IFormatProvider overload
using System.Text; // System.Text required for StringBuilder class

namespace Extensions
{
    public static class DateTimeFormatExtension
    {
        // Consts for building the custom format string
        private const string ROUNDTRIP_FORMAT_PREFIX = "yyyy'-'MM'-'dd'T'HH':'mm':'ss";
        private const char ROUNDTRIP_FORMAT_FRACTION = 'f';
        private const char ROUNDTRIP_FORMAT_SUFFIX = 'K';

        // Appending the 'f' custom format string maxes out at "fffffff"(7 dp) and will throw an exception if given more 
        private const int DATETIME_MAX_DP = 7;

        private static int GetRoundtripLength(int decimalPlaces) =>
            ROUNDTRIP_FORMAT_PREFIX.Length + decimalPlaces + 2; // +2 to account for the '.' and the 'K' suffix

        public static string ToFormattedString(this DateTime input) => input.ToString();

        public static string ToFormattedString(this DateTime input, string format)
        {
            var provider = DateTimeFormatInfo.CurrentInfo;
            return input.ToFormattedString(format, provider);
        }

                    public static string ToFormattedString(this DateTime input, string format, IFormatProvider provider)
        {
            string parsedFormat = format;
            if (!string.IsNullOrWhiteSpace(format))
            {
                switch (format[0])
                {
                    case 'o':
                    case 'O':
                        var precision = format.Substring(1);
                        // Only do this if we have a custom 'o' string, otherwise us the base functionality
                        if (!string.IsNullOrWhiteSpace(precision))
                        {
                            // If the custom addition to the format string is an integer, use that to determine dp
                            if (int.TryParse(precision, out int decimalCount))
                            {
                                // Build the format string
                                var formatBuilder = new StringBuilder(GetRoundtripLength(decimalCount));
                                formatBuilder.Append(ROUNDTRIP_FORMAT_PREFIX);

                                // Append '.' and 'f' chars to format string (Append nothing if 0 dp)
                                if (decimalCount > 0)
                                {
                                    formatBuilder
                                        .Append('.')
                                        .Append(ROUNDTRIP_FORMAT_FRACTION,
                                            // Cap max dp length to avoid exceptions
                                            Math.Min(decimalCount, DATETIME_MAX_DP));
                                }

                                // Append 'K' suffix
                                formatBuilder.Append(ROUNDTRIP_FORMAT_SUFFIX);
                                parsedFormat = formatBuilder.ToString();
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
            return input.ToString(parsedFormat, provider);
        }
    }
}

然后可以像這樣使用:

using Extensions;
...
DateTime currentDateTime = DateTime.UtcNow;
string result;
result = currentDateTime.ToFormattedString(); // empty just calls the default ToString()
// result: "2018-03-02 12:31:17 AM"
result = currentDateTime.ToFormattedString("dd/MM/yy ssmmhh"); // custom format strings still work
// result: "02-03-18 173112"
result = currentDateTime.ToFormattedString("d"); // standard format strings still work
// result: "2018-03-02"
result = currentDateTime.ToFormattedString("D");
// result: "Friday, March 2, 2018"
result = currentDateTime.ToFormattedString("F");
// result: "Friday, March 2, 2018 12:31:17 AM"
result = currentDateTime.ToFormattedString("o"); // standard format specifier uses default ToString("o") behaviour
// result: "2018-03-02T00:31:17.9818727Z"
result = currentDateTime.ToFormattedString("o0"); // no decimal places
// result: "2018-03-02T00:31:17Z"
result = currentDateTime.ToFormattedString("o3"); // 3 decimal places
// result: "2018-03-02T00:31:17.981Z"
result = currentDateTime.ToFormattedString("o100"); // too many decimals cap at 7
// result: "2018-03-02T00:31:17.9818727Z"

編輯:更新以從格式字符串中刪除“:”,以符合在數字格式上指定小數精度的方式:

標准數字格式字符串采用Axx形式,其中:

A是稱為格式說明符的單個字母字符。

...

xx是一個可選整數,稱為精度說明符

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM