python-humanize / python-humanize/humanize

Accept ISO 8601 string, in addition to datetime/date/time object

未关闭
#129 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
Python
星标
756
派生
148
平均合并
6 天 1 小时
30 天内合并 PR
12

描述

Motivation:

I believe that the humanize functions are often used indrectly from template engines. In my case I use Jinja2 templating engine and the jinja2_humanize_extension, which relies on the humanize package.

{{ value | humanize_naturaldate }}

In many cases the underlying data (either a database, json-file or csv-file) stores/returns the datetime (or date or time) as a string, formatted in ISO8601 format. But in order to make humanize_naturaldate accept this, I need to first parse it.

{{ value | fromisoformat | humanize_naturaldate }}

The fromisoformat then needs to be set as a filter in the local or global jinja environment.

It would be great if humanize accepted these ISO8661 strings itself.

How to implement:

As far as I can tell, it would be very simple to implement and very simple to maintain. Every function callable by the client, that takes a datetime should start with:

if type(value) == str:
    try:
        value = datetime.datetime.fromisoformat(value)
    else:
        value = str

Similarly for date and time objects.

I will admit that I have only studied the code cursorily, and I might have missed some reasons why this is more complex than I currently believe it is.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先定位接受 datetime、date 或 time 值的公共 humanize 函数,包括 humanize_naturaldate,并检查它们当前的输入处理方式。检查现有测试如何覆盖这些入口点。完成的标准是:受支持的 ISO 8601 字符串能够被一致地接受,同时现有的对象输入和无效值保留已定义的行为。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
42/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。