summaryrefslogtreecommitdiffstats
path: root/_posts/2014-06-28-Python-datetime-sucks.md
blob: 54ff4d16a75a65202421e39b45bb97ab9e45654b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
---
# vim: tw=82
title: Python's datetime sucks
layout: post
tags: [python]
---

I've been playing with Python for about a year now, and I like pretty much
everything about it. There's one thing that's really rather bad and really should
not be that bad, however - date & time support. It's ridiculous how bad it is in
Python. This is what you get with the standard datetime module:

* The current time and strftime, with a reasonable set of properties
* Time deltas with days, seconds, and microseconds and nothing else
* Acceptable support for parsing dates and times

What you don't get is:

* Meaningful time deltas
* Useful arithmetic

Date and time support is a rather tricky thing to do and it's something that the
standard library should support well enough to put it in the back of your mind
instead of making you do all the work.

We'll be comparing it to C# and .NET.

Let's say I want to get the total hours between two `datetime`s.

{% highlight csharp %}
// C#
DateTime a, b;
double hours = (b - a).TotalHours;
{% endhighlight %}

{% highlight python %}
# Python
a, b = ...
hours = (b - a).seconds / 60 / 60
{% endhighlight %}

That's not so bad. How about getting the time exactly one month in the future:

{% highlight csharp %}
var a = DateTime.Now.AddMonths(1);
{% endhighlight %}

{% highlight python %}
a = date.now() + timedelta(days=30)
{% endhighlight %}

Well, that's not ideal. In C#, if you add one month to Janurary 30th, you get
Feburary 28th (or leap day if appropriate). In Python, you could write a janky
function to do this for you, or you could use the crappy alternative I wrote
above.

How about if I want to take a delta between dates and show it somewhere, like a
countdown? Say an event is happening at some point in the future and I want to
print "3 days, 5 hours, 12 minutes, 10 seconds left". This is distinct from the
first example, which could give you "50 hours", whereas this example would give
you "2 days, 2 hours".

{% highlight csharp %}
DateTime future = ...;
var delta = future - DateTime.Now;
Console.WriteLine("{0} days, {1} hours, {2} minutes, {3} seconds left",
    delta.Days,
    delta.Hours,
    delta.Minutes,
    delta.Seconds);
{% endhighlight %}

{% highlight python %}
# ...mess of math you have to implement yourself omitted...
{% endhighlight %}

Maybe I have a website where users can set their locale?

{% highlight csharp %}
DateTime a = ...;
Console.WriteLine(a.ToString("some format string", user.Locale));
{% endhighlight %}

{% highlight python %}
locale.setlocale(locale.LC_TIME, "sv_SE") # Global!
print(time.strftime("some format string"))
{% endhighlight %}

By the way, that Python one doesn't work on Windows. It uses system locales names
which are different on Windows than on Linux or OS X. Mono (cross-platform .NET)
handles this for you on any system.

And a few other cases that are easy in .NET and not in Python:

* Days since the start of this year
* Constants like the days in every month
* Is it currently DST in this timezone?
* Is this a leap year?

In short, Python's datetime module could really use a lot of fleshing out. This
is common stuff and easy for a naive programmer to do wrong.