Skip to Content
Menu
This question has been flagged
3 Replies
13890 Views

import pytz

from datetime import datetime
from datetime import timedelta

from openerp import SUPERUSER_ID

        user_pool = self.pool.get('res.users')
        user = user_pool.browse(self.cr, SUPERUSER_ID, self.uid)
        tz = pytz.timezone(user.partner_id.tz) or pytz.utc
        # Suppose 'tz' fetch 'Asia/Kolkata'

# Suppose date_end = '2014-06-17 16:25:25'
        date_from = pytz.utc.localize(datetime.strptime(date_end, "%Y-%m-%d %H:%M:%S")).astimezone(tz)

#        Now if i print date_form --->'2014-06-17 16:25:25+05:30' but i want to print it in '2014-06-17 21:55:25'

please help me guys

Thanks

Avatar
Discard
Best Answer

I have the same problem and solving through this function passing DateTime would be converted to UTC format and your timezone to save on the database as UTC 

def convert_TZ_UTC(self,mydate, tz):
    tz = tz if tz else self.env.user.tz
fmt = "%Y-%m-%d %H:%M:%S"
# Current time in UTC
now_utc = datetime.now(timezone('UTC'))
# Convert to current user time zone
now_timezone = now_utc.astimezone(tz)
UTC_OFFSET_TIMEDELTA = datetime.strptime(now_utc.strftime(fmt), fmt) - datetime.strptime(now_timezone.strftime(fmt),
fmt)
local_datetime = datetime.strptime(mydate.strftime(fmt), fmt)
result_utc_datetime = local_datetime + UTC_OFFSET_TIMEDELTA
return result_utc_datetime.strftime(fmt)


Avatar
Discard
Best Answer

You really have to check tz value. If this happen

'2014-06-17 16:25:25+05:30' but i want to print it in '2014-06-17 21:55:25'

then tz = pytz.utc.

Avatar
Discard
Author

Thanks Ben Bernard

Related Posts Replies Views Activity
2
Sep 23
6394
2
Apr 23
1729
2
Mar 23
44268
2
Dec 23
54322
0
Mar 22
1026