views:

15

answers:

1

Hi,

I am using Ruby on rails. I am trying to retrieve timestamp column from particular table.

Activerecord returns me the date, if timestamp contains all zeros.

e.g: If timestamp stored is 2010-09-06 00:00:00:000, it returns me 2010-09-06. But if I have 2010-09-06 00:00:20:000, it returns me in the expected format(i.e: 2010-09-06 00:00:20:000).

This leads to inconsistency. Please help me improve on this.

All suggestions are welcome.

Thanks in Advance. Sachin

+1  A: 

What is the type of the returned object? If that's String, you may manually add missing parts, but if that is a Time object, then you may use a .strftime() method:

t + " 00:00:00.0000" if t =~ /^[-0-9]{10}$/

t.strftime("%Y-%m-%d %H:%M:%S.") + t.usec.to_s
Arsen7