ruby on rails date format problem - ruby-on-rails

Hi
In my application the user can selects a date in a "dd/mm/yyyy" format from a calendar and save it to a date field in the database (mySql).
Everything is going ok on my local server (in Israel) but on my host server ( in the US) it fails.
I did some checking in the console and found that
in my local computer(Israel) when I do the following:
d=Date.new
d.reported_date="23/01/2011",br>
d.save
I get
=>true.
In my host server(US) when I do the following:
d=Date.new
d.reported_date="23/01/2011"
d.save
I get
=>false
When I did this check with another date for example "04/01/2011"
the local and the host could save the record.
So I concluded that it seems like the host(in US) can receive date only in "mm/dd/yyyy" format and convert it to mySql date format
and the local computer(in Israel) can received date only in "dd/mm/yyyy" format and convert it to mySql date format.
(Both in the server and in my local machine the format in the mySql server is yyyy-mm-dd)
I need to find a solution which can adjust mySql in my local machine to behave like in the host server.
I will be glad to some advise
Thanks

Try using yyyy-mm-dd instead, that can never be read wrong and all databases I have worked with accepts that format.

try to use puts DateTime.strptime(inputStr, "%Y-%m-%d_%H-%M-%S")

Related

Primefaces calendar timezone handling to fixed backend timezone

In a web application the user can choose the timezone which is passed to the timezone attribute of the calendar widget:
<p:calendar value="#{curValue}" timeZone="#{settingsBL.getTimeZoneIdSet()}" />
The delivered date in the backing bean is converted to the timezone of the server (JBoss in my case with CEST). The backend want the date and time as always as UTC (and delivers it in UTC).
So when I store a date I have to convert the CEST date to UTC and save it. If a date is delivered from backend it is UTC. I have to convert it to the system default (JBoss with CEST) and the calendar will take care that it is displayed correctly on the client.
Is this correct? I am a little bit confused about that. The server timezone is variable and cannot be set hard to UTC or something.
The date from the client is always converted to CEST for my example. Regardless what I set to javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE in web.xml.
I am using primefaces 5.2.13 and Mojarra 2.2.12 with JBoss 6.4
The timeZone attribute of any java.util.Date based JSF component which allows manipulating the time part must be set to the one as expected by the view (frontend). It will be used when converting the java.util.Date instance from the model (backend) to the String representation which will be embedded in generated HTML output. It will also be used when converting the incoming String request parameter value to a concrete java.util.Date instance which will be used in the model. If you don't allow manipulating the time part, then just stick to the default of timeZone="GMT".
And now comes the key: the java.util.Date does not hold any timezone information. It's internally always GMT. JSF knows that. JDBC knows that. JPA knows that. As long as you tell JSF what timezone the view uses, and you tell JDBC/JPA what timezone the DB uses, then all should be well.
Perhaps your confusion is caused because you did something like System.out.println(date) to verify one and other. Its toString() result will internally use TimeZone#getDefault() and thus not explicitly use GMT/UTC during the String generation. You'll then confusingly see the date in the system default timezone being printed. To print the date with GMT timezone (in order to debug/log/verify it and such), do so:
System.out.println(DateTimeFormatter.ISO_DATE_TIME.format(date.toInstant().atZone(ZoneId.of("GMT"))));

Rails DateTime re-format using #{DateTime.now}

I have a task in Rails that exports an xml, I've one line in it though
tmp_filename="#{Rails.root}/tmp/orders-#{o.id}-#{DateTime.now}.xml"
and this outputs the xml file with a filename like
orders-42-2015-01-28T17:22:35+00-00.xml
This is the way it shows up when its uploaded directly to amazon s3, the problem is I need to get rid of the colons and just have dashes because the system thats taking these files doesn't work properly with the colon in the filename.
The strange thing is that when I download the file from s3 it downloads as dashes.
I'm not sure how or if I can use strftime on #{}
Could anybody help with what I'm trying to do. Or if this is just an amazon s3 thing and the file is actually being generated with the - and not : already.
Strftime doesn't seem to work on amazon s3, the file still uploads as the original format even after adding
tmp_filename="#{Rails.root}/tmp/orders-#{o.id}-#{DateTime.now.strftime('%d-%m-%Y-%H%M%S')}.xml"
and it also adds an extra +00:00 at the end for some reason that I can't get rid of
Can't you just format the DateTime without colons, for example:
tmp_filename="#{Rails.root}/tmp/orders-#{o.id}-#{DateTime.now.strftime('%Y-%m-%d-%H-%M-%S')}.xml"
With this you'll get the time in format like below, without colons:
irb(main):010:0> DateTime.now.strftime('%Y-%m-%d-%H-%M-%S')
=> "2015-01-29-10-50-30"

Handling date text input in italian format on Rails 3.0.x

I've a Rails 3.0.7 application that handles many date "text field inputs" in italian format (%d/%m/%Y).
On my Linux Box (configured with italian locale) I run the application and insert into the text box the date 12 november 2014 with an italian format:
12/11/2014 (%d/%m/%Y)
here Rails correctly saves the date without any problem, same thing if I try to insert a date like 27 november 2014:
27/11/2014 (%d/%m/%Y)
On the remote production server the date is wrongly saved swapping day and month parts, so my input:
12/11/2014
is wrongly saved into date:
11/12/2014
and the date input:
27/11/2014
raise an exception because 27 is not a valid month...
I suppose that into production server the problem would be that the date format system setting is wrong (mm/dd/yyyy perhaps...) but the server is administered by a plesk panel and I don't see anything related to date format system setting...
So the question is: what can I configure in Rails (v. 3.0.x) so that I can save the text field date input in italian format ? (possibly avoiding to write a gazillion of code...)
Many thanks in advance...
Your first step should be setting up the locale:
# config/initializers/locale.rb
I18n.default_locale = :it
A few options for dealing with input:
1. Replace the text inputs with HTML5 date inputs or Javascript "date pickers"
I really recommend this since you can get validations on the cheap and its a good feature.
2. Use Time/Date/DateTime .strptime
params_with_dates = params.merge({
"date" => Date.strptime( params["date"], '%d/%m/%Y' )
})
Foo.create(params_with_dates);
Additional reading:
Date.strptime
Rails Internationalization (I18n) API
Ruby 1.8 uses date format conversion %m/%d/%Y, the 1.9 version instead uses date format %d/%m/%Y, so the solution seems to be switching to Ruby 1.9.x...
Example, the date 28th december 2015 is parsed:
In Ruby 1.8: Date.parse("28/01/2015")
In Ruby 1.9: Date.parse("01/28/2015")

How save dd-mm-yyyy date format to mysql using grails

In my grails application i use a javascript calendar plugin which accepts date in dd-mm-yyyy format. I converted the date into yyyy-MM-dd HH:mm:ss z before saving to database which is the default date format in grails. But still it gives me an error of Invalid date format. I tried changing the
default.date.format in messages.properties file. But still it not works. How can i save the dd-mm-yyy format to Mysql using grails?
try using the binding annotation on your domain class
import org.grails.databinding.BindingFormat
...
class MyClass{
#BindingFormat('dd-mm-yyyy hh:mm a')
Date dummydate
}
in the #BindingFormat annotation you can put the format you use client side.
You can also do
Date.parse("dd-MM-yyyy HH:mm:ss", params.date)
In case you want to save it to database without time we can do
Date.parse("dd-MM-yyyy", params.date).clearTime()

Struts2, type conversion and json plugin (struts2-json-plugin-2.3.14.2)

I have created a web application that simulates an excel spreadsheet.
I would like to use Spanish number format. Example 2.345,67 and not american format: 2,345.67
In the server side I save these values in a List of BigDecimal numbers (List<BigDecimal>).
To send the information from client to server and server to client I am using Ajax with Json.
The problem is that automatically only works the american format (1,234.56). I suppose that I need to create a Type Conversion but It seems that when I am using in the server the package json-default it doesn't work.
Please help.

Resources