Android-DDP icon indicating copy to clipboard operation
Android-DDP copied to clipboard

Support for dates (parsing + serializing)

Open ocram opened this issue 8 years ago • 7 comments

Meteor stores dates as timestamps in milliseconds. The appropriate Java type would be long.

But when a client communicates with the server, the server always sends them in another form and expects them in the same form when it receives data from the client.

What Meteor does is wrapping a plain timestamp such as 1459443366000 in a JSON object containing the date as a property, such as { "$date": 1459443366000 }.

So when you receive a date, the value is not the timestamp itself but the JSON object that you must parse the date from, first.

And when you try to send a date, you can't just put the timestamp right in there but must wrap it in a JSON string, e.g. myMap.put("myDate", "{ \"$date\": 1459443366000 }");.

We should add built-in support to make this easier.

ocram avatar Mar 31 '16 17:03 ocram

thanks!

romaluca avatar Apr 01 '16 10:04 romaluca

@romaluca Did that solve your problem?

In general, my idea for the solution to this problem would be as follows:

  1. When this library parses the JSON for you (i.e. when you're using the built-in database), it can parse the JSON date for you. It will just replace the { "$date": 1459443366000 } string with a java.util.Date or java.util.Calendar instance, or a long primitive.
  2. Whenever you put a java.util.Date or java.util.Calendar instance into a Map that is used in insert, update or remove, it will be transformed into the corresponding JSON date string. Unfortunately, we can't do this for long primitives, as we don't know if it's a date or something else.

The first change would be a breaking change, but shold be helpful. We still have to decide on the data type to use.

The second change could be implemented right away without any downsides.

ocram avatar Apr 01 '16 16:04 ocram

@ocram I am sending the date while subscribing but i will get the error "Match Failed" in android. the format i am sending is "1970-01-01 00:00:00 +0000". the same exact date format we are using in IOS it is working fine. can u please give a solution for this as soon as possible. Thank u

Tailmaters avatar Nov 24 '16 07:11 Tailmaters

@Tailmaters I'm afraid that is not really enough information for us to know what's your problem. Does your server expect a Date instance here? If so, send the date as a string, as shown above:

"{ \"$date\": "+date.getTime()+" }"

Otherwise, your problem is not really relate to this issue.

the same exact date format we are using in IOS it is working fine.

That doesn't mean anything! What works on iOS is probably unrelated to what's working on Android.

ocram avatar Nov 26 '16 13:11 ocram

@ocram Can u please Share your email-id. We have some Issues with connecting and Fetching Data from the collections.we need to discuss with u how exactly it need to be done.

Tailmaters avatar Nov 28 '16 09:11 Tailmaters

In general, I prefer public discussions here in the issues which help everybody using this library. If someone has the same problem later, they can benefit as well.

My email address can be found by going to the website linked in my profile and choosing "Contact".

ocram avatar Nov 29 '16 22:11 ocram

See also: https://github.com/meteor/meteor/blob/b6c2991a28b2ac4e635081133360b55c1e4387f6/packages/ejson/ejson.js#L90

ocram avatar Feb 17 '17 14:02 ocram