Ruby spreadsheet datetime error - ruby

I have a row that looks like the following (in a .xls):
bob abc 6/14/14 8:23
I want to return the date and time as: 61414823
I tried to get started with:
def self.regex_date
_test_column = #sheet.row(1)[2].date
...
end
Which I read about here: How to retrieve date properly using spreadsheet gem in ruby
But I'm getting the following error, before I can get to the regex part:
in `regex_date': undefined method `date' for #<DateTime:0x007f94e111b700> (NoMethodError)

It means that #sheet.row(1)[2] is a DateTime object. It doesn't have .date method. Instead, you can use .to_date.
But I don't understand why do you want to convert it to Date. What you want is to use .strftime like this:
#sheet.row(1)[2].strftime("%-m%-d%y%-k%M")
DateTime.new(2014, 6, 14, 8, 23).strftime("%-m%-d%y%-k%M") # => "61414823"

Related

RSpec - trying to stub a method that returns its own argument

I have a method which I'm trying to stub out in my unit test. The real method gets called with one argument (a string) and then sends out a text message. I need to stub out the method but return the string that gets passed in as an argument.
The code I have in my RSpec test is this:
allow(taxi_driver).to receive(:send_text).with(:string).and_return(string)
This returns:
NameError: undefined local variable or method 'string'
If I change the return argument to :string, I get the following error:
Please stub a default value first if message might be received with other args as well
I've tried googling and checking the relishapp.com site, but can't find the answer to something which appears quite simple and straightforward.
You can pass a block:
allow(taxi_driver).to receive(:send_text).with(kind_of(String)){|string| string }
expect(taxi_driver.send_text("123")).to eq("123")
My method is being called like this: send_text("the time now is #{Time.now}"). The string varies according to the time, thats why I need the mock to return the varying string. Perhaps its not within the scope of a mock to do this?
In such a case, I usually use Timecop gem in order to freeze system time. Here is a sample use case:
describe "#send_text" do
let(:taxi_driver) { TaxiDriver.new }
before do
Timecop.freeze(Time.local(2016, 1, 30, 12, 0, 0))
end
after do
Timecop.return
end
example do
expect(taxi_driver.send_text("the time now is #{Time.now}")).to eq \
"the time now is 2016-01-30 12:00:00 +0900"
end
end

python datetime.strftime() fails to stringify datetime object with RqlTzinfo

My fetched rows from rethinkdb cluster include date objects with timezones.
Here's an example of one:
ipython> dt
datetime.datetime(2015, 12, 18, 0, 22, 4, 644000, tzinfo=<rethinkdb.ast.RqlTzinfo object at 0x7f072c5d6250>)
I'm trying to stringify them into a certain format:
dt.strftime("%d-%m-%Y %H:%M:%S (%Z)")
and it leads to
*** TypeError: tzinfo.tzname() must return None or a string, not 'unicode'
How can I overcome this?
If you look at the source code for RqlTzinfo.tzname(), you can see that it simply returns its offsetstr attribute. Since that attribute is never modified or converted anywhere in that class and the class's behavior does not depend on it being either a str or unicode, the following should suffice:
dt.tzinfo.offsetstr = str(dt.tzinfo.offsetstr)

Generate Timestamp in Ruby

I am getting a timestamp value in an xml request as the following format.
2014-06-27T12:41:13.0000617Z
I need to form the xml response with this kind of time format in ruby. How do I get this format for the corresponding time?
I wanted to know the name of this format.
Try this:
t = Time.utc(2010,3,30, 5,43,"25.123456789".to_r)
t.iso8601(10)
This produces:
"2010-03-30T05:43:25.1234567890Z"
require 'date'
datetime = DateTime.parse('2014-06-27T12:41:13.0000617Z')
repr = datetime.strftime('%Y-%m-%dT%H:%M:%S.%7NZ')
puts repr
#=> 2014-06-27T12:41:13.0000617Z

evaluate string to modify data model in ruby

I seem to be running into a problem with Rails 3 and I can't seem to figure it out.
Here's what I am trying to do:
att1 = "column"
att2 = "1"
final_column = "#{att1}_#{att2}"
obj.final_column = 4
====> Error
-----> NoMethodError: undefined method `final_column=' for class....
If I do this it works though:
obj.column1=4
What can I do to my final_column to make it work?
Thanks!
You want to do this:
obj.send("#{final_column}=", 4)
If you want to respect the private/protected visibliy, use public_send instead of send.

Ruby - convert string to date

I have a string like "2011-06-02T23:59:59+05:30".
I want to convert it to date format and need to parse only the date, "2011-06-02".
For Ruby 1.9.2:
require 'date' # If not already required. If in Rails then you don't need this line).
puts DateTime.parse("2011-06-02T23:59:59+05:30").to_date.to_s
require 'date'
d = Date.parse("2011-06-02T23:59:59+05:30")
d.strftime("%F")
Simplies way is
require 'date'
date = "2011-06-02T23:59:59+05:30".gsub(/T.*/, '')
DateTime.parse(date)
Time.parse() should allow you to parse the whole date time. Then you can use time.strftime( string ) to format that as just a date in a string.
date = Time.parse("2011-06-02T23:59:59+05:30")
date_string = time.strftime("%y-%m-%d")
of
date_string = time.strftime("%F")
(see Ruby Doc for Time for more output string formats)
The above should work if you want a string; if you want a date object to handle then the ruby Date class can help you handle it but I belive that everything still needs to be done with Time objects; see Ruby Doc for Date for details of the Date class.
Hope that helps, let me know if I have headed off in the wrong direction with my answer.

Resources