콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
3 답글
15305 화면

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

아바타
취소
베스트 답변

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)


아바타
취소
베스트 답변

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.

아바타
취소
작성자

Thanks Ben Bernard

관련 게시물 답글 화면 활동
2
9월 23
7582
2
4월 23
3278
2
3월 23
46630
2
12월 23
57589
0
3월 22
2295