This is a tool that takes a string representing a date range and returns a dictionary or it can translate it to a date range. The interesting thing is this library can take many different strings representing different ranges or time periods and return the appropriate date range.
Takes a string representing some sort of date or date range in the form of:
- A 4 digit year -> yyyy -> 0000 - 9999
- An isoyear-iso month (yyyy-mm)
- Isoweek (yyyyWnn), week (01 - 53)
- assumes it is a week in the past of the current year
- Isodate - a date in iso format (yyyy-mm-dd)
- Week number - 1 to 53
- Assumes current year
- Week offset (0, -1, -2)
- The relative week less than or equal to 0,
- date range
- that is one yyyy-mm-dd to another ("yyyy-mm-dd to yyyy-mm-dd")
It will return a dictionary with one of the following keys:
- 'year' -> returns an integer
- 'year-month' -> tuple(int(year), int(mm))
- 'year-month-day' -> tuple(int(year), int(mm), int(dd))
- 'year-week' -> tuple(int(year), int(week))
- 'week-offset' -> int
- 'week-number' -> int
- 'date-range' -> tuple(tuple(int(year), int(mm), int(dd)), tuple(int(year), int(mm), int(dd)))
- first element is the start date, second element is the end date
To get this project up and running from the repository, it uses Rye as the build/dependency manager. There are instructions for installing Rye on many different systems. This set of instructions are for Linux and windows. See the installation guide for other operating systems.
You have to download Rye to your system. Follow the installation guide for your operating system.
Why Rye? That is a good question. Python is a great language but it is tough to create a reproducible environment. You have to have the correct version of Python installed or available. You have to have the correct tools configured. If you are on Linux/BSD you have to make sure that your work doesn't mess up your system Python installation. It is fairly trivial if you are experienced, but annoying enough to have to do it over-and-over again. If you are new, it can be extremely difficult.
Rye takes care of handling the different versions of Python and managing the tools you need for a reproducible environment, particularly if you are doing cross-platform work.
For Linux, you can use the following:
curl -sSf https://rye-up.com/get | bash
There are also good guides to configuring Rye for your shell. Here is what I had to do to get it working in ZSH on my system.
Edit .zshrc:
vi ~/.zshrc
Add the following:
source "$HOME/.rye/env"
Restart the terminal and type rye. To add shell completion, you can:
mkdir $ZSH_CUSTOM/plugins/rye
rye self completion -s zsh > $ZSH_CUSTOM/plugins/rye/_rye
For windows, download the installer listed in the installation guide link.
rye self update
Once you have rye properly installed, you can run rye sync, to build (or update) the virtual environment.
Create/Update Virtual Environment
rye sync
NOTE: This needs to be run from within the repository. If you add new dependencies or modify the pyproject.toml you should run rye sync.
You can add the following alias to your .zshrc or .bashrc, or you can run the activate script directly:
# Python Virtual Environment Alias
alias activate="source .venv/bin/activate"
NOTE: On Windows, there is an activate.ps1, a PowerShell script that you can execute.
BuiUse the module like:
>>> from dateranger.dateranger import date_range_str
>>> date_range_str('2020-01-01 - 2020-07-31')
(date(2020,1,1),date(2020,7,31))
>>> date_range_str('2022W03')
(date(2022,1,17),date(2022,1,23))
Please refer to LICENSE.md.