Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Represent a time with no date in ruby

Is there a way to represent a time in ruby without having a Date attached? 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?

like image 366
loosecannon Avatar asked Jun 15 '11 15:06

loosecannon


People also ask

What is now () in Ruby?

Ruby | DateTime now() function DateTime#now() : now() is a DateTime class method which returns a DateTime object denoting the given calendar date. Return: DateTime object denoting the given calendar date.


2 Answers

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?

like image 191
Michael Kohl Avatar answered Sep 20 '22 04:09

Michael Kohl


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")
like image 23
sml Avatar answered Sep 23 '22 04:09

sml