Problem with storing timestamp as datetime object

Hi,

I am storing timestamp of a file as “datetime” object in a DB, now when
i parse through a file i check if there is an existing entry in the DB
with same name and get the timestamp out of it. I compare both the
timestamps.

i have used GetFileInfo #{file} | grep modified.split(": ").last to
get something of this kind
Tue Jun 05 14:16:28 UTC 2012

When i retrive the datatime field from the DB i see this 2012-06-05
14:16:28 UTC. Since these two point to the same time , how do i compare
them?

Hi,

Thanks for the reply. But i see a problem here

time_1 = File.mtime ‘myfile.foo’ # mtime of file
this returns “Thu Dec 22 16:26:43 -0800 2011”
time_2 = Time.parse ‘2012-06-05 14:16:28 UTC’ # from database
returns “Thu Dec 22 16:26:43 UTC 2011”

so the UTC and -0800 characters dont match and as a result the strcmp
fails.

Hi,

You should use the built-in method to get the mtime of a file rather
than fumble with the console:

time_1 = File.mtime ‘myfile.foo’

This returns a Time object (independend of the platform).

Then you can load the Time extension from the standard library, parse
the timestamp from the database and compare the Time object to the one
above:

#-----------------------
require ‘time’

time_1 = File.mtime ‘myfile.foo’ # mtime of file
time_2 = Time.parse ‘2012-06-05 14:16:28 UTC’ # from database
puts time_1 == time_2
#-----------------------

On Jun 8, 2012, at 15:53, “cyber c.” [email protected] wrote:

fails.
It isn’t a matter of utc vs 0800… Those are completely different times
and would fail any equality comparison.

cyber c. wrote in post #1063772:

so the UTC and -0800 characters dont match and as a result the strcmp
fails.

No, the Time object does not compare the strings (this wouldn’t make any
sense) but the actual points of time.

cyber c. wrote in post #1063777:

Im sorry , reposting it again to avoid confusion

Those are still two different points of time: “2011-12-22 16:26:43
-0800” is “2011-12-22 23:26:43 UTC”, so it’s definitely not equal to the
database time “2011-12-22 16:26:43 UTC”.

You’ll probably have to fix the database timestamps (subtract 8 hours
according to your local timezone).

Im sorry , reposting it again to avoid confusion

time_1 = File.mtime ‘myfile.foo’ # mtime of file
this returns “Thu Dec 22 16:26:43 -0800 2011”
time_2 = Time.parse ‘2011-12-22 16:26:43 UTC’ # from database //
Modified the param to Time.parse
returns “Thu Dec 22 16:26:43 UTC 2011”

Jan E. wrote in post #1063785:

You’ll probably have to fix the database timestamps (subtract 8 hours
according to your local timezone).

Sorry, I meant add 8 hours.