[FIX] Calculate date interval using super user time zone
authorSamus CTO <cto@openerp.com>
Fri, 8 Aug 2014 09:46:41 +0000 (11:46 +0200)
committerSamus CTO <cto@openerp.com>
Fri, 8 Aug 2014 16:01:13 +0000 (18:01 +0200)
When you set the date of a cron the July 1st at midnight, if the user
time zone has a positive offset, then the converted UTC date is the
June 30th and adding 1 month will end up on July 30th translating to
July 31th instead of September 1st.

To solve this issue we use the super user time zone for the date
calculation.

openerp/addons/base/ir/ir_cron.py

index 77c9835..cfabd97 100644 (file)
@@ -24,9 +24,10 @@ import time
 import psycopg2
 from datetime import datetime
 from dateutil.relativedelta import relativedelta
+import pytz
 
 import openerp
-from openerp import netsvc
+from openerp import netsvc, SUPERUSER_ID
 from openerp.osv import fields, osv
 from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
 from openerp.tools.safe_eval import safe_eval as eval
@@ -149,8 +150,8 @@ class ir_cron(osv.osv):
             must not be committed/rolled back!
         """
         try:
-            now = datetime.now() 
-            nextcall = datetime.strptime(job['nextcall'], DEFAULT_SERVER_DATETIME_FORMAT)
+            now = fields.datetime.context_timestamp(job_cr, SUPERUSER_ID, datetime.now())
+            nextcall = fields.datetime.context_timestamp(job_cr, SUPERUSER_ID, datetime.strptime(job['nextcall'], DEFAULT_SERVER_DATETIME_FORMAT))
             numbercall = job['numbercall']
 
             ok = False
@@ -166,7 +167,7 @@ class ir_cron(osv.osv):
             if not numbercall:
                 addsql = ', active=False'
             cron_cr.execute("UPDATE ir_cron SET nextcall=%s, numbercall=%s"+addsql+" WHERE id=%s",
-                       (nextcall.strftime(DEFAULT_SERVER_DATETIME_FORMAT), numbercall, job['id']))
+                       (nextcall.astimezone(pytz.UTC).strftime(DEFAULT_SERVER_DATETIME_FORMAT), numbercall, job['id']))
 
         finally:
             job_cr.commit()