3

I have a method in which self is a simple stock.move record. Besides, as you may know, stock.move model has a Datetime field named date. If I write this code inside the method:

lines = self.search([
    ('date', '=', self.date),
])

It should return me at least the current line. And it does, but only in some servers. Others return nothing. Why? Because if I do a query to get the date in PostgreSQL, the servers which are working OK return 2017-12-27 17:10:00, however, the servers which are working wrong return 2017-12-27 17:10:00.131112. So, for the cases with miliseconds the ORM search method is doing this:

lines = self.search([
    (2017-12-27 17:10:00.131112, '=', 2017-12-27 17:10:00),
])

self.date is returning the Datetime value without the miliseconds and that's why the comparison fails. I need to get the miliseconds too.

How can I manage this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
forvas
  • 9,801
  • 7
  • 62
  • 158
  • Actually I think that `create_date` and `write_date` fields should have miliseconds. `Datetime` fields shouldn't have because the `DEFAULT_SERVER_DATETIME_FORMAT` constant is used. So I don't know what's happening in your case – ChesuCR Dec 30 '17 at 11:25

1 Answers1

1

Absolute right, I confirm here odoo Datetime does not keep timestame in UTC, it store datetime in UTC. Even since odoo 5.0 we working on it, till date.

In Odoo 11.0

 id  |        create_date         |         write_date         |    date_expected    
-----+----------------------------+----------------------------+---------------------
  41 | 2017-12-21 13:06:06.927814 | 2017-12-21 13:06:06.927814 | 2017-12-21 13:01:30
   7 | 2017-12-21 08:33:22.431197 | 2017-12-21 08:33:22.431197 | 2017-12-21 08:31:54
  42 | 2017-12-21 13:07:03.659194 | 2017-12-21 13:07:03.659194 | 2017-12-21 13:06:54
   2 | 2017-12-21 07:24:24.953689 | 2017-12-21 07:24:24.953689 | 2017-12-21 07:24:25

Yes. odoo create_date, write_date able to gives you millisecond, might it helpful you. but sure in Datetime, not allow to do so...

In case you need really millisecond based needs, Additional field add to store millisecond, than all it works.

Problem persist in odoo Community and Enterprise edition both.

*** NEED ADDITIONAL SMALL CUSTOMIZATION

Tejas Tank
  • 1,100
  • 2
  • 16
  • 28
  • If I add an additional field to get the miliseconds, from where do I have to take its value? Users fill in field `date`, and in ORM `create` method I get the datetime with no miliseconds, so I can't tell `create` to store the milisecond part into the additional field. However, I solved my particular problem adding +1 second in the search comparison, but my solution doesn't answer the question I'm doing. – forvas Jan 02 '18 at 11:28
  • Overide odoo create method, in that you can store timestamp or millisecond in additional field, directly. I am not sure what you working on module, but create method helps to resovle this. – Tejas Tank Jan 03 '18 at 15:37
  • I just computed a char field to store write_date formatted with milliseconds read_date_time = rec.create_date.strftime('%Y-%m-%d %H:%M:%S.%f') – Derick Aug 18 '21 at 05:47