Skip to content

request for calendar.dayofyear() function #54118

Closed

Description

@jfinkels
mannequin
BPO 9909
Nosy @rhettinger, @abalkin
Files
  • dayofyear.patch: Adds calendar.dayofyear() function.
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/abalkin'
    closed_at = <Date 2011-03-24.19:31:28.511>
    created_at = <Date 2010-09-21.03:27:41.378>
    labels = ['type-feature', 'library']
    title = 'request for calendar.dayofyear() function'
    updated_at = <Date 2011-03-24.19:31:28.510>
    user = 'https://bugs.python.org/jfinkels'

    bugs.python.org fields:

    activity = <Date 2011-03-24.19:31:28.510>
    actor = 'belopolsky'
    assignee = 'belopolsky'
    closed = True
    closed_date = <Date 2011-03-24.19:31:28.511>
    closer = 'belopolsky'
    components = ['Library (Lib)']
    creation = <Date 2010-09-21.03:27:41.378>
    creator = 'jfinkels'
    dependencies = []
    files = ['18941']
    hgrepos = []
    issue_num = 9909
    keywords = ['patch']
    message_count = 6.0
    messages = ['117024', '118941', '121773', '121776', '121824', '125568']
    nosy_count = 5.0
    nosy_names = ['rhettinger', 'belopolsky', 'jfinkels', 'JJeffries', 'ark3']
    pr_nums = []
    priority = 'low'
    resolution = 'rejected'
    stage = None
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue9909'
    versions = ['Python 3.3']

    Activity

    1. jfinkels commented on Sep 21, 2010

      jfinkelsmannequin
      MannequinAuthor

      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 bpo-9864, in which parsing of date strings has an unimplemented "day of year" feature.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      on Sep 21, 2010
    3. JJeffries commented on Oct 17, 2010

      JJeffriesmannequin
      Mannequin

      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__.

    4. ark3 commented on Nov 20, 2010

      ark3mannequin
      Mannequin

      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.

    5. ark3 commented on Nov 20, 2010

      ark3mannequin
      Mannequin

      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)
    6. rhettinger commented on Nov 21, 2010

      @rhettinger
      Contributor

      "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).

    7. abalkin commented on Jan 6, 2011

      @abalkin
      Member

      This is yet another request for functionality that can be implemented as a simple expression using datetime arithmetics. Also, issue bpo-9864 is not a compelling use case because "the parsedate() function now returns a datetime object." See msg117496.

    8. self-assigned this
      on Jan 6, 2011
    9. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions