处理和存储经过的时间

发布于 2024-07-05 15:38:09 字数 289 浏览 4 评论 0原文

我在决定处理和存储时间测量的最佳方法时遇到问题。

我有一个应用程序,它有一个文本框,允许用户以 hh:mm:ss 或 mm:ss 格式输入时间。

因此,我计划解析这个字符串,在冒号上标记它并为我的业务逻辑创建 TimeSpan (或使用 TimeSpan.Parse() 并仅在 mm:ss 情况下添加“00:”)。 好的?

我如何将其存储在数据库中? 字段类型是什么? 日期时间似乎错误。 我不希望时间 00:54:12 存储为 1901-01-01 00:54:12 这看起来有点差?

I'm having problems deciding on what is the best way is to handle and store time measurements.

I have an app that has a textbox that allows the users to input time in either hh:mm:ss or mm:ss format.

So I was planning on parsing this string, tokenizing it on the colons and creating TimeSpan (or using TimeSpan.Parse() and just adding a "00:" to the mm:ss case) for my business logic. Ok?

How do I store this as in a database though? What would the field type be? DateTime seems wrong. I don't want a time of 00:54:12 to be stored as 1901-01-01 00:54:12 that seems a bit poor?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

眉黛浅 2024-07-12 15:38:09

大多数数据库都有某种时间间隔类型。 答案取决于您所讨论的数据库。 对于 Oracle 来说,它只是一个代表天数(包括小数天数)的浮点 NUMBER。 您可以在任何 DATE 类型中添加/减去该值,然后得到正确的答案。

Most databases have some sort of time interval type. The answer depends on which database you're talking about. For Oracle, it's just a floating point NUMBER that represents the number of days (including fractional days). You can add/subtract that to/from any DATE type and you get the right answer.

信愁 2024-07-12 15:38:09

我认为最简单的是将用户输入转换为整数秒。 所以 54:12 == 3252 秒,所以将 3252 存储在数据库或任何地方。 然后当您需要将其显示给用户时,您可以再次将其转换回来。

I think the simplest is to just convert user input into a integer number of seconds. So 54:12 == 3252 seconds, so store the 3252 in your database or wherever. Then when you need to display it to the user, you can convert it back again.

秋心╮凉 2024-07-12 15:38:09

对于少于一天的时间,只需像其他人所说的那样使用秒。

对于较长时间,这取决于您的数据库引擎。 如果是 SQL Server,2008 之前的版本您需要一个日期时间。 没关系 - 您可以忽略默认的 1/1/1900 日期。 如果您有幸拥有 sql server 2008,那么您可以使用单独的日期和时间数据类型。 使用真实日期时间/时间类型的优点是使用 DateDiff 函数来比较持续时间。

For periods less than a day, just use seconds as other have said.

For longer periods, it depends on your db engine. If SQL Server, prior to version 2008 you want a datetime. It's okay- you can just ignore the default 1/1/1900 date they'll all have. If you are fortunate enough to have sql server 2008, then there are separate Date and Time datatypes you can use. The advantage with using a real datetime/time type is the use of the DateDiff function for comparing durations.

鹿童谣 2024-07-12 15:38:09

TimeSpan 有一个可以存储的 Int64 Ticks 属性相反,以及一个 构造函数刻度值。

TimeSpan has an Int64 Ticks property that you can store instead, and a constructor that takes a Ticks value.

靖瑶 2024-07-12 15:38:09

您是否同时收集开始时间和停止时间? 如果是这样,您可以使用“时间戳”数据类型(如果您的 DBMS 支持)。 如果没有,就作为日期/时间类型。 现在,您已经说过您不希望存储日期部分 - 但请考虑时间段跨越午夜的情况 - 例如,您从 23:55:01 开始到 00:05:14 结束 - 除非你那里也有日期。 有标准内置函数可返回两个日期时间值之间经过的时间(以秒为单位)。

Are you collecting both the start time and stop time? If so, you could use the "timestamp" data type, if your DBMS supports that. If not, just as a date/time type. Now, you've said you don't want the date part to be stored - but consider the case where the time period spans midnight - you start at 23:55:01 and end at 00:05:14, for example - unless you also have the date in there. There are standard build in functions to return the elapsed time (in seconds) between two date-time values.

伴我心暖 2024-07-12 15:38:09

作为秒的整数计数(或适当的毫秒)

As an integer count of seconds (or Milliseconds as appropriate)

风柔一江水 2024-07-12 15:38:09

int 类型应该做到这一点,将其存储为秒并来回解析它

http ://msdn.microsoft.com/en-us/library/ms187745.aspx

and int type should do it, storing it as seconds and parsing it back and forth

http://msdn.microsoft.com/en-us/library/ms187745.aspx

还不是爱你 2024-07-12 15:38:09

使用整数表示秒或分钟。 秒可能更好。 你永远不会因为选择过于精确的东西而责备自己。 另外,对于您的 UI,请考虑使用多个文本输入,您不必担心用户实际正确输入“:”。 添加其他约束(例如包含 0-59 的分和秒值)也更容易。

Go with integers for seconds or minutes. Seconds is probably better. you'll never kick yourself for choosing something with too much precision. Also, for your UI, consider using multiple text inputs you don't have to worry about the user actually typing in the ":" properly. It's also much easier to add other constraints such as the minute and second values conting containing 0-59.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文