Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
3 ตอบกลับ
15177 มุมมอง

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

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

http://stackoverflow.com/questions/79797/how-do-i-convert-local-time-to-utc-in-python

อวตาร
ละทิ้ง
ผู้เขียน

Thanks Prakash

คำตอบที่ดีที่สุด

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

Related Posts ตอบกลับ มุมมอง กิจกรรม
2
ก.ย. 23
7474
2
เม.ย. 23
3102
2
มี.ค. 23
46481
2
ธ.ค. 23
57344
0
มี.ค. 22
2144