问题描述
我需要在24小时内使用日程表的应用程序格式的天时间执行一些计算。例如,17小时+ 12H = 5小时。我试图创建自己的时间
struct和环绕时间跨度
或的DateTime
。它的工作确定,但我越来越坚持的格式在的ToString
工作要做。
I need to perform some calculations on times of days in 24 hours formatting in an application using schedules. For example, 17h + 12h = 5h. I tried to create my own Time
struct and wrap around TimeSpan
or DateTime
. It's working ok but I'm getting stuck with the formatting to be done in ToString
.
事实上,我的当然希望我的结构,能够任何文化下工作。起初我想委托的格式为的DateTime
结构,并跳过所有自定义格式是不严格的时间有关。我不能将此任务委托给一个时间跨度
,因为它不处理AM / PM在某些文化中,例如使用。例如,在我的时间
结构我写的:
Indeed, I of course want my struct to be able to work under any culture. At first I was thinking of delegating the formatting to a DateTime
struct and skip all custom formatting that are not strictly time-related. I can't delegate this task to a TimeSpan
because it doesn't handle AM/PM used in some cultures for example. For example, in my Time
struct I wrote:
public string Format(string format, IFormatProvider provider)
{
return TimeFormat.Format(this, format, DateTimeFormatInfo.GetInstance(provider), provider);
}
// ...
internal class TimeFormat
{
internal string Format(Time time, string format, DateTimeFormatInfo dtfi, IFormatProvider provider)
{
DateTime dt = new DateTime(DateTime.MinValue, 1, 1, time.Hours, time.Minutes, time.Seconds, time.Milliseconds);
/* Here I am checking what the 'format' string is made of
and filter-out anything that is not strictly-time related. */
return dt.Format(format, provider);
}
}
我看了一下如何使用<$ C $在的DateTime
结构的C>的ToString 方法落实到mscorlib程序。而waow ......这么多东西要考虑。我想或多或少地改写里面有些什么,但只保留什么是时间相关的,但一切都在这混起来不容易理解。我注意到一些绝对不是简单的东西也处理诸如希伯来格式化等等,这使我的方式更多的时间比我期待改写这一切! 。特别是我不想做没有理解我在做什么明确
I had a look at how the ToString
method of the DateTime
struct is implemented into mscorlib. And waow... So many stuffs to take into account. I wanted to more or less rewrite what was in it but keeping only what is time-related but everything's mixed up in this and not easy to understand. And I noticed some definitely not straightforward stuffs to handle also such as Hebrew formatting etc. It would take me way more time than I was expecting to rewrite all this! Especially I don't want to do it without understanding clearly what I'm doing.
所以,总结一下:
- 如何创建的一天?
- 的上一级,时间是兼容所有的文化格式化我在正确处理问题使用包装的
的DateTime
或时间跨度
?我不能直接使用时间跨度
:它的工作负倍(我使用的模数有严格的时间介于0和23:59:59.999),这不是'在一些文化T柄AM / PM,它使用天
秒。对我来说,'的天的时间'是从'的时间跨度的'。 - 我不需要不同处理时区或DST,也没有闰秒,因为我在一个日历不可知的情况下,但我希望能够处理的一天中的时间的本地化格式。 (用H(或任何希伯来语,中国,泰米尔语或俄语字符),而不是':'或AM / PM,而不是为例如24小时制)
- How can I create an "all cultures compliant" formatting of time of the day?
- One level above, am I handling the problem correctly using wrappers of
DateTime
orTimeSpan
? I can't use directly aTimeSpan
: It's working with negative times (I'm using modulos to have times strictly between 0 and 23:59:59.999), it doesn't handle AM/PM in some cultures, it usesDay
s. For me, a 'time of the day' is different from a 'span of time'. - I don't need to handle time zone or DST, nor leap second as I am in a calendar agnostic context but I want to be able to handle localized formatting of time of the day. (using 'H' (or any Hebrew, Chinese, Tamil or Russian character) instead of ':' or AM/PM instead for 24-hour format for example).
任何提示或帮助指向我一些图书馆或教程会有所帮助!
Any hint or help pointing me to some libraries or tutorials will be helpful!
我一看的还,但它看起来太复杂关于我想达到的目标。
I had a look at this post before asking this question and took a look at this library also but it looks too complex regarding what I want to achieve.
推荐答案
(我知道这是我自己的项目的一个插件,但我希望人们会看到它是完全相关:)
(I realize this is a plug for my own project, but hopefully folks will see it's entirely relevant :)
任何提示或帮助指向我一些图书馆或教程会有所帮助!
我的库,它应该做你想要的主要开发者。请参阅初始信息的 - 和随意问更多的问题这里或在邮件列表上。
I'm the main developer for the Noda Time library, which should do most of what you want. See the user guide for initial information - and feel free to ask more questions either here or on the mailing list.
你想要的类型的。
The type you want is LocalTime
.
和是的,你说得对双方的DateTime
和时间跨度
不恰当这里,也该格式/解析复杂:)
And yes, you're right about both DateTime
and TimeSpan
being inappropriate here, and also that the formatting/parsing is complicated :)
在野田佳彦时,你的可以的使用的ToString
方法与正常BCL类型,有的目前的静态解析
/ 的TryParse
/ ParseExact
/ TryParseExact
方法 - 但这些可能消失今晚:)相反,你应该使用的:一旦定义模式(每格式和文化),然后用其来进行格式化和分析。
In Noda Time you can use the ToString
methods as with the normal BCL types, and there are currently static Parse
/TryParse
/ParseExact
/TryParseExact
methods - but those may well vanish this evening :) Instead, you should use a LocalTimePattern
: define the pattern once (per format and culture), and then use it for both formatting and parsing.
在格式化和解析方面,你需要了解你需要什么很清楚。野田时间小猪背上到BCL的东西,如AM / PM指示符是什么,等等,你只处理时间的一天,这一事实使得它的多的,因为简单AREN ŧ月/日的名称(和不同的日历)的担心。您可以使用 T
模式为短和 T
为长的一个特定的文化 - 或指定自定义模式。自定义模式一般是机器对机器通信更为有用;标准的是面向用户的格式更加有用。
In terms of formatting and parsing, you need to be very clear about what you need. Noda Time piggy-backs onto the BCL for things like what the AM/PM designators are, etc. The fact that you're only dealing with time-of-day makes it much simpler as there aren't month/day names (and different calendars) to worry about. You can use the t
pattern for "short" and T
for "long" for a particular culture - or specify a custom pattern. Custom patterns are generally more useful for machine-to-machine communication; standard ones are more useful for user-oriented formatting.
(目前,你必须知道的 T
和 T
模式的存在 - 尽管他们的用户手册中的记录是当然,在某些时候,我将创建方法,使这个简单的...)
(Currently you have to "know" that the t
and T
patterns exist - although they're documented in the user guide, of course. At some point I'll create methods to make this simpler...)
这篇关于在C#中的结构一天时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!