Repository: apache/superset
File: superset/models/helpers.py (Python)

Code before:
    @renders("changed_on")
    def changed_on_delta_humanized(self) -> str:
        return self.changed_on_humanized

    @renders("changed_on")
    def changed_on_dttm(self) -> float:
        return datetime_to_epoch(self.changed_on)

    @renders("created_on")
    def created_on_delta_humanized(self) -> str:
        return self.created_on_humanized

    @renders("changed_on")
    def changed_on_utc(self) -> str:
        # Convert naive datetime to UTC
        return self.changed_on.astimezone(pytz.utc).strftime("%Y-%m-%dT%H:%M:%S.%f%z")

    @property
    def changed_on_humanized(self) -> str:
        locale = str(get_locale() or 'en')
        try:
            humanize.i18n.activate(locale)
            result = humanize.naturaltime(datetime.now() - self.changed_on)
            humanize.i18n.deactivate()
            return result
        except:
            pass
        # Fallback to English
        return humanize.naturaltime(datetime.now() - self.changed_on)

    @property
    def created_on_humanized(self) -> str:
        locale = str(get_locale() or 'en')
        try:
            humanize.i18n.activate(locale)
            result = humanize.naturaltime(datetime.now() - self.created_on)
            humanize.i18n.deactivate()
            return result
        except:
            pass
        # Fallback to English
        return humanize.naturaltime(datetime.now() - self.created_on)

    @renders("changed_on")
    def modified(self) -> Markup:
        return Markup(f'<span class="no-wrap">{self.changed_on_humanized}</span>')


class QueryResult:  # pylint: disable=too-few-public-methods
    """Object returned by the query interface"""


Review comment (bug):
Are we expecting this to fail? In what cases? Not sure if swallowing anything/everything is best here, often if we know something might fail we'd try to swallow just that one particular exception, and maybe `logging.exception(e)` to handle the rest of the pessimisticism, so it's unexcpected, handled - but **logged** somewhere. 

Otherwise you may just never know that say your particular locale isn't handled by `humanize`.

Code after:

    @renders("changed_on")
    def changed_on_dttm(self) -> float:
        return datetime_to_epoch(self.changed_on)

    @renders("created_on")
    def created_on_delta_humanized(self) -> str:
        return self.created_on_humanized

    @renders("changed_on")
    def changed_on_utc(self) -> str:
        # Convert naive datetime to UTC
        return self.changed_on.astimezone(pytz.utc).strftime("%Y-%m-%dT%H:%M:%S.%f%z")

    def _format_time_humanized(self, timestamp: datetime) -> str:
        locale = str(get_locale())
        time_diff = datetime.now() - timestamp
        # Skip activation for 'en' locale as it's humanize's default locale
        if locale == "en":
            return humanize.naturaltime(time_diff)
        try:
            humanize.i18n.activate(locale)
            result = humanize.naturaltime(time_diff)
            humanize.i18n.deactivate()
            return result
        except Exception as e:
            logger.warning(f"Locale '{locale}' is not supported in humanize: {e}")
            return humanize.naturaltime(time_diff)

    @property
    def changed_on_humanized(self) -> str:
        return self._format_time_humanized(self.changed_on)

    @property
    def created_on_humanized(self) -> str:
        return self._format_time_humanized(self.created_on)

    @renders("changed_on")
    def modified(self) -> Markup:
        return Markup(f'<span class="no-wrap">{self.changed_on_humanized}</span>')


class QueryResult:  # pylint: disable=too-few-public-methods
    """Object returned by the query interface"""

    def __init__(  # pylint: disable=too-many-arguments
        self,
        df: pd.DataFrame,
        query: str,
        duration: timedelta,
        applied_template_filters: Optional[list[str]] = None,