def ctx_tz(record, field): res_lang = None ctx = record._context tz_name = pytz.timezone(ctx.get('tz') or record.env.user.tz) timestamp = Datetime.from_string(record[field]) if ctx.get('lang'): res_lang = record.env['res.lang'].search([('code', '=', ctx['lang'])], limit=1) if res_lang: timestamp = pytz.utc.localize(timestamp, is_dst=False) return datetime.strftime( timestamp.astimezone(tz_name), res_lang.date_format + ' ' + res_lang.time_format) return Datetime.context_timestamp(record, timestamp)
def reformat_server_datetime_for_frontend(cls, date_time, date_first=False, two_character_year=False, context_with_timezone=None): """ Reformat a datetime in Odoo's 'default server datetime format' (see imports) to one more appropriate for the front end. Can choose whether the date or time comes first and optionally convert to client timezone. :param date_time: :type date_time: str :param date_first: :type date_first: bool :param two_character_year: :type two_character_year: bool :param context_with_timezone: A record's context with a 'tz' key specifying the timezone of the current client. :type context_with_timezone: dict :return: :rtype: str """ date_time = cls.zero_seconds(date_time) date_time = datetime.strptime(date_time, DTF) time_format = cls.time_format_front_end date_format = cls.date_format_front_end if two_character_year: date_format = cls.date_format_front_end_two_character_year if date_first: datetime_format = cls.format_string.format(date_format, time_format) else: datetime_format = cls.format_string.format(time_format, date_format) if context_with_timezone: cls._context = context_with_timezone date_time = Datetime.context_timestamp(cls, date_time) date_time = date_time.strftime(datetime_format) return date_time
def UTC_Datetime_To_TW_TZ(timestr): if not timestr: return None return Datetime.context_timestamp(TimeZoneHelper_TW2, Datetime.from_string(timestr))