Facing a problem with GWT Data Serialization - serialization

This is similar to the question: GWT Data Serialization
I am using Date Object # my client,
Here is what I tried,
I created a Date Object, showed its toString() in a Label and passed it to server via Rpc whose return value is the same Date's toString()
value passed:
Date value = new Date(2011, 0, 19);
Output # Hosted/Development mode : (http://127.0.0.1:8888/MyApplication.html?gwt.codesvr=127.0.0.1:9997)
Client Reads: Thu Jan 19 00:00:00 IST 3911 Server reads:Thu Jan 19 00:00:00 IST 3911
Output # Production mode (after GWT compile) : (http://127.0.0.1:8888/MyApplication.html)
Client Reads: Thu Jan 19 00:00:00 GMT+530 3911 Server reads:Thu Jan 19 00:00:00 IST 3911
Output # Deploying in Jboss running in the same Machine :
Client Reads: Thu Jan 19 00:00:00 GMT+530 3911 Server reads:Wed Jan 18 18:30:00 GMT 3911
What's mind boggling is that its the same machine and hence the locale, etc should ideally be the same, still # hosted mode I see client sends IST server receiving IST, #production I see client sends GMT server receives IST and #Jboss server deploy I see client sends GMT and server receives GMT with one Day less !!
The fact that the server recieves one day less is very seriously affecting my application business logic, basically I am using iBatis thats populating my pojos from database and sending it to client and at client I am using GXT which is showing me a UI for date selection.
Using String for date is not suitable for me since its a major change in application core pojos, all database select and insert queries just because my client library does not handle Dates well.
Does anyone have a alternative to this ?

You can try to use DateTimeFormat to build something that is unambiguous among machines despite locales. Format like 'yyyy-MM-dd HH:mm:ss ZZZZ' instead of using toString()
Date today = new Date();
DateTimeFormat fmt = DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss ZZZZ");
GWT.log(fmt.format(today));

Related

How to determine and show the expiry time of a specific session

I am using spring-session and wondering how to know a specific session expired.
Querying Redis persistence, I find a couple of the following lines
[ root#redis-master-6fbbc44567-cc28m:/data ]$ redis-cli
127.0.0.1:6379> keys *
1) "spring:session:expirations:1581796140000"
2) "spring:session:sessions:23d6aff1-cb43-44f6-920d-cc3536ab6d46"
127.0.0.1:6379>
Converting the expirations to date, they are equavalent to Mon 14 Feb 52095 16:40:00 GMT which looks weird at the year.
We might extract the expired time from HttpSession:
HttpSession httpSession = request.getSession()
long currTime = System.currentTimeMillis()
long expiryTime = currTime + httpSession.getMaxInactiveInterval()
But the snippet doesn't show exactly what weed anticipate.
I reckon we need to retrieve expirations from Redis server instead.
What am I doing wrong here?
Any of you know how to retrieve sessions and expirations from Redis?
The expiration timestamp 1581796140000 is a Unix epoch in milliseconds and translates to GMT: Saturday, February 15, 2020 7:49:00 PM.

Jasmine .toHaveBeenCalledWith(aDate) not working

I'm inheriting some code, and I've got two of their tests that are still failing, not sure if they were before, or if it's because I have a different version of Jasmine (they were pre 2.0)
The test that is failing has this spy setup in the beforeEach
spyOn(datacontext, 'getImportLogForDate').and.callThrough();
Then in the test
controller.DepositDate = new Date();
controller.PerformActionThatCallsGetImportLogForDate();
expect(context.getImportLogForDate).toHaveBeenCalledWith('1', controller.DepositDate);
The resulting error is confounding because they are identical
Expected spy getImportLogForDate to have been called with [ '1', Date(Thu Dec 04 2014 13:00:51 GMT-0600 (Central Standard Time)) ] but actual calls were [ '1', Date(Thu Dec 04 2014 13:00:51 GMT-0600 (Central Standard Time)) ].
Can I not verify functions have been called with a Date?
What is PerformActionThatCallsGetImportLogForDate doing with the date object? Jasmine compares date objects by their millisecond value so if it's off even by 1ms, they won't be equal, but you won't see that level of detail just reading the console output.
Alternatively, you have 2 other options.
Just test that a date object was used as the 2nd argument.
expect(context.getImportLogForDate)
.toHaveBeenCalledWith('1', jasmine.any(Date));
Test that date value, but outside of a toHaveBeenCalledWith, in case of some specific weirdness with that matcher.
expect(context.getImportLogForDate.calls.mostRecent().args[0])
.toEqual('1');
expect(context.getImportLogForDate.calls.mostRecent().args[1])
.toEqual(controller.DepositDate);
I have found that when using toHaveBeenCalledWith for a specific Date, jasmine.objectContaining works great.
it('#userChangedMonth calls fetchData with end of month', () => {
spyOn(component, 'fetchData').and.returnValue(true);
const calendarChangedTo = new Date('2018-10-10 00:00:00');
const endOfMonth = new Date('2018-10-31 23:59:59');
component.userChangedMonth(calendarChangedTo);
expect(component.fetchData).toHaveBeenCalledWith(jasmine.objectContaining(endOfMonth));
});

Linking datetimes of events to their associated location timezone (TimeZone gem, ActiveSupport::TimeWithZone)

I beat my head against this for a day and a half before it finally all came together, partly because I was fundamentally misunderstanding some things because of my assumptions of how things should work, and so the results I was getting seemed so completely irrational and confusing that I was starting to think things must be horribly broken in the libraries. They weren't.
This thread is to share my question, and what worked, partly because it seems to me that a lot of times a datetime record should be tied to the localtime of its location, and a lot of times when applications behave strangely with respect to timezone changes I think it's because they don't do that. So here's how to do that.
The easy part was using the TimeZone gem to get the location's timezone (I already have the latitude/longitude from using the GMaps4Rails gem), as described here: Ruby gem for finding timezone of location
Initially, I hoped each datetime could have an intrinsic and persistent time zone, even after being saved and retrieved from the DB, since we only care about the local time zone for each event. For the unusual and rare case where the datetime was needed in other-than-local-zone, I could convert. But I guess that doesn't exist since MySQL seem to have no concept of timezone with its datetime, and just stores everything as a plain ol' date and time.
So, that's fine -- the DB stores in UTC and I must do the conversions each direction.
How? (See below.)
User enters a datetime (year, month, day, hour, min, sec) in the appropriate local time for the event location. To save this as the correct UTC time in your DB, and retrieve later retrieve it and use it with its correct timezone, here's the recipe:
You must first set Time.zone = timezone -- where timezone is a named timezone string, like "America/Toronto" (GeoKit/TimeZone style) or "Eastern Time (US & Canada)" (Rails 3 style):
1.9.3p200 :001 > Time.zone = "America/Toronto"
=> "America/Toronto"
Note that it is nontrivial to map between the GeoKit/TimeZone style and the Rails 3 style. There is some support for that, but many of the mappings that GeoKit returns are not in the Rails mappings, so I'm sticking with the GeoKit/TimeZone names because they seem to work just fine, and I don't need user-selectable timezones because I'm taking care of all of that automagically for the user using the locations.
Now you can parse and save your datetime variable using ActiveSupport::TimeWithZone, and it will be encoded properly using the Time.zone parameter:
1.9.3p200 :002 > t = Time.zone.local( 2012, 8, 1, 12, 0, 0 )
=> Wed, 01 Aug 2012 12:00:00 EDT -04:00
i.e. In my case, for my Event model with instance event:
event.start = Time.zone.local( year, month, day, hour, min, sec )
event.save
When the data is saved to the DB, it is in UTC: 2012-08-01 16:00:00.000000
(Simulated below with call to t.utc)
1.9.3p200 :003 > t = t.utc
=> 2012-08-01 16:00:00 UTC
1.9.3p200 :004 > t.zone
=> "UTC"
Now, when you read it back from the DB, you need to again use the TimeWithZone support to tell the DateTime what timezone it is in.
Note that the in_time_zone function does not change the underlying datetime variable's timezone:
1.9.3p200 :005 > t.in_time_zone( "America/Toronto" )
=> Wed, 01 Aug 2012 12:00:00 EDT -04:00
1.9.3p200 :006 > t
=> 2012-08-01 16:00:00 UTC
1.9.3p200 :007 > t.zone
=> "UTC"
so you really need to assign the function output back to your datetime variable:
1.9.3p200 :008 > t = t.in_time_zone( "America/Toronto" )
=> Wed, 01 Aug 2012 12:00:00 EDT -04:00
1.9.3p200 :009 > t
=> Wed, 01 Aug 2012 12:00:00 EDT -04:00
1.9.3p200 :010 > t.zone
=> "EDT"
Finally, a warning -- do not use t.time! It's output is completely useless and irrational, with the time and it's timezone out of sync:
1.9.3p200 :011 > t.time
=> 2012-08-01 12:00:00 UTC

How to set timezone for my application deployed on RUN#Cloud

I am using the cloud bees to run our application. But the Time is being shown in GMT. What we want is IST ?
Here is the sample code :
Calendar cal = Calendar.getInstance();
TimeZone istTime = TimeZone.getTimeZone("IST");
cal.setTimeZone(istTime);
Date chkOut = cal.getTime();
startTripMapperObj.setRentalStartTime(chkOut);
Date chkOut2 = startTripMapperObj.getRentalStartTime();
RentalTrip r = new RentalTrip();
This is the output :
Trip Check In Wed Aug 15 14:34:20 GMT 2012
Trip Check Out Wed Aug 15 14:12:00 GMT 2012
by default the timezone is GMT - for java apps you can set the timezone yourself (I have seen some people do it in a servlet context listener so it runs on app startup time).
If you are using the cloudbees SDK, you can also set jvmTimeZone=XXX parameter which will then set it for all instance of that application from then on.

changed? method always returns TRUE with DateTime field even is wasn`t changed (using jQuery UI datetimepicker)

I have encountered strange problem - when using jQuery UI datetimepicker (http://trentrichardson.com/examples/timepicker/) for editing DateTime field it seems like Rails thinks that this field is always changed even if I don`t changed it. The thing is that I have validation if this field changed and it is always executed.
self.begin_time - self.changed_attributes["begin_time"] == 0.0 # => true
self.begin_time_changed? #=> true
I use text_field for displaying DateTime field and I specified Time format and initializer like this:
Time::DATE_FORMATS[:default] = "%d.%m.%Y %H:%M"
And even more:
#d.begin_time
# => Thu, 24 Nov 2011 14:00:00 MSK +04:00
#d.begin_time = "24.11.2011 14:00"
#d.begin_time_change
# => [Thu, 24 Nov 2011 14:00:00 MSK +04:00, Thu, 24 Nov 2011 14:00:00 MSK +04:00]
#d.begin_time_changed?
=> true
So times are equal and I don`t know how to make changed? method work properly.