Bendemann
Bendemann

Reputation: 776

Issues with TimeStamp precision at RESTful services

I am working with some data on postgreSQL and I am fetching them using JPA specifications. When displaying them in Angular front-end I can already see that the date is missing precision, namely from 2018-01-26 11:48:07.13487 it is something like 2018-01-26 11:48:07.134, that is it is missing 2 digits of precision. Now the problem is that this column is part of the primary key so that I cannot work on operations like put later on because the date won't match.
The problem is really happening at the REST services. If I print the data on eclipse the date precision appears totally fine but then when I check them at REST it has lost 2 digits of precision 2018-01-26T10:48:07.134+0000.
Conversely, I can try to fix the precision on the front-end (that is, 2018-01-26 11:48:07.13487) and then issue a put request. On the back-end I can notice that it looses the precision and goes back to 2018-01-26 11:48:07.134.

I am testing on very simple RESTful services like for instance:

@GetMapping("/getAllData")
  @ResponseBody
  public List<StammSolvaraJahr> getAllData() {
    EntityManager em = Connection.getEntityManger();
    return em.createQuery("SELECT ssj FROM StammSolvaraJahr ssj").setMaxResults(100).getResultList();
  }

Upvotes: 0

Views: 250

Answers (1)

JimmyB
JimmyB

Reputation: 12620

That's why using timestamps as keys is discouraged, same reason for floating point types. Note that JSON does not define a specific representation for dates/times and thus no precision/resolution which must be supported. So you're bound to get into trouble if you rely on it. An option may be to send the timestamp with e.g. seconds resolution in one field and the fractionals (e.g. in nanoseconds) in another integer field.

Upvotes: 1

Related Questions