<story><title>Python datetime pitfalls, and what libraries are (not) doing about it</title><url>https://dev.arie.bovenberg.net/blog/python-datetime-pitfalls/</url></story><parent_chain></parent_chain><comment><author>the__alchemist</author><text>I will state this briefly as a tangent. This article mentions third party libraries; the top are Arrow and Pendulum. These are a wellspring of subtle bugs due to a decision they made that I suspect is based off the Javascript library Moment: They conflate Dates, Times, and Datetimes, as a single type. If you are using their Arrow etc type to represent a Date or Time, the resulting code will be fragile, and fail in surprising ways. Additionally, if my function signature accepts a time or date, you have no way of validating the parameter, and can do things like ask for &amp;quot;seconds&amp;quot; on a date. The moment&amp;#x2F;arrow&amp;#x2F;pendulum community advocates that the concept of a date or time individually is meaningless; I disagree. It is application specific, and there are applications that call for any of the 3 variants.&lt;p&gt;Python&amp;#x27;s builtin `datetime` library has flaws as pointed out in the article, but is safer than Arrow or Moment. I think Rust&amp;#x27;s Chrono lib is the best datetime library I&amp;#x27;ve seen, in terms of not surprising you with errors.</text></comment>
<story><title>Python datetime pitfalls, and what libraries are (not) doing about it</title><url>https://dev.arie.bovenberg.net/blog/python-datetime-pitfalls/</url></story><parent_chain></parent_chain><comment><author>guitarnick</author><text>The author provides this example to illustrate inconsistent handling of non-existent datetimes:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    # This time doesn&amp;#x27;t exist on this date
    d = datetime(2023, 3, 26, 2, 30, tzinfo=paris)

    # No timestamp exists, so it just makes one up
    t = d.timestamp()
    datetime.fromtimestamp(t) == d  # False
&lt;/code&gt;&lt;/pre&gt;
Criticisms:&lt;p&gt;1) The example would fail just as well for any datetime with given tzinfo. Because fromtimestamp returns native datetimes.&lt;p&gt;2) The timestamp isn&amp;#x27;t just &amp;quot;made up&amp;quot;. Its behaviour is clearly documented in PEP 495, as linked by the author [0]. In this case it consistently corresponds to datetime(2023, 3, 26, 3, 30, tzinfo=paris).&lt;p&gt;[0] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;peps.python.org&amp;#x2F;pep-0495&amp;#x2F;&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;peps.python.org&amp;#x2F;pep-0495&amp;#x2F;&lt;/a&gt;&lt;p&gt;Finally if we disallow creating non-existent datetimes in the proposed library, how do we represent the 2am in &amp;quot;clock changes forwards at 2am&amp;quot;? Use 3am? There are tradeoffs.</text></comment>