Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
3 Odpowiedzi
14817 Widoki

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

Awatar
Odrzuć
Najlepsza odpowiedź

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)


Awatar
Odrzuć
Najlepsza odpowiedź

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.

Awatar
Odrzuć
Autor

Thanks Ben Bernard

Powiązane posty Odpowiedzi Widoki Czynność
2
wrz 23
6951
2
kwi 23
2618
2
mar 23
45600
2
gru 23
56227
0
mar 22
1667