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
.
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);
}
}
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.
So to sum up:
- 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.