Google Calendar API(v2)'s time-related query is required to be RFC3339-formatted. When I looked up Time class after 'require "time"', I could not see rfc3339 method.
If you are using ActiveRecord, you can use the to_datetime method to convert the time to a DateTime object.
Time.now.to_datetime.rfc3339 #=> "2014-11-06T10:40:54+11:00"
See:
http://www.ruby-doc.org/stdlib-2.4.1/libdoc/date/rdoc/Time.html.
Does this help? http://www.ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/DateTime.html#method-i-rfc3339
DateTime.parse('2001-02-03T04:05:06.123456789+07:00').rfc3339(9)
#=> "2001-02-03T04:05:06.123456789+07:00"
The way I chose to do this was Time.now.utc.strftime('%FT%TZ')
#=> "2013-08-15T06:13:28Z" which is perfect for an HTML5 type='datetime' input field.
A website mentioned that RFC3339 is most common date format in RSS feeds, so that the conversion method is implemented as #xmlschema, but not #rfc3339.
Related
I have a date like this:
Date.today - 7
I tried to convert it into a string:
#last_week = strftime((Date.today - 7), '%Y-%m-%d')
But I get the error "undefined method `strftime'". What am I doing wrong?
You can do it like this:
#last_week = (Date.today - 7).strftime('%Y-%m-%d')
This is what you want, but don't do it.:
module Kernel
def strftime(date, format)
date.strftime(format)
end
end
for the reason, see below comments~~~~~
You are trying to use strftime() as though it was a standalone function. In Ruby, there is no such function. The correct way to do this is to call the method Date#strftime().
Here's an example to format today's date as a string:
Date.today.strftime("%m/%d/%y")
Now that you know how to get a date and format the date to a printable string, you can address your specific code need, which is
#last_week = (Date.today - 7).strftime("%Y-%m-%d")
This will give you the date formatted string "2016-04-28" (or thereabouts, depending on when you run the code).
There is no method as strftime on Kernel (although there is such instance method on Date), but you are trying to call such method.
Addition by #Keith Bennett
You are not calling strftime with an explicit object to receive the method, so the Ruby runtime defaults to calling the method on self, which, in this context, is the top level object, an instance of Object, which inherits from BasicObject and includes the Kernel module. None of these contain a strftime method. However, the Date method does have strftime defined. So you can do what you want to do by calling strftime on the calculated Date instance.
I am building an iOS app using Rubymotion.
I get data from a Rails 3.2.8 API and I want to convert the timestamp I get (2013-01-24T23:42:59Z) to 2013-01-24 23:42:59. How can I do that with Ruby?
What is this format called (2013-01-24T23:42:59Z)?
Perhaps it is called ISO 8601. You can accept this form and turn it into a time object by doing this:
require "time"
Time.iso8601("2013-01-24T23:42:59Z")
# => 2013-01-24 23:42:59 UTC
i am new to ruby and nanoc. I am trying to sort articles based on time. So I get more accurate results on my blog.
This is what I am using in my sorted_articles_time.rb file under /helpers/
def sorted_articles_time
articles.sort_by do |a|
attribute_to_time(a[:time])
end.reverse
end
But then I get the error
NoMethodError: private method `sorted_articles_time' called for #<Nanoc::Site:0x007fd93b0a3f40>
What am I doing wrong ? And is there a way to overwrite the existing sorted_articles method ?
Thanks
UPDATE: I already have it initiated in the rake file. So I think my rake file is fine here.
time1 = Time.new
#time = time1.inspect
Are you calling #site.sorted_article_times? If so, leave off the #site part. Helpers are intended to be called as functions, not as methods on #site.
Use a scrope from your model. Check this activeactive_record_querying this is the best place to sort your articles.
I'm trying to output the timezone of a ruby DateTime object:
DateTime.parse('2012/05/23').strftime('%Z')
This outputs "+00:00". According to the documentation, it should return GMT.
Am I doing something wrong, or have I found a bug?
The DateTime class does not seem to support zone data as zone names. The Time class however does this correctly. So either do this:
require 'date'
require 'time'
Time.parse('...').strftime('%Z')
Or if you already have your data in DateTime format then:
Time.parse(DateTime.parse('...').to_s).strftime('%Z')
I am new to Ruby and had a quick question.
I am trying to get all of the posts from a user's timeline, so I figured I would need to do a user_timeline api call to twitter and then filter out the posts manually. Then, while reading the Ruby Twitter documentation, I found this:
puts Twitter.user_timeline("twitter_handle").first.text
...and that will return the post already parsed out.
Is there a way to get more than just the first post automatically parsed out like that, or is that just an array method for the first and last object in the array?
Thanks
It looks like user_timeline just returns an array, so you ought to be able to use Ruby's normal array methods with it.
Twitter.user_timeline("twitter_handle").each do |tweet|
puts tweet
end
You need to iterate over each object.
Twitter.user_timeline("twitter_handle").each do |tweet|
puts tweet.text
end
The first and last methods are just convenience methods on arrays.
As you can see from source, Twitter::Client#user_timeline returns array from 20 most recent Twitter::Status, so that you can use up to 20 parsed records.