App uses SOAP4r for consuming API/SOAP
But SOAP::SOAPTimeFormat is returning
2015-11-15T16:59:521468.7999999999999545-04:00
chkout.add('purchasedDt ', SOAP::SOAPDateTime.new(basket.purchase_Date))
Using strftime('%Y-%m-%dT%H:%M:%S') is giving the following
chkout.add('purchasedDt ', SOAP::SOAPDateTime.new(basket.purchase_Date.strftime('%Y-%m-%dT%H:%M:%S')))
2015-11-15T16:59:52Z
What App needs is
2015-11-15 16:59:52 -0400
Please advise ...need the format in
yyyy-mm-ddThh:mm:ss-/+gmt
-Fransis
A simple change in your strftime and you can find out more in the doc for Time#strftime
basket.purchase_Date.now.strftime('%Y-%m-%d %H:%M %z')
=> "2016-04-26 22:48 -0400"
Seems like your applicaton accepts the iso8601 format. You can use Time#xmlschema as a shortcut to generate iso8601 compatible strings:
basket.purchase_Date.xmlschema
#=> "2015-11-15T16:59:52-04:00"
Just change this line in your example:
chkout.add('purchasedDt ', SOAP::SOAPDateTime.new(basket.purchase_Date.xmlschema))
Related
I'm on Rails 5 (Ruby 2.4). I want to read an .xls doc and I would like to get the data into CSV format, just as it appears in the Excel file. Someone recommended I use Roo, and so I have
book = Roo::Spreadsheet.open(file_location)
sheet = book.sheet(0)
text = sheet.to_csv
arr_of_arrs = CSV.parse(text)
However what is getting returned is not the same as what I see in the spreadsheet. For isntance, a cell in the spreadsheet has
16:45.81
and when I get the CSV data from above, what is returned is
"0.011641319444444444"
How do I parse the Excel doc and get exactly what I see? I don't care if I use Roo to parse or not, just as long as I can get CSV data that is a representation of what I see rather than some weird internal representation. For reference the file type I was parsing givies this when I run "file name_of_file.xls" ...
Composite Document File V2 Document, Little Endian, Os: Windows, Version 5.1, Code page: 1252, Author: Dwight Schroot, Last Saved By: Dwight Schroot, Name of Creating Application: Microsoft Excel, Create Time/Date: Tue Sep 21 17:05:21 2010, Last Saved Time/Date: Wed Oct 13 16:52:14 2010, Security: 0
You need to save the custom formula in a text format on the .xls side. If your opening the .xls file from the internet this won't work but this will fix your problem if you can manipulate the file. You can do this using the function =TEXT(A2, "mm:ss.0") A2 is just the cell I'm using as an example.
book = ::Roo::Spreadsheet.open(file_location)
puts book.cell('B', 2)
=> '16.45.8'
If manipulating the file is not an option you could just pass a custom converter to CSV.new() and convert the decimal time back to the correct format you need.
require 'roo-xls'
require 'csv'
CSV::Converters[:time_parser] = lambda do |field, info|
case info[:header].strip
when "time" then begin
# 0.011641319444444444 * 24 hours * 3600 seconds = 1005.81
parse_time = field.to_f * 24 * 3600
# 1005.81.divmod(60) = [16, 45.809999999999999945]
mm, ss = parse_time.divmod(60)
# returns "16:45.81"
time = "#{mm}:#{ss.round(2)}"
time
rescue
field
end
else
field
end
end
book = ::Roo::Spreadsheet.open(file_location)
sheet = book.sheet(0)
csv = CSV.new(sheet.to_csv, headers: true, converters: [:time_parser]).map {|row| row.to_hash}
puts csv
=> {"time "=>"16:45.81"}
{"time "=>"12:46.0"}
Under the hood roo-xls gem uses the spreadsheet gem to parse the xls file. There was a similar issue to yours logged here, but it doesn't appear that there was any real resolution. Internally xls stores 16:45.81 as a Number and associates some formatting with it. I believe the issue has something to do with the spreadsheet gem not correctly handling the cell format.
I did try messing around with adding a format mm:ss.0 by following this guide but I couldn't get it to work, maybe you'll have more luck.
You can use converters option. It seems looking like this:
arr_of_arrs = CSV.parse(text, {converters: :date_time})
http://ruby-doc.org/stdlib-2.0.0/libdoc/csv/rdoc/CSV.html
Your problem seems to be with the way you're parsing (reading) the input file.
roo parses only Excel 2007-2013 (.xlsx) files. From you question, you want to parse .xls, which is a different format.
Like the documentation says, use the roo-xls gem instead.
I have been using local_time to convert servertime to client local time. However now I am using datatables and to sort date/time columns. I have to use date-moment.js plugin of Datatables which uses moment.js to handle date conversions.
My problem is: the local_time's view helper is wrapping the date with < time > tag like this
<time data-format="%B %e, %Y %l:%M%P"
data-local="time"
datetime="2013-11-27T23:43:22Z"
title="November 27, 2013 6:43pm EDT"
data-localized="true">November 27, 2013 6:43pm</time>
The wrapping thing is creating problem for moment.js to get the actual date-time as it expects. I need it to convert the date-time but not wrap with <time> tag. Is it possible. If yes How?
currently created a helper method to extract the core date from the string returned by the gem's local_date method
in views/
<%= extract_date(local_date(workflow.created_at, CommonConstants::DATE_FORMAT_LONG)) %>
# Parse the string generated by local_time gem
# Expectation :
# "<time data-format=\"%B %e, %Y\" data-local=\"time\" datetime=\"2015-10-28T11:19:54Z\">October 28, 2015</time>"
def extract_date(date_string)
date_string.split('>').pop.split('</')[0] rescue ''
end
What I would like to do is to go from "2013", "December", "20" and to create 2013-12-20.
Does someone have an idea ? Thanks !
Rails provide nice converters in part of it's framework
2.1.2 :001 > "20 december 2013".to_date
=> Fri, 20 Dec 2013
For your required format you can use this bit for formatting:
strftime("The date is %y-%m-%d")
This can be called on any time object.
You can do this:
Date.new(2013, 12, 20)
You can read more about Date here
This is an extension to #Marc-Alexandre Bérubé 's answer to get your desired format:
"20 december 2013".to_date.strftime("%Y-%m-%d")
# => "2013-12-20"
I have a date (Which is actually parsed from a PDF) and it could be any of the following format:
MM/DD/YYYY
MM/DD/YY
M/D/YY
October 15, 2007
Oct 15, 2007
Is there any gem or function available in rails or ruby to parse my date?
Or I need to parse it using regex?
BTW I'm using ruby on rails 3.2.
You can try Date.parse(date_string).
You might also use Date#strptime if you need a specific format:
> Date.strptime("10/15/2013", "%m/%d/%Y")
=> Tue, 15 Oct 2013
For a general solution:
format_str = "%m/%d/" + (date_str =~ /\d{4}/ ? "%Y" : "%y")
date = Date.parse(date_str) rescue Date.strptime(date_str, format_str)
I find the chronic gem very easy to use for time parsing and it should work for you. i tried the examples you gave.
https://github.com/mojombo/chronic
saving a string to my db as a date and having some strange results
if the date is formatted like,
dd/mm/yy it will save
if date is formatted like,
mm/dd/yy it will fail to save silently
in my console if i go
'20/10/2012'.to_date
=> Sat, 20 Oct 2012
it works
if i go
'10/20/2012'.to_date
=> ArgumentError: invalid date ...
it breaks
i used an initializer to set up my default date format to %m/%d/%Y which you can see is accurately reflected in my DATE_FORMATS hash.
Date::DATE_FORMATS
=> {:short=>"%e %b", :long=>"%B %e, %Y", :db=>"%Y-%m-%d", :number=>"%Y%m%d", :long_ordinal=>#<Proc:0x007f8663f1aae0#/Users/ian/.rvm/gems/ruby-1.9.3-p0#rails-3.2/gems/activesupport-3.2.1/lib/active_support/core_ext/date/conversions.rb:12 (lambda)>, :rfc822=>"%e %b %Y", :default=>"%m/%d/%Y"}
Uncertain what is the cause of the issue here, as things seem to be configured correctly. How to resolve?
thank you!
Try:
DateTime.strptime('20/10/2012', '%d/%m/%Y')
Or just use Date if you don't need an associated time:
Date.strptime('20/10/2012', '%d/%m/%Y')
Use gem american_date
In gem file
gem 'american_date'
Now it will save mm/dd/yyyy date format.