Python's datetime sucks June 28, 2014 on Drew DeVault's blog

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:

What you don’t get is:

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 datetimes.

// C#
DateTime a, b;
double hours = (b - a).TotalHours;
# Python
a, b = ...
hours = (b - a).seconds / 60 / 60

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

var a = DateTime.Now.AddMonths(1);
a = date.now() + timedelta(days=30)

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”.

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);
# ...mess of math you have to implement yourself omitted...

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

DateTime a = ...;
Console.WriteLine(a.ToString("some format string", user.Locale));
locale.setlocale(locale.LC_TIME, "sv_SE") # Global!
print(time.strftime("some format string"))

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:

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.

Have a comment on one of my posts? Start a discussion in my public inbox by sending an email to ~sircmpwn/public-inbox@lists.sr.ht [mailing list etiquette]

Articles from blogs I read Generated by openring

Status update, August 2020

Hi! Regardless of the intense heat I’ve been exposed to this last month, I’ve still been able to get some stuff done (although having to move out to another room which isn’t right under the roof). I’ve worked a lot on IRC-related projects. I’ve added a znc-i…

via emersion 2020-08-19 00:00:00 +0200 +0200

What's cooking on Sourcehut? August 2020

Another month passes and we find ourselves writing (or reading) this status update on a quiet, rainy Sunday morning. Today our userbase numbers 16,683 members strong, up 580 from last month. Please extend a kind welcome to our new colleagues! Thanks for read…

via Blogs on Sourcehut 2020-08-16 00:00:00 +0000 +0000

Go 1.15 is released

Today the Go team is very happy to announce the release of Go 1.15. You can get it from the download page. Some of the highlights include: Substantial improvements to the Go linker Improved allocation for small objects at high core coun…

via The Go Programming Language Blog 2020-08-11 11:00:00 +0000 +0000

North Pacific Logbook

The passage from Japan (Shimoda) to Canada (Victoria) took 51 days, and it was the hardest thing we've ever done. We decided to keep a logbook, to better remember it and so it can help others who wish to make this trip.Continue Reading

via Hundred Rabbits 2020-07-31 00:00:00 +0000 GMT