{"id":10021054,"date":"2025-10-30T00:11:13","date_gmt":"2025-10-29T16:11:13","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021054"},"modified":"2025-10-30T00:11:13","modified_gmt":"2025-10-29T16:11:13","slug":"%e4%bd%bf%e7%94%a8pydantic-%e5%a4%84%e7%90%86%e5%90%84%e7%a7%8d%e5%90%84%e6%a0%b7%e4%b9%b1%e4%b8%83%e5%85%ab%e7%b3%9f%e7%9a%84%e6%97%b6%e9%97%b4%e6%a0%bc%e5%bc%8f%e5%ad%97%e7%ac%a6%e4%b8%b2%ef%bc%8c","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021054","title":{"rendered":"\u4f7f\u7528pydantic \u5904\u7406\u5404\u79cd\u5404\u6837\u4e71\u4e03\u516b\u7cdf\u7684\u65f6\u95f4\u683c\u5f0f\u5b57\u7b26\u4e32\uff0c\u5c24\u5176\u662f iso8601"},"content":{"rendered":"<p>\u8fd9\u4e2a\u6559\u7a0b\u662f\u53eb\u4f60\u600e\u4e48\u4f18\u96c5\u7684\u5904\u7406\u5404\u79cd\u4e71\u4e03\u516b\u7cdf\u7684\u65e5\u671f\u5b57\u7b26\u4e32<\/p>\n<p>\u6309\u7167 iso8601\uff0c\u4e0b\u9762\u7684\u683c\u5f0f\u90fd\u662f\u5408\u6cd5\u7684<\/p>\n<ul>\n<li>2023-08-15T12:34:56Z<\/li>\n<li>2023-08-15 12:34:56+00:00<\/li>\n<li>2023-08-15 12:34:56<\/li>\n<\/ul>\n<p>\u5982\u679c\u4f60\u8981\u81ea\u5df1\u5199 re \u53bb\u5904\u7406\u8fd9\u79cd T\u3001+ \u3001Z \u8fd9\u4e9b\u533a\u522b\uff0c\u5c31\u4f1a\u5f88\u9ebb\u70e6\u5f88\u9ebb\u70e6<\/p>\n<p>\u600e\u4e48\u4f18\u96c5\u7684\u5904\u7406\uff1f\u7528 pydantic \u5c31\u884c\uff0cpydantic \u5df2\u7ecf\u628a\u8fd9\u79cd\u4e71\u4e03\u516b\u7cdf\u7684\u95ee\u9898\u90fd\u8003\u8651\u8fdb\u53bb\u4e86<\/p>\n<p>\u53ea\u6709\u5bfc\u5165 <code>from pydantic.v1.datetime_parse import parse_datetime<\/code> \u5c31\u884c\u4e86<\/p>\n<pre><code class=\"python\">from pydantic.v1.datetime_parse import parse_datetime\n\n# \u7ed9\u6211\u4e00\u5806 parse_datetime \u7684\u793a\u4f8b\ndt_strs = [\n    '2023-08-15T12:34:56Z',\n    '2023-08-15 12:34:56+00:00',\n    '2023-08-15 12:34:56',\n]\n\nfor dt_str in dt_strs:\n    dt = parse_datetime(dt_str)\n    print(dt)\n<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u7684 pydantic \u662f 1.x \u7248\u672c\uff0c\u5c31\u7528 <code>from pydantic.datetime_parse import parse_datetime<\/code><\/p>\n<p>\u5982\u679c\u4f60\u7684 pydantic \u662f 2.x \u7248\u672c\uff0c\u5c31\u7528 <code>from pydantic.v1.datetime_parse import parse_datetime<\/code><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047356166\">https:\/\/segmentfault.com\/a\/1190000047356166<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e2a\u6559\u7a0b\u662f\u53eb\u4f60\u600e\u4e48\u4f18\u96c5\u7684\u5904\u7406\u5404\u79cd\u4e71\u4e03\u516b\u7cdf\u7684\u65e5\u671f\u5b57\u7b26\u4e32 \u6309\u7167 iso8601\uff0c\u4e0b\u9762\u7684\u683c\u5f0f\u90fd\u662f\u5408\u6cd5\u7684 2023-0 [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021054","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021054","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021054"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021054\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}