Postgresql 日期格式

发布于 2024-10-27 23:17:56 字数 189 浏览 1 评论 0原文

我有一个 Web 应用程序(用 python/django 编写),(由于规范不正确)Web 表单期望“YYYY-mm-dd”日期格式,而其他应用程序则使用“dd/mm/yy”日期格式。

有没有办法告诉 postgresql 接受两种格式的日期?例如,尝试“dd/mm/yy”,如果失败,则尝试“yyyy-mm-dd”。

那太棒了。

I have a web application (written with python/django) that (due a bad specification) Web Forms expecting "YYYY-mm-dd" date format and others using "dd/mm/yy" date format.

Is there a way to tell postgresql to accept dates in both formats? for example, to try "dd/mm/yy" and, if it fails, then try "yyyy-mm-dd".

That would be awesome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬シ愛 2024-11-03 23:17:56

来自精细手册

日期和时间输入几乎可以接受任何合理的格式,包括 ISO 8601、SQL 兼容、传统的 POSTGRES 等。对于某些格式,日期输入中的日、月和年顺序不明确,并且支持指定这些字段的预期顺序。将 DateStyle 参数设置为 MDY 以选择月-日-年解释,将 DMY 设置为选择日-月-年解释,或将 YMD 设置为选择年-月-日解释。

PostgreSQL 在处理日期/时间输入方面比 SQL 标准要求的更加灵活。请参阅附录 B 了解日期/时间输入的确切解析规则以及识别的文本字段(包括月份、星期几和时区)。

因此 PostgreSQL 应该能够处理您输入的几乎任何日期格式。但是,您的“dd/mm/yy”格式不明确。但是,有 DateStyle配置参数可以帮助解决这种歧义。

例如:

=> create table x (d date not null);
=> insert into x values ('2001-01-10');
=> insert into x values ('Feb 2 2980');
=> insert into x values ('01/02/03');
=> select * from x;
     d      
------------
 2001-01-10
 2980-02-02
 2003-02-01

也就是说,我建议在内部将所有内容移至 ISO 8601 (YYYY-MM-DD),并在应用程序边缘处理转换。 OTOH,有现实需要应对,所以你应该尽一切努力让它继续下去。

From the fine manual:

Date and time input is accepted in almost any reasonable format, including ISO 8601, SQL-compatible, traditional POSTGRES, and others. For some formats, ordering of day, month, and year in date input is ambiguous and there is support for specifying the expected ordering of these fields. Set the DateStyle parameter to MDY to select month-day-year interpretation, DMY to select day-month-year interpretation, or YMD to select year-month-day interpretation.

PostgreSQL is more flexible in handling date/time input than the SQL standard requires. See Appendix B for the exact parsing rules of date/time input and for the recognized text fields including months, days of the week, and time zones.

So PostgreSQL should be able to deal with just about any date format you throw at it. Your "dd/mm/yy" format is, however, ambiguous. But, there is the DateStyle configuration parameter to help with such ambiguity.

For example:

=> create table x (d date not null);
=> insert into x values ('2001-01-10');
=> insert into x values ('Feb 2 2980');
=> insert into x values ('01/02/03');
=> select * from x;
     d      
------------
 2001-01-10
 2980-02-02
 2003-02-01

That said, I'd recommend moving everything to ISO 8601 (YYYY-MM-DD) internally and handle the conversions at the edges of the application. OTOH, there is reality to contend with so you should do whatever you have to do to make it go.

不必在意 2024-11-03 23:17:56

如果这些是唯一可能的两种格式,那么最好明确只允许这些格式,而不是依赖 postgres 来解释。例如:

with w as (select '2011-12-13' as input_date union select '13/12/2011')
select case when input_date~'^\d\d\d\d-\d\d-\d\d
 
                 then to_date(input_date, 'yyyy-mm-dd')
            when input_date~'^\d\d/\d\d/\d\d\d\d
 
                 then to_date(input_date, 'dd/mm/yyyy')
            end 
from w;

    case
------------
 2011-12-13
 2011-12-13
(2 rows)

If those are the only two formats possible then it may be better to explicitly allow only those, rather than rely on postgres to interpret. For example:

with w as (select '2011-12-13' as input_date union select '13/12/2011')
select case when input_date~'^\d\d\d\d-\d\d-\d\d
 
                 then to_date(input_date, 'yyyy-mm-dd')
            when input_date~'^\d\d/\d\d/\d\d\d\d
 
                 then to_date(input_date, 'dd/mm/yyyy')
            end 
from w;

    case
------------
 2011-12-13
 2011-12-13
(2 rows)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文