开发者

Represent a time with no date in ruby

Is there a way to represent a time in ruby without having a Date attach开发者_如何转开发ed? I am doing a timetracking application and I only need in/out times, there is a separate column for date.

The MySQL TIME data type stores only the time, but in Rails it comes back as Jan 1 2000 + what ever time is there.

Should i just ignore the date part, or is there a way to eliminate it?


We just store times with no attached dates as minutes since midnight in an integer column (that's Postgres not MySQL but nonetheless). Maybe that approach can work for you too?


There's no way to eliminate it, because:

Time is stored internally as the number of seconds with fraction since the Epoch, January 1, 1970 00:00 UTC.

Just format it like this:

t = Time.now
t.strftime("at %I:%M%p")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜