Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functions strpdate() and daterange(), which make working with date objects more convenient. #75

Merged
merged 3 commits into from
May 21, 2016

Conversation

kiwidrew
Copy link
Contributor

@kiwidrew kiwidrew commented May 17, 2016

I've often wanted to use a strptime()-style format string to parse date values, and find that having a complimentary strpdate() function is clearer than writing e.g. datetime.strptime(val, '%Y-%m-%d').date() all over the place.

Also, iterating over date ranges can be quite awkward, because any date arithmetic requires the involvement of the dreaded timedelta class.

@mahmoud
Copy link
Owner

mahmoud commented May 17, 2016

These are good ones! Nice. A bit of feedback:

  • I think the extra value checks in strpdate are kind of extraneous. If someone wants to parse a date out from a string with hhmmss in it, let them, I say. Of course removing that turns it into a trivial function. But still one I feel like would have made for improved parallelism for the datetime.date type. So still good and mergeable
  • daterange is also good. That said, I see some issues.
    • The first is that asserts are kind of bad form in this case. They can be disabled and don't provide great error messages. Explicit checks with good ValueErrors and TypeErrors are preferable.
    • The second is that day-only ranges are tough for iterating through months and years. Leap years throw it off. And months are of course all different lengths. So we could either complicate this function a lot, or keep it simple. A name change. How about, dayrange?

Thanks for the great work!

@kiwidrew
Copy link
Contributor Author

Thanks for the excellent (and quick) feedback! I really like to idea behind your boltons module, and these are two functions that I have been carrying around with me across various projects. I'll make some changes based on your suggestions and then we can take another look.

@mahmoud
Copy link
Owner

mahmoud commented May 17, 2016

Great! Yes, in fact, ideally, I'd like to include these in the next release, so if it's not likely you'll be able to make the changes in the next day or so, I can merge and modify them myself. It's really no trouble.

@kiwidrew
Copy link
Contributor Author

I'm going to argue for keeping the name daterange, as this is a range-alike function that operates on date values. In the future I would also like to contribute a datetimerange function which can likewise operate on datetime values. I think month and year iterators are out-of-scope, because Python's timedelta object only supports intervals composed of (day, seconds, microseconds).

@kiwidrew
Copy link
Contributor Author

I'll be unavailable for the next couple of days, so please feel free to make any additional changes that you think are required – I trust your judgement here.

@mahmoud mahmoud merged commit 1027e5f into mahmoud:master May 21, 2016
mahmoud added a commit that referenced this pull request May 21, 2016
…s well as add some tests and tweak some docstrings
@mahmoud
Copy link
Owner

mahmoud commented May 21, 2016

So, I made a few tweaks, here are the highlights:

  • Year and month are now supported with a reasonably compact syntax
  • Negative steps are also supported (including with year and month)
  • The stop argument is now required (though None is still supported for infinite sequences, a bit too dangerous of an implicit behavior)

Have a look at the docs and take it for a spin! http://boltons.readthedocs.io/en/latest/timeutils.html#boltons.timeutils.daterange

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants