简体   繁体   中英

how to change odoo server timezone to user timezone in attendance report

我已经定制了一个 odoo 报告来打印员工考勤 签入和签出的时间在 UI 中是正确的但是当我打印报告时,每条记录都会增加 2 小时

It is a timezone problem: dates in Odoo are written as UTC datetime objects. When a date is rendered in the browser UI, it is automatically converted to the user's device timezone, but this conversion is not automatic when it comes to report, where datetime fields are rendered exactly as they are saved in the database.

Try to create a method that parses the datetime objects according to the user configuration:

import logging
import pytz
_logger = logging.getLogger(__name__)

def convert_datetime_field(datetime_field, user=None):
    dt = datetime.strptime(datetime_field, '%Y-%m-%d %H:%M:%S')
    if user and user.tz:
        user_tz = user.tz
        if user_tz in pytz.all_timezones:
            old_tz = pytz.timezone('UTC')
            new_tz = pytz.timezone(user_tz)
            dt = old_tz.localize(dt).astimezone(new_tz)
        else:
            _logger.info("Unknown timezone {}".format(user_tz))

    return datetime.strftime(dt, '%d/%m/%Y %H:%M:%S')

It was time zone problem. set up your correct time Zone.

Step 1.

**Click on your logged account name and choose preferences. **

Preferences喜好

Step 2

** change the time zone ** Time zone 时区

Very simple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM