This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: request for calendar.dayofyear() function
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: belopolsky Nosy List: JJeffries, ark3, belopolsky, jfinkels, rhettinger
Priority: low Keywords: patch

Created on 2010-09-21 03:27 by jfinkels, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
dayofyear.patch jfinkels, 2010-09-21 03:27 Adds calendar.dayofyear() function.
Messages (6)
msg117024 - (view) Author: Jeffrey Finkelstein (jfinkels) * Date: 2010-09-21 03:27
This is a function which computes the integer between 1 and 366 representing the day of the year, given a year, month, and day.

The implementation I have provided computes the difference between the ordinal numbers of the given day and January first of that month.

This will be useful in resolving issue9864, in which parsing of date strings has an unimplemented "day of year" feature.
msg118941 - (view) Author: JJeffries (JJeffries) Date: 2010-10-17 13:27
I agree, I think this would be very useful. I use a function that does this quite often.

Should also be added to calendar.py's __all__.
msg121773 - (view) Author: Abhay Saxena (ark3) Date: 2010-11-20 20:57
The test is incorrect on leap years for February 29, due to the way it constructs its list of dates. The function itself appears to give the right answer.
msg121776 - (view) Author: Abhay Saxena (ark3) Date: 2010-11-20 21:07
Quick hack alternative test. It would look nicer if the test used the datetime module, but I'm not sure that would be appropriate.


    def test_dayofyear(self):                                                                                                  
        """Test for the calendar.dayofyear() function, which computes the                                                      
        integer between 1 and 366 (inclusive) representing the specified day in                                                
        the specified month of the specified year.                                                                             
                                                                                                                               
        """                                                                                                                    
        for expected_total, year in (366, 2008), (365, 2010):                                                                  
            expected_day_of_year = 1                                                                                           
            for month in range(1, 13):                                                                                         
                lastDay = calendar.mdays[month]                                                                                
                if year == 2008 and month == 2:                                                                                
                    lastDay = 29                                                                                               
                for day in range(1, lastDay + 1):                                                                              
                    day_of_year = calendar.dayofyear(year, month, day)                                                         
                    self.assertEqual(expected_day_of_year, day_of_year)                                                        
                    # The computed day of the year must be between 1 and 366.                                                  
                    self.assertGreaterEqual(day_of_year, 1)                                                                    
                    self.assertLessEqual(day_of_year, 366)                                                                     
                    expected_day_of_year += 1                                                                                  
            self.assertEqual(expected_day_of_year - 1, expected_total)
msg121824 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-11-21 01:02
"We already got one, and it's very nice-a"

ISTM, this should be done with regular date arithmetic in the datetime module.

>>> date(1964, 7, 31) - date(1963, 12, 31)
datetime.timedelta(213)

I don't see why we need a new function for this or why it would be put in the calendar module (where some of its functions have been supplanted by the datetime module).
msg125568 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2011-01-06 17:15
This is yet another request for functionality that can be implemented as a simple expression using datetime arithmetics.  Also, issue #9864 is not a compelling use case because "the parsedate() function now returns a datetime object."   See msg117496.
History
Date User Action Args
2022-04-11 14:57:06adminsetgithub: 54118
2011-03-24 19:31:28belopolskysetstatus: pending -> closed
2011-01-06 17:15:45belopolskysetstatus: open -> pending
nosy: rhettinger, belopolsky, jfinkels, JJeffries, ark3
messages: + msg125568

assignee: belopolsky
resolution: rejected
2011-01-06 16:42:23pitrousetnosy: + belopolsky
2010-11-21 01:02:50rhettingersetpriority: normal -> low
nosy: + rhettinger
messages: + msg121824

2010-11-20 21:07:01ark3setmessages: + msg121776
2010-11-20 20:57:48ark3setnosy: + ark3
messages: + msg121773
2010-10-17 13:27:50JJeffriessetnosy: + JJeffries
messages: + msg118941
2010-09-21 14:55:24belopolskylinkissue9864 dependencies
2010-09-21 03:27:41jfinkelscreate