Hacker News new | past | comments | ask | show | jobs | submit login

I wasn't aware about the functions until this thread, and my expectations match the grandparent. Why is ts not equal to x_ts?



It's actually worse than I had initially suspected. Pythons' dateutil timezone handling is, depending on your opinion, either hard to reason about or just outright broken - certainly it tries to be clever in unintuitive ways. This is why there is a couple of external libraries, like dateutil to try and make timezone things easier - which is even mentioned at the beginning of pythons' datetime documentation!

In this case, I believe that it's actually the inverse from what I thought - utcfromtimestamp is doing... nothing; but is different by the fact that nothing is different from everything else. It looks like .fromtimestamp builds a "naive" datetime in local time. So, it assumes the timestamp is UTC and "helpfully" converts it to the local date and time. When you do .timestamp it inverses this process and gives you back the timestamp value in UTC by converting implicitly from your local time zone. Thus, if you do .utcfromtimestamp it doesn't do this conversion, and gives you a naive datetime "in UTC".

This is horrible and I fully support the recommendation never to use it.


Turns out the answer is what the article is all about.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: