为什么TimeSpan.ToString()要求转义分隔符? [英] Why does TimeSpan.ToString() require escaping separators?

查看:1611
本文介绍了为什么TimeSpan.ToString()要求转义分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以指定一个像这样的的DateTime 对象的自定义格式:

You can specify a custom format for a DateTime object like this:

DateTime.Now.ToString("HH:mm:ss");   // 19:55:23

但是当我尝试使用相同的格式,像这样的时间跨度对象:

DateTime.Now.TimeOfDay.ToString("HH:mm:ss");

我得到的输入字符串的不正确的格式。例外。

原来,解决的办法是,你需要躲避:像字符HH \\\\:\\\\毫米: SS。请注意,有一个双反斜线,因为如果只指定一个,它会打破字符串,所以你一定要逃逸一个了。

It turns out, the solution is that you need to escape the ':' characters like in "HH\\:mm\\:ss". Note that there is a double backslash because if you specify only one, it will break the string so you need to escape that one too.

现在的问题是,为什么.NET框架开发者所做的这种方式?必须有一定的理由。为什么我们不能用自定义格式说明不喜欢,我们可以逃避他们一个的DateTime 对象吗?

The question is, why .NET Framework developers made it this way? There must be a reason for sure. Why can't we use custom format specifiers without escaping them like we can with a DateTime object?

寻找.NET大师们对这个问题线索。

Looking for .NET gurus to shed light on this subject.

推荐答案

作为的文档则DateTime.ToString TimeSpan.ToString 之间的区别之一格式说明如下:自定义时间跨度格式说明不包括占位符分隔符号,如符号,从小时分开的日子,从分钟,或分数秒秒小时。相反,这些符号必须包括在自定义格式字符串作为字符串

As stated in documentation, one of the differences between DateTime.ToString and TimeSpan.ToString format specifiers is the following: the custom TimeSpan format specifiers do not include placeholder separator symbols, such as the symbols that separate days from hours, hours from minutes, or seconds from fractional seconds. Instead, these symbols must be included in the custom format string as string literals.

对比时间跨度(见文档格式说明表),的DateTime 格式说明包括predefined符号日期分隔符 / ,和时间分隔符。这意味着,例如意大利文化分号将被识别为时间分隔符(不字面),将与替换符号:

In contrast with TimeSpan (see table of format specifiers in docs), DateTime format specifiers include predefined symbols for Date separator /, and for Time separator :. It means that for example for Italian culture semicolon will be recognized as time separator (not the literal) and will be replaced with . symbol:

    // outputs 09.57.18 instead of 09:57:18 because of Italian culture.
    Console.WriteLine(DateTime.Now.ToString("hh:mm:ss", CultureInfo.GetCultureInfo("it-IT")));

我觉得.NET设计的DateTime 时间跨度字符串格式化之间作出这样的差异故意的,它是安静合理。这是因为历史上的日期/时间分别为不同的文化不同的格式。和.NET试图提供全球化意味着为此事与的DateTime 键入一起。但时间跨度并没有得到这样的全球化的职责,它仅仅是一种可再$ P的时间$ psenting时期,它的格式是不依赖于任何文化的细节(如果它们曾经存在),而是格式化的是在不同的文化设置常数。

I think .NET designers made such difference between DateTime and TimeSpan string formatters intentionally, and it is quiet reasonable. This is because historically Date/Time were formatted differently for different cultures. And .NET tried to provide globalization means for that matter along with DateTime type. But TimeSpan did not get such 'globalization' duties, it is just a type representing period of time, and formatting of it is not tied to any culture specifics (if they are ever existed), but instead formatting of it is constant in different culture settings.

这篇关于为什么TimeSpan.ToString()要求转义分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆