Answer the question
In order to leave comments, you need to log in
Which type is best for keeping time?
I need to store the start and end times of the establishment. What type is best for this? For example, DateTime does not even have a purely time-based constructor - the date must be specified anyway. And TimeSpan is not logically suitable IMHO - after all, I need to store a specific time (for example, opening at 9:00, closing at 18:00), and not a time interval ...
UPD. I was misunderstood. I asked exactly in C# which type to use for this. And not how it will look in the database. Int is inconvenient to use.
Answer the question
In order to leave comments, you need to log in
int. The number of seconds since some point. For example - since midnight.
Judging by your task, TimeSpan is the most suitable for you. It is strange that you discarded it, while considering DateTime, because inside this structure, the time is defined as:
In addition, TimeSpan stores not just an interval, but the amount of time elapsed since midnight.
I would also use TimeSpan, or wrap it in my own type. As an alternative, use the LocalTime type from the Noda Time library , which is just designed to store the time of day. But this is a cannon for sparrows.
TimeSpan is ideal for storing specific times. It does not store a period, but a specific time.
In general, if you need to store exactly pairs of times (beginning and end), and the end can be the next day (for example, from 9 am to 1 am) or you need to work with timezones, then you most likely need your own type. You can store pairs of times in it, check which day the desired time belongs to (for example, if you need to find out which shift the time 0:30 belongs to), etc.
Looking what to do with this time.
For example, if you have the exact date and time, then manually writing a generic increment function is a VERY big pain! If it is necessary to operate with large intervals of time, then an integer number of seconds must be stored in a 64-bit int.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question