Archive for September 2012

How (not) to screw up timezone processing in Python

If you use pytz without reading the documentation, you might think you can do this:

cltime = datetime.datetime(

This is an easy way to utterly screw up your timezones.

Never pass a tzinfo into the datetime constructor. Here’s why:

>>> cltime = datetime.datetime(
>>> cltime.astimezone(pytz.timezone('US/Pacific')).isoformat()

That’s simply the wrong time. 1:15 Chilean time should be 21:15 Pacific the previous day, not 22:58.

For whatever reason, the wrong tzinfo is attached to the object. Compare:

>>> cltime.tzinfo
<DstTzInfo 'America/Santiago' SMT-1 day, 19:17:00 STD>
<DstTzInfo 'America/Santiago' CLST-1 day, 21:00:00 DST>

The correct way is to use pytz.localize():

>>> correct_cltime = pytz.timezone('America/Santiago'
>>> correct_cltime.astimezone(pytz.timezone('US/Pacific')).isoformat()
>>> correct_cltime.tzinfo
<DstTzInfo 'America/Santiago' CLST-1 day, 21:00:00 DST>

The pytz documentation does not indicate if creates the correct timezone, so I tested it:

>>> for tz in pytz.all_timezones:
...     assert
...         ).tzinfo == pytz.UTC.localize(datetime.datetime.utcnow()
...         ).astimezone(pytz.timezone(tz)).tzinfo

The assertion never failed, so it is safe to use"[timezonename]")) to generate the current date in a specific timezone.

In general, you should store dates in UTC format and convert them to the user’s timezone at the latest possible time. Try to never store naive datetimes. if you need the current time in UTC, use


because utcnow() returns a naive datetime. Localizing it to UTC adds the correct timezone to it.


is the correct way to construct a datetime in the UTC timezone. Technically, you CAN pass tzinfo to the datetime constructor if tzinfo is UTC. This is because UTC does not have daylight savings time. However, for consistency, do not do this; do not ever pass tzinfo into a datetime constructor.

Converting to or from UTC is simple, using astimezone:

>>> utctime = cltime.astimezone(pytz.UTC)
>>> utctime
datetime.datetime(2012, 9, 26, 4, 15, tzinfo=<UTC>)
>>> utctime.astimezone(pytz.timezone('America/Santiago'))
datetime.datetime(2012, 9, 26, 1, 15, tzinfo=<DstTzInfo 'America/Santiago' CLST-1 day, 21:00:00 DST>)

Arch Linux Handbook for Kindle

I have had a handful of requests that the Arch Linux Handbook be made available for the Kindle platform. It seemed like an odd request, given that the latest version of the Beginners’ Guide is already freely available in electronic format online. However, I had some free time this week and tried the conversion. It wasn’t difficult and I uploaded a version of the Handbook to the Kindle app store. I’ve helped publish other books to Kindle, so I already knew the process.

I received an e-mail from Amazon Customer Service to the following effect:

During a review of your KDP submission(s), we found content that is freely available on the web. You can do an online search for the content inside your book(s) to discover which sites are offering the content for free. Copyright is important to us – we want to make sure that no author or other copyright holder has their work claimed and sold by anyone else.

To confirm you have publishing rights to and control where you distribute the book(s), please provide all of the following information:

1. The URLs for all websites where this content is published
2. An explanation as to why the content is available online

If the books are in the public domain, please confirm this and include the information you used to make this determination. We may request additional information to confirm the public domain status.

Please respond within 5 days to, and include the title and ID of your books in your reply. Your book has been moved to a blocked status on your bookshelf and will not be available for sale in the Kindle store until we receive the documentation requested.

Sure, no problem. I responded to explain that it was freely redistributable under the Free Documentation License:

This content is indeed freely available on the web at although I have done a certain amount of editing to get it into its current format.

However, this freely available content is published under the GNU Free Documentation License 1.3 or later. ( which explicitly states:

“The purpose of this License is to make a manual, textbook, or other functional and useful document “free” in the sense of freedom: to assure everyone the effective freedom to copy and redistribute it, with or without modifying it, either commercially or noncommercially. Secondarily, this License preserves for the author and publisher a way to get credit for their work, while not being considered responsible for modifications made by others.”

Their response makes me sound like some kind of criminal:


We’ve reviewed the information you provided and have decided to block these books from being sold in the Kindle Store. The books closely match content that is freely available on the web and we are not confident that you hold exclusive publishing rights. This type of content can create a poor customer experience, and is not accepted. As a result, we have blocked the books listed below from being sold in the Kindle Store.

Arch Linux Handbook 3.0 by Phillips, Dusty (EDITOR) (ID: 2884216)

Please be advised that you must hold exclusive publishing rights for books that closely match content that is freely available on the web. If your catalog continues to contain books that fail to comply with these conditions or do not meet our Content Guidelines, your account may be terminated.

The content guidelines applicable to all Kindle Direct Publishing (KDP) publishers can be found here:

Best regards,

Megan B.
Your feedback is helping us build Earth’s Most Customer-Centric Company.

I was insulted and hurt by this message. It’s arrogant and it is confrontational. It makes no effort to address the specifics of the e-mail. But I cheered up at the end, or at least I laughed, “Customer-Centric Company”? Don’t advertise what you aren’t.

The original message made no mention of the fact that I should have “exclusive” publishing rights. It even said public domain work was acceptable. I have published Creative Commons books that are freely available online on the Kindle before (although they are a non-commercial creative commons license, so we still have “exclusive” rights to publish on Kindle).

In other news, the Arch Linux Handbook can be downloaded in .mobi format, free of charge, from


After this article unexpectedly hit Reddit and Hacker News (my target audience was the Arch Linux Community), Amazon let the title go through and it is now available from They have been in contact with me but have not been able to explain why the book has been allowed to be published! The best explanation I got was:

We’ve re-reviewed your content and have determined it may be published to the Kindle Store. We generally can’t accept content that closely matches content that is freely available on the web, for which you do not hold the sole publishing rights, or that which is not in the public domain. For example, content from Wikipedia and content with private label rights are not allowed since it disappoints our customers to pay for content that is freely available on the web.

Since the Arch Linux Beginners’ Guide is a wiki-developed article no different from Wikipedia, I’m not certain why the Handbook is now allowed.

I originally posted this article to explain why the handbook is not available in the Kindle store. My intent was not to complain about Amazon’s policies (although I was not happy with the suggestion that I am prone to criminal activity). The new purpose is to mention it’s availability through the store, although you can still download my quick one-off conversion if it suits you as well. In all honesty, I can’t imagine why anyone would want to purchase something that is already freely available in electronic format, but at least now that choice is open.