tags:

views:

452

answers:

2

Sure I could write this myself, but before I go reinventing the wheel is there a function that already does this?

+22  A: 

Given an instance x of datetime.date, (x.month-1)//3 will give you the quarter (0 for first quarter, 1 for second quarter, etc -- add 1 if you need to count from 1 instead;-).

Edit: originally two answers, multiply upvoted and even originally ACCEPTED (both currently deleted), were buggy -- not doing the -1 before the division, and dividing by 4 instead of 3. Since .month goes 1 to 12, it's easy to check for yourself what formula is right:

for m in range(1, 13):
  print m//4 + 1,
print

gives 1 1 1 2 2 2 2 3 3 3 3 4 -- two four-month quarters and a single-month one (eep).

for m in range(1, 13):
  print (m-1)//3 + 1,
print

gives 1 1 1 2 2 2 3 3 3 4 4 4 -- now doesn't this look vastly preferable to you?-)

This proves that the question is well warranted, I think;-). I don't think the datetime module should necessarily have every possible useful calendric function, but I do know I maintain a (well-tested;-) datetools module for the use of my (and others') projects at work, which has many little functions to perform all of these calendric computations -- some are complex, some simple, but there's no reason to do the work over and over (even simple work) or risk bugs in such computations;-).

Alex Martelli
+1, you are right
Nadia Alramli
people are in a hurry at times to get that 2+ when you accept an answer
Perpetualcoder
Thanks Alex. This is why there should be a function. Look how many people got it wrong.
Jason Christa
I think you proved your point. It's unnecessary to pollute the entire page with repeated comments.
JG
@Jason, yes, exactly -- that's why I upvoted your question once I saw the other buggy answers (currently deleted), even though somebody else seem to have downvoted to counted my upvote, ah well.
Alex Martelli
@JG, what "repeated comments"? As the buggy answers were deleted, the comments went away with them, so I brought their contents into the answer -- it's important to be aware of how easy it is to be distracted or hurried and get an avoidable bug even in a simple computation, and it has consequences (in terms of making and solidly testing reusable functions) for programming best-practices; this case is a good example (which I think proves the question was warranted).
Alex Martelli
@JG, Alex was right with his comments. I admit I answered too quickly and I should have thought about this more. I've seen this happen before were the OP accepts an answer and then disappears.
Nadia Alramli
@AM is your 'datetools' module available under open license for use outside your org?
myroslav
@myroslav, haven't open-sourced it, but it wouldn't be hard if there was demand, e.g. it would fit as part of http://code.google.com/p/just-the-time/ . What parts are you interested in?
Alex Martelli
A: 

hmmm so calculations can go wrong, here is a better version (just for the sake of it)

first, second, third, fourth=1,2,3,4# you can make strings if you wish :)

quarterMap = {}
quarterMap.update(dict(zip((1,2,3),(first,)*3)))
quarterMap.update(dict(zip((4,5,6),(second,)*3)))
quarterMap.update(dict(zip((7,8,9),(third,)*3)))
quarterMap.update(dict(zip((10,11,12),(fourth,)*3)))

print quarterMap[6]
Anurag Uniyal