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

I have created a computed field to calculate the Hour and Minute ( ) from check_in, I have changed it to user timezone


 when i create a new record it shows in user timezone, but in my database i already have a month attendance records for all these records it shows UTC time


How can i convert this into user timezone

My code is here,

@api.depends('check_in', 'check_out')
def compute_time(self):
for attendance in self:
intime = attendance.check_in
outtime = attendance.check_out
user_timezone = self.env.user.tz or 'UTC'
print(user_timezone)
user_tz = timezone(user_timezone)
print(user_tz)
intime = fields.Datetime.context_timestamp(self, intime).astimezone(user_tz)
print(intime)
intime = intime.strftime('%H.%M') if intime else '00.00'
print(intime)
attendance.in_time = intime
print(attendance.in_time)
if outtime:
outtime = fields.Datetime.context_timestamp(self, outtime).astimezone(user_tz)
outtime = outtime.strftime('%H.%M') if outtime else '00.00'
attendance.out_time = outtime
else:
attendance.out_time = '00.00'

If anyone knows kindly reply

Thanks

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

Hi,

Use  fields.Datetime.context_timestamp(self, outtime). 

This method returns the provided timestamp converted to the client's timezone.


Hope it helps

อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
3
เม.ย. 25
3852
Compute Fields แก้ไขแล้ว
2
ก.ค. 24
1265
1
ก.ค. 22
1469
0
ต.ค. 24
13977
4
มี.ค. 24
10228