Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1026459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:05:10+00:00 2026-05-16T12:05:10+00:00

I’m processing data from an application that has a few quirks in how it

  • 0

I’m processing data from an application that has a few quirks in how it keeps time. One of the simpler quirks is that it uses “day of year” (Jan 1 is 1, Febuary 1 is 32, etc) instead of month + day. So I want to make my own date class that inherits from the default datetime class and has a few custom methods. I’m calling this daytime. In addition to methods that output to my weird formats, I need methods that input the weird format into a daytime. Just like the method .fromordinal()can be called from datetime, I want to have a method .fromdayofyear() which can be called from daytime. So far I got:

import datetime

class daytime(datetime.datetime):
    @property
    def dayofyear(self):
        return (self.date - datetime.date(self.year,1,1)).days + 1
    def fromdayofyear(year,dayofyear):
        return datetime(year,1,1)+datetime.timedelta(dayofyear-1)

The problem is that since fromdayofyear is a class, it needs an instance of daytime to perform any methods.

>>> from utils import daytime
>>> day = daytime.fromdayofyear(2010,32)          #should give a datetime, Feburary first
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unbound method from_datetime() must be called with DayTime instance as first argument (got str instance instead)
>>>

I could easily write a series of functions to do this, but that defeats the point of making a custom class to begin with. I realize that I probably shouldn’t be using a class here. Can someone point me in the right direction?

EDIT:

Here’s what I settled on:

import datetime

class daytime(datetime.datetime):
    @property
    def dayofyear(self):
        return (self.date - datetime.date(self.year,1,1)).days + 1
    @classmethod
    def fromdayofyear(cls,year,dayofyear):
        dt = cls(year,1,1)+datetime.timedelta(dayofyear-1)
        return cls(dt.year,dt.month,dt.day)
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T12:05:11+00:00Added an answer on May 16, 2026 at 12:05 pm

    The OP’s “settled on” solution has serious bugs (self.date needs to be called, not just mentioned; and the fromdayofyear method actually returns a datetime.datetime instance, not a daytime one, despite the apparent attempt to do so by using cls).

    Here’s a version which I believe works as intended:

    class daytime(datetime.datetime):
    
        @property
        def dayofyear(self):
            return (self.date() - datetime.date(self.year, 1, 1)).days + 1
    
        @classmethod
        def fromdayofyear(cls, year, dayofyear):
            dt = datetime.datetime(year, 1, 1) + datetime.timedelta(dayofyear-1)
            return cls(dt.year, dt.month, dt.day)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.