I have the following record on my data base:
"availability_date" : ISODate("2014-09-29T15:45:00.000Z")
and I trying to get the differences between two datetime like this:
#minutes = (((#date_time.to_time) - (Time.now))/60).round
but the #date_time have the following value and I don't understand why???
"2014-09-29 17:45:00 +0200"
could someone help me please.
Thanks in advance
I don't think there is any problem, The datetime in #date_time is the same as in your database. The +0200 at the end means that it is written in a different timezone, here GMT +2, I guess. It is probably the time zone that your computer uses.
What is the result you expect ? Can you give an example ? And be sure to read the answer to In Ruby on Rails, what's the difference between DateTime, Timestamp, Time and Date?
Good luck.
Try to convert them to integer using to_i m this give you the number of seconds in unix time, the n it's probably more easy to do a calculation
Related
I am creating a time object, which I use to gather all info from the past 24 hours, I have no issue creating the object displaying the current time. But I am unsure as to how to set it exactly 24 hours in the past, without having the timezone attached.
def set_time
#past_time = Time.now.to_s(:db) - 1.days
end
Expected Output Format :
"2021-11-29 09:15:17"
Result:
undefined method `-' for "2021-11-29 10:19:46":String
You are subtracting the time from the string object as you converted Time.now into the string using to_s.
Instead of you can do this
(Time.new - 1.days).to_s(:db)
Note: You will get multiple ways to accomplish these rails. You can improve the code readability and understanding of code by doing this.
Example:
DateTime.now.days_ago(1)
The easiest I can think of would be:
24.hours.ago.to_s(:db)
Note that the returned time would default to UTC in this case.
You can use DateTime#advance from ActiveSupport:
Time.current.advance(hours: -24)
# or
Time.current.advance(days: -1)
Note that in timezones that use DST a day is not always 24 hours so the two are not actually equivilent. You can also use the methods that ActiveSupport::Duration monkeypatches onto Integer:
24.hours.ago
1.day.ago
This always uses your default timezone though.
I need to use strptime to convert a timestamp string with milliseconds to a Time object.
A work around is using parse:
t= Time.parse('29 Sep 2013 12:25:00.367')
=> 2013-09-29 12:25:00 -0400
But it is very important for my code to use strptime, because I want to be able to pass multiple types of format including: "HH:MM", "HH", etc. through the function.
I can do it with nanoseconds like this:
Time.strptime("12:34:56:789434", "%H:%M:%S:%N")
=> 2016-03-16 12:34:56 +0100
I want something like this:
Time.strptime("12:34:56:789", "%H:%M:%S:%[insert magic letter that represent milliseconds]")
My thought is that there must be a way to do it with milliseconds as well.
Is it possible and how?
Try %L.
Refer to Ruby's DateTime documentation.
If you can update to Ruby 1.9.3, it supports this using %3N:
http://ruby-doc.org/core-1.9.3/Time.html#method-i-strftime
I'm working with a Dataset which gives me a Time Variable for Objects, just like the created_at. The value tho is :
1398037671
Is this a special kind of encoding Timestamps or am i missing something ?
I guess it is "seconds since the Epoch" timestamp
Time.at(1398037671)
2014-04-21 01:47:51 +0200
http://www.ruby-doc.org/core-2.1.1/Time.html#method-c-at
That's a Unix timestamp. That specific timestamp represents 04 / 20 / 14 # 11:47:51pm UTC
You can find out more about them here: http://www.unixtimestamp.com/index.php and at good old wikipedia here: http://en.wikipedia.org/wiki/Unix_time
In Ruby, you can generate a Unix timestamp with Time.now.to_i (or obviously any other time if you don't want the timestamp for now).
Sorry if that question sounds strange, but I'm diving into Rails and I'm still learning the jargon. Basically, I'm trying to create a single-pass query that uses the value of one of the model's attributes in a calculation in the query (assuming that's even possible).
I have a Tournament model that has a start_date attribute that is a DateTime object. I'm trying to create a query that returns all the Tournaments that have a start_date no older than 1 hour + the length of the tournament, or put another way, all tournaments that haven't yet started or have started, but haven't ended longer than an hour ago. My current query, which doesn't work, looks like this...
validTourneys = Tournament.where("start_date > (? - duration_in_mins)", (DateTime.now.utc - 1.hour))
where duration_in_mins is an integer attribute of the Tournament model, but this query doesn't work and it seems to be returning all the Tournaments all the time. I'd like to include duration_in_mins in the (DateTime.now.utc - 1.hour) part of the calculation, but I don't know how to reference it, which is why I included it in the string part of the query, hoping that would work. Am I at least on the right track?
I should mention I'm using SQLite for development and PostgreSQL for production.
Thanks for your wisdom!
The problem is that if you subtract minutes from a DateTime object, you are not subtracting minutes but days.
# This works as expected
dt = DateTime.now # Thu, 28 Apr 2011 09:55:14 +0900
an_hour_ago = dt - 1.hour # Thu, 28 Apr 2011 08:55:14 +0900
# But, this does not...
two_hours_in_minutes = 120
two_hours_ago = dt - two_hours_in_minutes # Wed, 29 Dec 2010 09:55:14 +0900
In the last example 120 days are subtracted instead of minutes. This is probably also happening in your query. You have to convert duration_in_minutes to days and then subtract.
I don't know enough about SQL to answer your question directly (I think this will probably also depend on what database you're using, so you might want to mention that).
Have you considered, though, having start_date and end_date as DateTime columns instead of start_date and duration_in_mins? If this is going to be a common query, that would certainly make it more performant, as well as making your code easier to read and understand.
This query will only work if your database is smart enough to know how to add (what I am assuming) is a DateTime and and integer. And I can't think of a database that will do that correctly the way you have it coded. No database will assume minutes. Some might do ticks, seconds, or days.
This part of the calculation
(? - duration_in_mins)
is going to happen on the database, not in Ruby-land.
I am using Time.parse to create a Time object from a string.
For some reason
Time.parse("05-14-2009 19:00")
causes an argument our of range error, whereas
Time.parse("05-07-2009 19:00")
does not
Any ideas?
If you know the format of the string use:
Time.strptime(date, format, now=self.now) {|year| ...}
http://www.ruby-doc.org/core-1.9/classes/Time.html#M000266
It will solve your problem and will probably be faster than Time.parse.
EDIT:
Looks like they took strptime from Time class, but it called Date.strptime anyway. If you are on Rails you can do:
Date.strptime("05-14-2009 19:00","%m-%d-%Y %H:%M").to_time
if you use pure ruby then you need:
require 'date'
d=Date._strptime("05-14-2009 19:00","%m-%d-%Y %H:%M")
Time.utc(d[:year], d[:mon], d[:mday], d[:hour], d[:min],
d[:sec], d[:sec_fraction], d[:zone])
See also: Date and Time formating issues in Ruby on Rails.
It's because of the heuristics of Time#parse.
And it's due to anglo-american formats.
With dashes '-' it expects mm-dd-yyyy, with slashes '/' it expects dd/mm/yyyy.
This behaviour changes intentionally in 1.9. to accomplish eur, iso and jp date standards.
My guess would be that its expecting the second part of the string (the 14) to be the month.
This link may help you parse it.
You probably do not need it to solve this problem but I still recommend checking out the natural language date/time parser Chronic, it has saved me a lot of work a couple of times.
It is probably expecting Day-Month-Year format, so your first value is trying to specify the 5th day of the 14th month.