def attributes(self, record, field_name, options, values): attrs = super(DateTime, self).attributes(record, field_name, options, values) if options.get('inherit_branding'): value = record[field_name] lg = self.env['res.lang']._lang_get( self.env.user.lang) or get_lang(self.env) locale = babel_locale_parse(lg.code) babel_format = value_format = posix_to_ldml( '%s %s' % (lg.date_format, lg.time_format), locale=locale) tz = record.env.context.get('tz') or self.env.user.tz if isinstance(value, str): value = fields.Datetime.from_string(value) if value: # convert from UTC (server timezone) to user timezone value = fields.Datetime.context_timestamp( self.with_context(tz=tz), timestamp=value) value_format = pycompat.to_text( babel.dates.format_datetime(value, format=babel_format, locale=locale)) value = fields.Datetime.to_string(value) attrs['data-oe-original'] = value attrs['data-oe-original-with-format'] = value_format attrs['data-oe-original-tz'] = tz return attrs
def value_to_html(self, value, options): if not value: return '' lang = self.user_lang() locale = babel_locale_parse(lang.code) if isinstance(value, pycompat.string_types): value = fields.Datetime.from_string(value) value = fields.Datetime.context_timestamp(self, value) if options and 'format' in options: pattern = options['format'] else: if options and options.get('time_only'): strftime_pattern = (u"%s" % (lang.time_format)) else: strftime_pattern = (u"%s %s" % (lang.date_format, lang.time_format)) pattern = posix_to_ldml(strftime_pattern, locale=locale) if options and options.get('hide_seconds'): pattern = pattern.replace(":ss", "").replace(":s", "") return pycompat.to_text(babel.dates.format_datetime(value, format=pattern, locale=locale))
def attributes(self, record, field_name, options, values): attrs = super(Date, self).attributes(record, field_name, options, values) if options.get('inherit_branding'): attrs['data-oe-original'] = record[field_name] if record._fields[field_name].type == 'datetime': attrs = self.env['ir.qweb.field.datetime'].attributes( record, field_name, options, values) attrs['data-oe-type'] = 'datetime' return attrs lg = self.env['res.lang']._lang_get( self.env.user.lang) or get_lang(self.env) locale = babel_locale_parse(lg.code) babel_format = value_format = posix_to_ldml(lg.date_format, locale=locale) if record[field_name]: date = fields.Date.from_string(record[field_name]) value_format = pycompat.to_text( babel.dates.format_date(date, format=babel_format, locale=locale)) attrs['data-oe-original-with-format'] = value_format return attrs
def _get_locale_time(self, dt_time, lang_code): """ Get locale time from datetime object :param dt_time: datetime object :param lang_code: language code (eg. en_US) """ locale = babel_locale_parse(lang_code) return babel.dates.format_time(dt_time, format='short', locale=locale)
def value_to_html(self, value, options): locale = babel_locale_parse(self.user_lang().code) if isinstance(value, pycompat.string_types): value = fields.Datetime.from_string(value) # value should be a naive datetime in UTC. So is fields.Datetime.now() reference = fields.Datetime.from_string(options['now']) return pycompat.to_text(babel.dates.format_timedelta(value - reference, add_direction=True, locale=locale))
def _get_totp_mail_code(self): self.ensure_one() key = self._get_totp_mail_key() now = datetime.now() counter = int(datetime.timestamp(now) / 3600) code = hotp(key, counter) expiration = timedelta(seconds=3600) lang = babel_locale_parse(self.env.context.get('lang') or self.lang) expiration = babel.dates.format_timedelta(expiration, lang) return str(code).zfill(6), expiration
def value_to_html(self, value, options): if not value: return '' options = options or {} lang = self.user_lang() locale = babel_locale_parse(lang.code) format_func = babel.dates.format_datetime if isinstance(value, str): value = fields.Datetime.from_string(value) value = fields.Datetime.context_timestamp(self, value) if options.get('tz_name'): tzinfo = babel.dates.get_timezone(options['tz_name']) else: tzinfo = None if 'format' in options: pattern = options['format'] else: if options.get('time_only'): strftime_pattern = (u"%s" % (lang.time_format)) elif options.get('date_only'): strftime_pattern = (u"%s" % (lang.date_format)) else: strftime_pattern = (u"%s %s" % (lang.date_format, lang.time_format)) pattern = posix_to_ldml(strftime_pattern, locale=locale) if options.get('hide_seconds'): pattern = pattern.replace(":ss", "").replace(":s", "") if options.get('time_only'): format_func = babel.dates.format_time return pycompat.to_text( format_func(value, format=pattern, tzinfo=tzinfo, locale=locale)) if options.get('date_only'): format_func = babel.dates.format_date return pycompat.to_text( format_func(value, format=pattern, locale=locale)) return pycompat.to_text( format_func(value, format=pattern, tzinfo=tzinfo, locale=locale))
def value_to_html(self, value, options): if value < 0: raise ValueError( _("The value (%s) passed should be positive", value)) hours, minutes = divmod(int(abs(value) * 60), 60) if hours > 23: raise ValueError(_("The hour must be between 0 and 23")) t = time(hour=hours, minute=minutes) locale = babel_locale_parse(self.user_lang().code) pattern = options.get('format', 'short') return babel.dates.format_time(t, format=pattern, tzinfo=None, locale=locale)
def value_to_html(self, value, options): units = {unit: duration for unit, label, duration in TIMEDELTA_UNITS} locale = babel_locale_parse(self.user_lang().code) factor = units[options.get('unit', 'second')] round_to = units[options.get('round', 'second')] if options.get('digital') and round_to > 3600: round_to = 3600 r = round((value * factor) / round_to) * round_to sections = [] sign = '' if value < 0: r = -r sign = '-' if options.get('digital'): for unit, label, secs_per_unit in TIMEDELTA_UNITS: if secs_per_unit > 3600: continue v, r = divmod(r, secs_per_unit) if not v and (secs_per_unit > factor or secs_per_unit < round_to): continue sections.append(u"%02.0f" % int(round(v))) return sign + u':'.join(sections) for unit, label, secs_per_unit in TIMEDELTA_UNITS: v, r = divmod(r, secs_per_unit) if not v: continue section = babel.dates.format_timedelta( v * secs_per_unit, granularity=round_to, add_direction=options.get('add_direction'), format=options.get('format', 'long'), threshold=1, locale=locale) if section: sections.append(section) if sign: sections.insert(0, sign) return u' '.join(sections)
def value_to_html(self, value, options): units = dict(TIMEDELTA_UNITS) locale = babel_locale_parse(self.user_lang().code) factor = units[options.get('unit', 'second')] round_to = units[options.get('round', 'second')] if options.get('digital') and round_to > 3600: round_to = 3600 r = round((value * factor) / round_to) * round_to sections = [] if options.get('digital'): for unit, secs_per_unit in TIMEDELTA_UNITS: if secs_per_unit > 3600: continue v, r = divmod(r, secs_per_unit) if not v and (secs_per_unit > factor or secs_per_unit < round_to): continue if len(sections): sections.append(u':') sections.append(u"%02.0f" % int(round(v))) return u''.join(sections) if value < 0: r = -r sections.append(u'-') for unit, secs_per_unit in TIMEDELTA_UNITS: v, r = divmod(r, secs_per_unit) if not v: continue section = babel.dates.format_timedelta(v * secs_per_unit, threshold=1, locale=locale) if section: sections.append(section) return u' '.join(sections)