Gifted Slacker

Last Day of the Month

Posted in Internet/Web, Software by Grant on June 15th, 2008

When a blog has never been visited my dissertation software retrieves any posts from the past 24 hours. This isn’t hard:

from datetime import datetime
lastvisit = datetime.utcnow().replace(day=(datetime.utcnow().day-1))

But when this happens and it’s the first day of the month you get:
ValueError: day is out of range for month
Since my project is only going to run for this summer, I did changed it so that it will work this year but not next year (sorry, WordPress strips out all of the tabs…):
from datetime import datetime

n = datetime.utcnow()
if n.day != 1:
    lastvisit = datetime.utcnow().replace(day=(datetime.utcnow().day-1))
elif n.month - 1 in (1, 3, 5, 7, 8, 10, 12):
    lastvisit = datetime.utcnow().replace(month=(n.month-1), day=31)
elif n.month - 1 in (4, 6, 9, 11):
    lastvisit = datetime.utcnow().replace(month=(n.month-1), day=30)
elif n.month - 1 == 2:
    try:
        lastvisit = datetime.utcnow().replace(month=(n.month-1), day=29)
    except ValueError:
        lastvisit = datetime.utcnow().replace(month=(n.month-1), day=2 8)


Today I ran across this article on ASPN. And now here’s what I would write (omitting some of the boilerplate from above):

if n.day == 1:
    days_in_month = [calendar.monthrange(year,month)[1] for year in [n.year] for month in range(1,13)]
    datetime.utcnow().replace(day=days_in_month[n.month-2], month=n.month-1)
elif:
    datetime.utcnow().replace(day=n.day-1)


If I would have spent 5 minutes looking for The Right WayTM to do this, I would have saved a little time and had cleaner code. Now I don’t want to change what I’ve got working well enough.

Dear Lazyweb: How do I post pretty code snippets?

10 Responses to 'Last Day of the Month'

Subscribe to comments with RSS or TrackBack to 'Last Day of the Month'.

  1. ethan said, on June 16th, 2008 at 5:55 pm

    http://ethancodes.com/entry/10

  2. Grant said, on June 16th, 2008 at 6:46 pm

    Nice. But I can’t do any CSS on this blog, as it’s hosted on WordPress.com… I think that means it’s time to move it to my own space….

  3. ethan said, on June 16th, 2008 at 7:46 pm

    Baloney: http://codex.wordpress.org/Writing_Code_in_Your_Posts

  4. ethan said, on June 16th, 2008 at 7:46 pm

    Fooey.

  5. ethan said, on June 16th, 2008 at 7:47 pm

    Poppy cock.

  6. ethan said, on June 16th, 2008 at 7:47 pm

    Hogwash.

  7. Grant said, on June 18th, 2008 at 5:30 pm

    I tried using pre and code tags, but WordPress still munges things up pretty good. And they aren’t exactly what I’m looking for.

    I ran across a snippet of code a while back that generated syntax-highlighted html versions of code listings… I can’t seem to find it anymore.

  8. ethan said, on June 18th, 2008 at 9:13 pm

    Found this: http://wordpress.org/extend/plugins/wp-syntax/

    Using here: http://ethancodes.com/?p=4

  9. ethan said, on June 18th, 2008 at 9:22 pm

    And here: http://ethancodes.com/?p=5

  10. Grant said, on June 18th, 2008 at 11:19 pm

    Thanks. I haven’t figured out how to add plugins to a WP.com hosted blog. It’s annoying, but that’s how it goes… I’m trying to free up enough cognitive bandwidth to move this to my own server or write up a simple custom rails app to blog on… (overkill, I know).

Leave a Reply