Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
3813 Lượt xem

Hello,


I have this piece of code to fill a text field with a time:


MyDate = record.x_studio_approved_date_time_exit + datetime.timedelta(hours=1)


As you see, I hard coded the time difference with odoo server. But I would like to remove that hard code and make it work all year long (and not modify hours to 1 or 2 depending on summer or winter).

Could you tell me how I must change this to always show my time in CET?


Thank you for your help!

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

Thank you, but I need additional precision.

My answer is probably in:

user_tz = self.env.user.tz
localutc = pytz.timezone('UTC')
to_usertz = pytz.timezone(user_tz) or pytz.utc

So is it correct to do it like below:

original hard coding:

MyDate = record.x_studio_approved_date_time_exit + datetime.timedelta(hours=2)
record['x_studio_time_exit'] = MyDate.strftime("%H:%M")

Modified code:

user_tz = self.env.user.tz
MyDate = record.x_studio_approved_date_time_exit.user_tz
record['x_studio_time_exit'] = MyDate.strftime("%H:%M")


But it gives me an error...




Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,

We can get the time based on the timezone 

Try this, 

Using this in Jquery

var date = moment.utc("Value").local();
var formatted_date = date.format('YYYY-MM-DD HH:mm:ss');

Using this in Python

user_tz = self.env.user.tz
localutc = pytz.timezone('UTC')
to_usertz = pytz.timezone(user_tz) or pytz.utc

OR

time = fields.Datetime.from_string("Value")
formatted_time = (time + timedelta(hours=1)).strftime('%Y-%m-%d %H:%M:%S')

Regards

Ảnh đại diện
Huỷ bỏ