Monday, May 4, 2015

Re: How to represent a calendar month as a field in django models

Hi Erik,

Thanks for your reply.

I'm aware of the problems with timedelta(months=2), since the length of a month is not fixed, a length of time specified in months is ambiguous. Though months do have an internally consistent algebra as you point out and people often refer to time-periods in month without any real-world confusion. I agree it is possible to create a class modelling this behaviour.

The downside to creating a new classt, is that all the existing date-related functionality in the standard library and django is lost and has to be rewritten or wrapped somehow.

I think I'll just have to look at my use-cases and pick the option that most easily facilitates those uses.

Cheers,



On Mon, May 4, 2015 at 2:30 PM, Erik Cederstrand <erik+lists@cederstrand.dk> wrote:

> Den 04/05/2015 kl. 14.21 skrev Erik Cederstrand <erik+lists@cederstrand.dk>:
>
> class Month(models.Model):
>    year = models.IntegerField()
>    month = models.PositiveSmallIntegerField()
>
>    def add_months(self, n):
>        assert n >= 0
>        dy, dm = divmod(self.month + n, 12)
>        self.year += dy
>        self.month += dm

Sorry, that's supposed to be:

    def add_months(self, n):
        assert n >= 0
        dy, self.month = divmod(self.month + n, 12)
        self.year += dy


Disclaimer: This possibly only applies to the Gregorian calendar. Read up on the others as needed: http://en.wikipedia.org/wiki/List_of_calendars :-)

Erik

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/vmsiHYzI-gg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/B025BC73-DE58-4E4F-9F55-B94CC990381E%40cederstrand.dk.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMtJH%2Bs7gvLj%2BiixemRZLqsurC7fsL3mDtBQ7rirPK%3DAwzJ2YQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment