How to convert timestamp? - ruby

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

Related

Converting a date to string representation

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.

converting Time class object to RFC3339 in Ruby

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.

Ruby DateTime human timezone output

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')

issues with date conversion, using rails 3.0.7 and ruby 1.9.2

i am using datepicker to insert date on my form, which of course returns date to the controller as a string.
the issue is when I try to convert this string to date, I get an error "invalid date"
Date.parse(params[:abc][:date])
To verify if I'm doing it right, I wrote the following ruby program:
string_date = "06/18/2011"
date = Date.parse(string_date)
puts date
This works perfectly fine. But when I try the same thing in my rails controller, it gives invalid date error.
please help.
When I do this in my Rails 3.1 console:
date = Date.parse("06/18/2011")
I get an "ArgumentError: invalid date" exception. However, it works fine with an ISO-8601 date:
date = Date.parse('2011-06-18')
So perhaps you're having a locale problem. Your script could be using your standard locale setting (which is probably some US locale judging by the date format) but your server is probably using something else.
Try changing the jQuery datepicker date format to something standard and unambiguous by adding this:
dateFormat: 'yy-mm-dd'
to the datepicker's options. The datepicker's default is 'mm/dd/yy' and Ruby's Date class doesn't like seem to like it without a special locale setting.

Future posting with dateCreated in Wordpress via XMLRPC in Ruby

I'm reaching the end of my tether trying to schedule a new post through Wordpress' XMLRPC interface from Ruby.
I am creating a new Time object and filling it with my date and time, I then call .xmlschema to get a datetime string in the correct format for Wordpress' XMLRPC interface.
Unfortunately, Wordpress treats this as a string, and I can't work out how to get the xmlrpc.php to treat it as an object; really can't get my head around it.
Calling the metaWeblog.newPost method, and sending:
{:title => 'Foo', :post_status => 'publish', :dateCreated => my_date.xmlschema}
to Wordpress.
Anyone been through this before?
Can you check what you are actually sending (the post data)? The date should be encapsulated in <dateTime.iso8601></dateTime.iso8601>, not in <string></string>. The WordPress Trac has a similar report.

Resources