Article 4MA0R CodeSOD: What a Happy Date

CodeSOD: What a Happy Date

by
Remy Porter
from The Daily WTF on (#4MA0R)

As is the case with pretty much any language these days, Python comes with robust date handling functionality. If you want to know something like what the day of the month is? datetime.now().day will tell you. Simple, easy, and of course, just an invitation for someone to invent their own.

Jan was witness to a little date-time related office politics. This particular political battle started during a code review. Klaus had written some date mangling code, relying heavily on strftime to parse dates out to strings and then parse them back in as integers. Richard, quite reasonably, pointed out that Klaus was taking the long way around, and maybe Klaus should possibly think about doing it in a simpler fashion.

"So, you don't understand the code?" Klaus asked.

"No, I understand it," Richard replied. "But it's far too complicated. You're doing a simple task- getting the day of the month! The code should be simple."

"Ah, so it's too complicated, so you can't understand it."

"Just" write it the simple way. Use the built-in accessor."

So, Klaus made his revisions, and merged the revised code.

import datetime# ...now = datetime.datetime.now() # Richarddate = now.strftime("%d") # Richard, this is a string over heredate_int = int(date) # day number, int("08") = 8, so no problem herehour = now.hour # Richard :)))))hour_int = int(hour) # int hour, e.g. if it's 22:36 then hour = 22

Richard did not have a big :))))) on his face when he saw that in the master branch.

proget-icon.png [Advertisement] Ensure your software is built only once and then deployed consistently across environments, by packaging your applications and components. Learn how today! TheDailyWtf?d=yIl2AUoC8zA57xRFmDBGK0
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments